vf_nlmeans.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  1. /*
  2. * Copyright (c) 2016 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. /**
  21. * @todo
  22. * - better automatic defaults? see "Parameters" @ http://www.ipol.im/pub/art/2011/bcm_nlm/
  23. * - temporal support (probably doesn't need any displacement according to
  24. * "Denoising image sequences does not require motion estimation")
  25. * - Bayer pixel format support for at least raw photos? (DNG support would be
  26. * handy here)
  27. * - FATE test (probably needs visual threshold test mechanism due to the use
  28. * of floats)
  29. */
  30. #include "libavutil/avassert.h"
  31. #include "libavutil/opt.h"
  32. #include "libavutil/pixdesc.h"
  33. #include "avfilter.h"
  34. #include "formats.h"
  35. #include "internal.h"
  36. #include "vf_nlmeans.h"
  37. #include "video.h"
  38. struct weighted_avg {
  39. float total_weight;
  40. float sum;
  41. };
  42. typedef struct NLMeansContext {
  43. const AVClass *class;
  44. int nb_planes;
  45. int chroma_w, chroma_h;
  46. double pdiff_scale; // invert of the filtering parameter (sigma*10) squared
  47. double sigma; // denoising strength
  48. int patch_size, patch_hsize; // patch size and half size
  49. int patch_size_uv, patch_hsize_uv; // patch size and half size for chroma planes
  50. int research_size, research_hsize; // research size and half size
  51. int research_size_uv, research_hsize_uv; // research size and half size for chroma planes
  52. uint32_t *ii_orig; // integral image
  53. uint32_t *ii; // integral image starting after the 0-line and 0-column
  54. int ii_w, ii_h; // width and height of the integral image
  55. ptrdiff_t ii_lz_32; // linesize in 32-bit units of the integral image
  56. struct weighted_avg *wa; // weighted average of every pixel
  57. ptrdiff_t wa_linesize; // linesize for wa in struct size unit
  58. float *weight_lut; // lookup table mapping (scaled) patch differences to their associated weights
  59. uint32_t max_meaningful_diff; // maximum difference considered (if the patch difference is too high we ignore the pixel)
  60. NLMeansDSPContext dsp;
  61. } NLMeansContext;
  62. #define OFFSET(x) offsetof(NLMeansContext, x)
  63. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  64. static const AVOption nlmeans_options[] = {
  65. { "s", "denoising strength", OFFSET(sigma), AV_OPT_TYPE_DOUBLE, { .dbl = 1.0 }, 1.0, 30.0, FLAGS },
  66. { "p", "patch size", OFFSET(patch_size), AV_OPT_TYPE_INT, { .i64 = 3*2+1 }, 0, 99, FLAGS },
  67. { "pc", "patch size for chroma planes", OFFSET(patch_size_uv), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 99, FLAGS },
  68. { "r", "research window", OFFSET(research_size), AV_OPT_TYPE_INT, { .i64 = 7*2+1 }, 0, 99, FLAGS },
  69. { "rc", "research window for chroma planes", OFFSET(research_size_uv), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 99, FLAGS },
  70. { NULL }
  71. };
  72. AVFILTER_DEFINE_CLASS(nlmeans);
  73. static int query_formats(AVFilterContext *ctx)
  74. {
  75. static const enum AVPixelFormat pix_fmts[] = {
  76. AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P,
  77. AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P,
  78. AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P,
  79. AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P,
  80. AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
  81. AV_PIX_FMT_YUVJ411P,
  82. AV_PIX_FMT_GRAY8, AV_PIX_FMT_GBRP,
  83. AV_PIX_FMT_NONE
  84. };
  85. AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
  86. if (!fmts_list)
  87. return AVERROR(ENOMEM);
  88. return ff_set_common_formats(ctx, fmts_list);
  89. }
  90. /**
  91. * Compute squared difference of the safe area (the zone where s1 and s2
  92. * overlap). It is likely the largest integral zone, so it is interesting to do
  93. * as little checks as possible; contrary to the unsafe version of this
  94. * function, we do not need any clipping here.
  95. *
  96. * The line above dst and the column to its left are always readable.
  97. */
  98. static void compute_safe_ssd_integral_image_c(uint32_t *dst, ptrdiff_t dst_linesize_32,
  99. const uint8_t *s1, ptrdiff_t linesize1,
  100. const uint8_t *s2, ptrdiff_t linesize2,
  101. int w, int h)
  102. {
  103. int x, y;
  104. const uint32_t *dst_top = dst - dst_linesize_32;
  105. /* SIMD-friendly assumptions allowed here */
  106. av_assert2(!(w & 0xf) && w >= 16 && h >= 1);
  107. for (y = 0; y < h; y++) {
  108. for (x = 0; x < w; x += 4) {
  109. const int d0 = s1[x ] - s2[x ];
  110. const int d1 = s1[x + 1] - s2[x + 1];
  111. const int d2 = s1[x + 2] - s2[x + 2];
  112. const int d3 = s1[x + 3] - s2[x + 3];
  113. dst[x ] = dst_top[x ] - dst_top[x - 1] + d0*d0;
  114. dst[x + 1] = dst_top[x + 1] - dst_top[x ] + d1*d1;
  115. dst[x + 2] = dst_top[x + 2] - dst_top[x + 1] + d2*d2;
  116. dst[x + 3] = dst_top[x + 3] - dst_top[x + 2] + d3*d3;
  117. dst[x ] += dst[x - 1];
  118. dst[x + 1] += dst[x ];
  119. dst[x + 2] += dst[x + 1];
  120. dst[x + 3] += dst[x + 2];
  121. }
  122. s1 += linesize1;
  123. s2 += linesize2;
  124. dst += dst_linesize_32;
  125. dst_top += dst_linesize_32;
  126. }
  127. }
  128. /**
  129. * Compute squared difference of an unsafe area (the zone nor s1 nor s2 could
  130. * be readable).
  131. *
  132. * On the other hand, the line above dst and the column to its left are always
  133. * readable.
  134. *
  135. * There is little point in having this function SIMDified as it is likely too
  136. * complex and only handle small portions of the image.
  137. *
  138. * @param dst integral image
  139. * @param dst_linesize_32 integral image linesize (in 32-bit integers unit)
  140. * @param startx integral starting x position
  141. * @param starty integral starting y position
  142. * @param src source plane buffer
  143. * @param linesize source plane linesize
  144. * @param offx source offsetting in x
  145. * @param offy source offsetting in y
  146. * @paran r absolute maximum source offsetting
  147. * @param sw source width
  148. * @param sh source height
  149. * @param w width to compute
  150. * @param h height to compute
  151. */
  152. static inline void compute_unsafe_ssd_integral_image(uint32_t *dst, ptrdiff_t dst_linesize_32,
  153. int startx, int starty,
  154. const uint8_t *src, ptrdiff_t linesize,
  155. int offx, int offy, int r, int sw, int sh,
  156. int w, int h)
  157. {
  158. int x, y;
  159. for (y = starty; y < starty + h; y++) {
  160. uint32_t acc = dst[y*dst_linesize_32 + startx - 1] - dst[(y-1)*dst_linesize_32 + startx - 1];
  161. const int s1y = av_clip(y - r, 0, sh - 1);
  162. const int s2y = av_clip(y - (r + offy), 0, sh - 1);
  163. for (x = startx; x < startx + w; x++) {
  164. const int s1x = av_clip(x - r, 0, sw - 1);
  165. const int s2x = av_clip(x - (r + offx), 0, sw - 1);
  166. const uint8_t v1 = src[s1y*linesize + s1x];
  167. const uint8_t v2 = src[s2y*linesize + s2x];
  168. const int d = v1 - v2;
  169. acc += d * d;
  170. dst[y*dst_linesize_32 + x] = dst[(y-1)*dst_linesize_32 + x] + acc;
  171. }
  172. }
  173. }
  174. /*
  175. * Compute the sum of squared difference integral image
  176. * http://www.ipol.im/pub/art/2014/57/
  177. * Integral Images for Block Matching - Gabriele Facciolo, Nicolas Limare, Enric Meinhardt-Llopis
  178. *
  179. * @param ii integral image of dimension (w+e*2) x (h+e*2) with
  180. * an additional zeroed top line and column already
  181. * "applied" to the pointer value
  182. * @param ii_linesize_32 integral image linesize (in 32-bit integers unit)
  183. * @param src source plane buffer
  184. * @param linesize source plane linesize
  185. * @param offx x-offsetting ranging in [-e;e]
  186. * @param offy y-offsetting ranging in [-e;e]
  187. * @param w source width
  188. * @param h source height
  189. * @param e research padding edge
  190. */
  191. static void compute_ssd_integral_image(const NLMeansDSPContext *dsp,
  192. uint32_t *ii, ptrdiff_t ii_linesize_32,
  193. const uint8_t *src, ptrdiff_t linesize, int offx, int offy,
  194. int e, int w, int h)
  195. {
  196. // ii has a surrounding padding of thickness "e"
  197. const int ii_w = w + e*2;
  198. const int ii_h = h + e*2;
  199. // we center the first source
  200. const int s1x = e;
  201. const int s1y = e;
  202. // 2nd source is the frame with offsetting
  203. const int s2x = e + offx;
  204. const int s2y = e + offy;
  205. // get the dimension of the overlapping rectangle where it is always safe
  206. // to compare the 2 sources pixels
  207. const int startx_safe = FFMAX(s1x, s2x);
  208. const int starty_safe = FFMAX(s1y, s2y);
  209. const int u_endx_safe = FFMIN(s1x + w, s2x + w); // unaligned
  210. const int endy_safe = FFMIN(s1y + h, s2y + h);
  211. // deduce the safe area width and height
  212. const int safe_pw = (u_endx_safe - startx_safe) & ~0xf;
  213. const int safe_ph = endy_safe - starty_safe;
  214. // adjusted end x position of the safe area after width of the safe area gets aligned
  215. const int endx_safe = startx_safe + safe_pw;
  216. // top part where only one of s1 and s2 is still readable, or none at all
  217. compute_unsafe_ssd_integral_image(ii, ii_linesize_32,
  218. 0, 0,
  219. src, linesize,
  220. offx, offy, e, w, h,
  221. ii_w, starty_safe);
  222. // fill the left column integral required to compute the central
  223. // overlapping one
  224. compute_unsafe_ssd_integral_image(ii, ii_linesize_32,
  225. 0, starty_safe,
  226. src, linesize,
  227. offx, offy, e, w, h,
  228. startx_safe, safe_ph);
  229. // main and safe part of the integral
  230. av_assert1(startx_safe - s1x >= 0); av_assert1(startx_safe - s1x < w);
  231. av_assert1(starty_safe - s1y >= 0); av_assert1(starty_safe - s1y < h);
  232. av_assert1(startx_safe - s2x >= 0); av_assert1(startx_safe - s2x < w);
  233. av_assert1(starty_safe - s2y >= 0); av_assert1(starty_safe - s2y < h);
  234. if (safe_pw && safe_ph)
  235. dsp->compute_safe_ssd_integral_image(ii + starty_safe*ii_linesize_32 + startx_safe, ii_linesize_32,
  236. src + (starty_safe - s1y) * linesize + (startx_safe - s1x), linesize,
  237. src + (starty_safe - s2y) * linesize + (startx_safe - s2x), linesize,
  238. safe_pw, safe_ph);
  239. // right part of the integral
  240. compute_unsafe_ssd_integral_image(ii, ii_linesize_32,
  241. endx_safe, starty_safe,
  242. src, linesize,
  243. offx, offy, e, w, h,
  244. ii_w - endx_safe, safe_ph);
  245. // bottom part where only one of s1 and s2 is still readable, or none at all
  246. compute_unsafe_ssd_integral_image(ii, ii_linesize_32,
  247. 0, endy_safe,
  248. src, linesize,
  249. offx, offy, e, w, h,
  250. ii_w, ii_h - endy_safe);
  251. }
  252. static int config_input(AVFilterLink *inlink)
  253. {
  254. AVFilterContext *ctx = inlink->dst;
  255. NLMeansContext *s = ctx->priv;
  256. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  257. const int e = FFMAX(s->research_hsize, s->research_hsize_uv)
  258. + FFMAX(s->patch_hsize, s->patch_hsize_uv);
  259. s->chroma_w = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w);
  260. s->chroma_h = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
  261. s->nb_planes = av_pix_fmt_count_planes(inlink->format);
  262. /* Allocate the integral image with extra edges of thickness "e"
  263. *
  264. * +_+-------------------------------+
  265. * |0|0000000000000000000000000000000|
  266. * +-x-------------------------------+
  267. * |0|\ ^ |
  268. * |0| ii | e |
  269. * |0| v |
  270. * |0| +-----------------------+ |
  271. * |0| | | |
  272. * |0|<->| | |
  273. * |0| e | | |
  274. * |0| | | |
  275. * |0| +-----------------------+ |
  276. * |0| |
  277. * |0| |
  278. * |0| |
  279. * +-+-------------------------------+
  280. */
  281. s->ii_w = inlink->w + e*2;
  282. s->ii_h = inlink->h + e*2;
  283. // align to 4 the linesize, "+1" is for the space of the left 0-column
  284. s->ii_lz_32 = FFALIGN(s->ii_w + 1, 4);
  285. // "+1" is for the space of the top 0-line
  286. s->ii_orig = av_mallocz_array(s->ii_h + 1, s->ii_lz_32 * sizeof(*s->ii_orig));
  287. if (!s->ii_orig)
  288. return AVERROR(ENOMEM);
  289. // skip top 0-line and left 0-column
  290. s->ii = s->ii_orig + s->ii_lz_32 + 1;
  291. // allocate weighted average for every pixel
  292. s->wa_linesize = inlink->w;
  293. s->wa = av_malloc_array(s->wa_linesize, inlink->h * sizeof(*s->wa));
  294. if (!s->wa)
  295. return AVERROR(ENOMEM);
  296. return 0;
  297. }
  298. struct thread_data {
  299. const uint8_t *src;
  300. ptrdiff_t src_linesize;
  301. int startx, starty;
  302. int endx, endy;
  303. const uint32_t *ii_start;
  304. int p;
  305. };
  306. static int nlmeans_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  307. {
  308. int x, y;
  309. NLMeansContext *s = ctx->priv;
  310. const struct thread_data *td = arg;
  311. const ptrdiff_t src_linesize = td->src_linesize;
  312. const int process_h = td->endy - td->starty;
  313. const int slice_start = (process_h * jobnr ) / nb_jobs;
  314. const int slice_end = (process_h * (jobnr+1)) / nb_jobs;
  315. const int starty = td->starty + slice_start;
  316. const int endy = td->starty + slice_end;
  317. const int p = td->p;
  318. const uint32_t *ii = td->ii_start + (starty - p - 1) * s->ii_lz_32 - p - 1;
  319. const int dist_b = 2*p + 1;
  320. const int dist_d = dist_b * s->ii_lz_32;
  321. const int dist_e = dist_d + dist_b;
  322. for (y = starty; y < endy; y++) {
  323. const uint8_t *src = td->src + y*src_linesize;
  324. struct weighted_avg *wa = s->wa + y*s->wa_linesize;
  325. for (x = td->startx; x < td->endx; x++) {
  326. /*
  327. * M is a discrete map where every entry contains the sum of all the entries
  328. * in the rectangle from the top-left origin of M to its coordinate. In the
  329. * following schema, "i" contains the sum of the whole map:
  330. *
  331. * M = +----------+-----------------+----+
  332. * | | | |
  333. * | | | |
  334. * | a| b| c|
  335. * +----------+-----------------+----+
  336. * | | | |
  337. * | | | |
  338. * | | X | |
  339. * | | | |
  340. * | d| e| f|
  341. * +----------+-----------------+----+
  342. * | | | |
  343. * | g| h| i|
  344. * +----------+-----------------+----+
  345. *
  346. * The sum of the X box can be calculated with:
  347. * X = e-d-b+a
  348. *
  349. * See https://en.wikipedia.org/wiki/Summed_area_table
  350. *
  351. * The compute*_ssd functions compute the integral image M where every entry
  352. * contains the sum of the squared difference of every corresponding pixels of
  353. * two input planes of the same size as M.
  354. */
  355. const uint32_t a = ii[x];
  356. const uint32_t b = ii[x + dist_b];
  357. const uint32_t d = ii[x + dist_d];
  358. const uint32_t e = ii[x + dist_e];
  359. const uint32_t patch_diff_sq = e - d - b + a;
  360. if (patch_diff_sq < s->max_meaningful_diff) {
  361. const float weight = s->weight_lut[patch_diff_sq]; // exp(-patch_diff_sq * s->pdiff_scale)
  362. wa[x].total_weight += weight;
  363. wa[x].sum += weight * src[x];
  364. }
  365. }
  366. ii += s->ii_lz_32;
  367. }
  368. return 0;
  369. }
  370. static void weight_averages(uint8_t *dst, ptrdiff_t dst_linesize,
  371. const uint8_t *src, ptrdiff_t src_linesize,
  372. struct weighted_avg *wa, ptrdiff_t wa_linesize,
  373. int w, int h)
  374. {
  375. int x, y;
  376. for (y = 0; y < h; y++) {
  377. for (x = 0; x < w; x++) {
  378. // Also weight the centered pixel
  379. wa[x].total_weight += 1.f;
  380. wa[x].sum += 1.f * src[x];
  381. dst[x] = av_clip_uint8(wa[x].sum / wa[x].total_weight);
  382. }
  383. dst += dst_linesize;
  384. src += src_linesize;
  385. wa += wa_linesize;
  386. }
  387. }
  388. static int nlmeans_plane(AVFilterContext *ctx, int w, int h, int p, int r,
  389. uint8_t *dst, ptrdiff_t dst_linesize,
  390. const uint8_t *src, ptrdiff_t src_linesize)
  391. {
  392. int offx, offy;
  393. NLMeansContext *s = ctx->priv;
  394. /* patches center points cover the whole research window so the patches
  395. * themselves overflow the research window */
  396. const int e = r + p;
  397. /* focus an integral pointer on the centered image (s1) */
  398. const uint32_t *centered_ii = s->ii + e*s->ii_lz_32 + e;
  399. memset(s->wa, 0, s->wa_linesize * h * sizeof(*s->wa));
  400. for (offy = -r; offy <= r; offy++) {
  401. for (offx = -r; offx <= r; offx++) {
  402. if (offx || offy) {
  403. struct thread_data td = {
  404. .src = src + offy*src_linesize + offx,
  405. .src_linesize = src_linesize,
  406. .startx = FFMAX(0, -offx),
  407. .starty = FFMAX(0, -offy),
  408. .endx = FFMIN(w, w - offx),
  409. .endy = FFMIN(h, h - offy),
  410. .ii_start = centered_ii + offy*s->ii_lz_32 + offx,
  411. .p = p,
  412. };
  413. compute_ssd_integral_image(&s->dsp, s->ii, s->ii_lz_32,
  414. src, src_linesize,
  415. offx, offy, e, w, h);
  416. ctx->internal->execute(ctx, nlmeans_slice, &td, NULL,
  417. FFMIN(td.endy - td.starty, ff_filter_get_nb_threads(ctx)));
  418. }
  419. }
  420. }
  421. weight_averages(dst, dst_linesize, src, src_linesize,
  422. s->wa, s->wa_linesize, w, h);
  423. return 0;
  424. }
  425. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  426. {
  427. int i;
  428. AVFilterContext *ctx = inlink->dst;
  429. NLMeansContext *s = ctx->priv;
  430. AVFilterLink *outlink = ctx->outputs[0];
  431. AVFrame *out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  432. if (!out) {
  433. av_frame_free(&in);
  434. return AVERROR(ENOMEM);
  435. }
  436. av_frame_copy_props(out, in);
  437. for (i = 0; i < s->nb_planes; i++) {
  438. const int w = i ? s->chroma_w : inlink->w;
  439. const int h = i ? s->chroma_h : inlink->h;
  440. const int p = i ? s->patch_hsize_uv : s->patch_hsize;
  441. const int r = i ? s->research_hsize_uv : s->research_hsize;
  442. nlmeans_plane(ctx, w, h, p, r,
  443. out->data[i], out->linesize[i],
  444. in->data[i], in->linesize[i]);
  445. }
  446. av_frame_free(&in);
  447. return ff_filter_frame(outlink, out);
  448. }
  449. #define CHECK_ODD_FIELD(field, name) do { \
  450. if (!(s->field & 1)) { \
  451. s->field |= 1; \
  452. av_log(ctx, AV_LOG_WARNING, name " size must be odd, " \
  453. "setting it to %d\n", s->field); \
  454. } \
  455. } while (0)
  456. void ff_nlmeans_init(NLMeansDSPContext *dsp)
  457. {
  458. dsp->compute_safe_ssd_integral_image = compute_safe_ssd_integral_image_c;
  459. if (ARCH_AARCH64)
  460. ff_nlmeans_init_aarch64(dsp);
  461. }
  462. static av_cold int init(AVFilterContext *ctx)
  463. {
  464. int i;
  465. NLMeansContext *s = ctx->priv;
  466. const double h = s->sigma * 10.;
  467. s->pdiff_scale = 1. / (h * h);
  468. s->max_meaningful_diff = log(255.) / s->pdiff_scale;
  469. s->weight_lut = av_calloc(s->max_meaningful_diff, sizeof(*s->weight_lut));
  470. if (!s->weight_lut)
  471. return AVERROR(ENOMEM);
  472. for (i = 0; i < s->max_meaningful_diff; i++)
  473. s->weight_lut[i] = exp(-i * s->pdiff_scale);
  474. CHECK_ODD_FIELD(research_size, "Luma research window");
  475. CHECK_ODD_FIELD(patch_size, "Luma patch");
  476. if (!s->research_size_uv) s->research_size_uv = s->research_size;
  477. if (!s->patch_size_uv) s->patch_size_uv = s->patch_size;
  478. CHECK_ODD_FIELD(research_size_uv, "Chroma research window");
  479. CHECK_ODD_FIELD(patch_size_uv, "Chroma patch");
  480. s->research_hsize = s->research_size / 2;
  481. s->research_hsize_uv = s->research_size_uv / 2;
  482. s->patch_hsize = s->patch_size / 2;
  483. s->patch_hsize_uv = s->patch_size_uv / 2;
  484. av_log(ctx, AV_LOG_INFO, "Research window: %dx%d / %dx%d, patch size: %dx%d / %dx%d\n",
  485. s->research_size, s->research_size, s->research_size_uv, s->research_size_uv,
  486. s->patch_size, s->patch_size, s->patch_size_uv, s->patch_size_uv);
  487. ff_nlmeans_init(&s->dsp);
  488. return 0;
  489. }
  490. static av_cold void uninit(AVFilterContext *ctx)
  491. {
  492. NLMeansContext *s = ctx->priv;
  493. av_freep(&s->weight_lut);
  494. av_freep(&s->ii_orig);
  495. av_freep(&s->wa);
  496. }
  497. static const AVFilterPad nlmeans_inputs[] = {
  498. {
  499. .name = "default",
  500. .type = AVMEDIA_TYPE_VIDEO,
  501. .config_props = config_input,
  502. .filter_frame = filter_frame,
  503. },
  504. { NULL }
  505. };
  506. static const AVFilterPad nlmeans_outputs[] = {
  507. {
  508. .name = "default",
  509. .type = AVMEDIA_TYPE_VIDEO,
  510. },
  511. { NULL }
  512. };
  513. AVFilter ff_vf_nlmeans = {
  514. .name = "nlmeans",
  515. .description = NULL_IF_CONFIG_SMALL("Non-local means denoiser."),
  516. .priv_size = sizeof(NLMeansContext),
  517. .init = init,
  518. .uninit = uninit,
  519. .query_formats = query_formats,
  520. .inputs = nlmeans_inputs,
  521. .outputs = nlmeans_outputs,
  522. .priv_class = &nlmeans_class,
  523. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
  524. };