FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
vf_nlmeans.c
Go to the documentation of this file.
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 /**
22  * @todo
23  * - better automatic defaults? see "Parameters" @ http://www.ipol.im/pub/art/2011/bcm_nlm/
24  * - temporal support (probably doesn't need any displacement according to
25  * "Denoising image sequences does not require motion estimation")
26  * - Bayer pixel format support for at least raw photos? (DNG support would be
27  * handy here)
28  * - FATE test (probably needs visual threshold test mechanism due to the use
29  * of floats)
30  */
31 
32 #include "libavutil/avassert.h"
33 #include "libavutil/opt.h"
34 #include "libavutil/pixdesc.h"
35 #include "avfilter.h"
36 #include "formats.h"
37 #include "internal.h"
38 #include "vf_nlmeans.h"
39 #include "video.h"
40 
41 struct weighted_avg {
42  float total_weight;
43  float sum;
44 };
45 
46 #define WEIGHT_LUT_NBITS 9
47 #define WEIGHT_LUT_SIZE (1<<WEIGHT_LUT_NBITS)
48 
49 typedef struct NLMeansContext {
50  const AVClass *class;
51  int nb_planes;
53  double pdiff_scale; // invert of the filtering parameter (sigma*10) squared
54  double sigma; // denoising strength
55  int patch_size, patch_hsize; // patch size and half size
56  int patch_size_uv, patch_hsize_uv; // patch size and half size for chroma planes
57  int research_size, research_hsize; // research size and half size
58  int research_size_uv, research_hsize_uv; // research size and half size for chroma planes
59  uint32_t *ii_orig; // integral image
60  uint32_t *ii; // integral image starting after the 0-line and 0-column
61  int ii_w, ii_h; // width and height of the integral image
62  ptrdiff_t ii_lz_32; // linesize in 32-bit units of the integral image
63  struct weighted_avg *wa; // weighted average of every pixel
64  ptrdiff_t wa_linesize; // linesize for wa in struct size unit
65  float weight_lut[WEIGHT_LUT_SIZE]; // lookup table mapping (scaled) patch differences to their associated weights
66  float pdiff_lut_scale; // scale factor for patch differences before looking into the LUT
67  uint32_t max_meaningful_diff; // maximum difference considered (if the patch difference is too high we ignore the pixel)
70 
71 #define OFFSET(x) offsetof(NLMeansContext, x)
72 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
73 static const AVOption nlmeans_options[] = {
74  { "s", "denoising strength", OFFSET(sigma), AV_OPT_TYPE_DOUBLE, { .dbl = 1.0 }, 1.0, 30.0, FLAGS },
75  { "p", "patch size", OFFSET(patch_size), AV_OPT_TYPE_INT, { .i64 = 3*2+1 }, 0, 99, FLAGS },
76  { "pc", "patch size for chroma planes", OFFSET(patch_size_uv), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 99, FLAGS },
77  { "r", "research window", OFFSET(research_size), AV_OPT_TYPE_INT, { .i64 = 7*2+1 }, 0, 99, FLAGS },
78  { "rc", "research window for chroma planes", OFFSET(research_size_uv), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 99, FLAGS },
79  { NULL }
80 };
81 
82 AVFILTER_DEFINE_CLASS(nlmeans);
83 
85 {
86  static const enum AVPixelFormat pix_fmts[] = {
95  };
96 
97  AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
98  if (!fmts_list)
99  return AVERROR(ENOMEM);
100  return ff_set_common_formats(ctx, fmts_list);
101 }
102 
103 /**
104  * Compute squared difference of the safe area (the zone where s1 and s2
105  * overlap). It is likely the largest integral zone, so it is interesting to do
106  * as little checks as possible; contrary to the unsafe version of this
107  * function, we do not need any clipping here.
108  *
109  * The line above dst and the column to its left are always readable.
110  */
111 static void compute_safe_ssd_integral_image_c(uint32_t *dst, ptrdiff_t dst_linesize_32,
112  const uint8_t *s1, ptrdiff_t linesize1,
113  const uint8_t *s2, ptrdiff_t linesize2,
114  int w, int h)
115 {
116  int x, y;
117  const uint32_t *dst_top = dst - dst_linesize_32;
118 
119  /* SIMD-friendly assumptions allowed here */
120  av_assert2(!(w & 0xf) && w >= 16 && h >= 1);
121 
122  for (y = 0; y < h; y++) {
123  for (x = 0; x < w; x += 4) {
124  const int d0 = s1[x ] - s2[x ];
125  const int d1 = s1[x + 1] - s2[x + 1];
126  const int d2 = s1[x + 2] - s2[x + 2];
127  const int d3 = s1[x + 3] - s2[x + 3];
128 
129  dst[x ] = dst_top[x ] - dst_top[x - 1] + d0*d0;
130  dst[x + 1] = dst_top[x + 1] - dst_top[x ] + d1*d1;
131  dst[x + 2] = dst_top[x + 2] - dst_top[x + 1] + d2*d2;
132  dst[x + 3] = dst_top[x + 3] - dst_top[x + 2] + d3*d3;
133 
134  dst[x ] += dst[x - 1];
135  dst[x + 1] += dst[x ];
136  dst[x + 2] += dst[x + 1];
137  dst[x + 3] += dst[x + 2];
138  }
139  s1 += linesize1;
140  s2 += linesize2;
141  dst += dst_linesize_32;
142  dst_top += dst_linesize_32;
143  }
144 }
145 
146 /**
147  * Compute squared difference of an unsafe area (the zone nor s1 nor s2 could
148  * be readable).
149  *
150  * On the other hand, the line above dst and the column to its left are always
151  * readable.
152  *
153  * There is little point in having this function SIMDified as it is likely too
154  * complex and only handle small portions of the image.
155  *
156  * @param dst integral image
157  * @param dst_linesize_32 integral image linesize (in 32-bit integers unit)
158  * @param startx integral starting x position
159  * @param starty integral starting y position
160  * @param src source plane buffer
161  * @param linesize source plane linesize
162  * @param offx source offsetting in x
163  * @param offy source offsetting in y
164  * @paran r absolute maximum source offsetting
165  * @param sw source width
166  * @param sh source height
167  * @param w width to compute
168  * @param h height to compute
169  */
170 static inline void compute_unsafe_ssd_integral_image(uint32_t *dst, ptrdiff_t dst_linesize_32,
171  int startx, int starty,
172  const uint8_t *src, ptrdiff_t linesize,
173  int offx, int offy, int r, int sw, int sh,
174  int w, int h)
175 {
176  int x, y;
177 
178  for (y = starty; y < starty + h; y++) {
179  uint32_t acc = dst[y*dst_linesize_32 + startx - 1] - dst[(y-1)*dst_linesize_32 + startx - 1];
180  const int s1y = av_clip(y - r, 0, sh - 1);
181  const int s2y = av_clip(y - (r + offy), 0, sh - 1);
182 
183  for (x = startx; x < startx + w; x++) {
184  const int s1x = av_clip(x - r, 0, sw - 1);
185  const int s2x = av_clip(x - (r + offx), 0, sw - 1);
186  const uint8_t v1 = src[s1y*linesize + s1x];
187  const uint8_t v2 = src[s2y*linesize + s2x];
188  const int d = v1 - v2;
189  acc += d * d;
190  dst[y*dst_linesize_32 + x] = dst[(y-1)*dst_linesize_32 + x] + acc;
191  }
192  }
193 }
194 
195 /*
196  * Compute the sum of squared difference integral image
197  * http://www.ipol.im/pub/art/2014/57/
198  * Integral Images for Block Matching - Gabriele Facciolo, Nicolas Limare, Enric Meinhardt-Llopis
199  *
200  * @param ii integral image of dimension (w+e*2) x (h+e*2) with
201  * an additional zeroed top line and column already
202  * "applied" to the pointer value
203  * @param ii_linesize_32 integral image linesize (in 32-bit integers unit)
204  * @param src source plane buffer
205  * @param linesize source plane linesize
206  * @param offx x-offsetting ranging in [-e;e]
207  * @param offy y-offsetting ranging in [-e;e]
208  * @param w source width
209  * @param h source height
210  * @param e research padding edge
211  */
213  uint32_t *ii, ptrdiff_t ii_linesize_32,
214  const uint8_t *src, ptrdiff_t linesize, int offx, int offy,
215  int e, int w, int h)
216 {
217  // ii has a surrounding padding of thickness "e"
218  const int ii_w = w + e*2;
219  const int ii_h = h + e*2;
220 
221  // we center the first source
222  const int s1x = e;
223  const int s1y = e;
224 
225  // 2nd source is the frame with offsetting
226  const int s2x = e + offx;
227  const int s2y = e + offy;
228 
229  // get the dimension of the overlapping rectangle where it is always safe
230  // to compare the 2 sources pixels
231  const int startx_safe = FFMAX(s1x, s2x);
232  const int starty_safe = FFMAX(s1y, s2y);
233  const int u_endx_safe = FFMIN(s1x + w, s2x + w); // unaligned
234  const int endy_safe = FFMIN(s1y + h, s2y + h);
235 
236  // deduce the safe area width and height
237  const int safe_pw = (u_endx_safe - startx_safe) & ~0xf;
238  const int safe_ph = endy_safe - starty_safe;
239 
240  // adjusted end x position of the safe area after width of the safe area gets aligned
241  const int endx_safe = startx_safe + safe_pw;
242 
243  // top part where only one of s1 and s2 is still readable, or none at all
244  compute_unsafe_ssd_integral_image(ii, ii_linesize_32,
245  0, 0,
246  src, linesize,
247  offx, offy, e, w, h,
248  ii_w, starty_safe);
249 
250  // fill the left column integral required to compute the central
251  // overlapping one
252  compute_unsafe_ssd_integral_image(ii, ii_linesize_32,
253  0, starty_safe,
254  src, linesize,
255  offx, offy, e, w, h,
256  startx_safe, safe_ph);
257 
258  // main and safe part of the integral
259  av_assert1(startx_safe - s1x >= 0); av_assert1(startx_safe - s1x < w);
260  av_assert1(starty_safe - s1y >= 0); av_assert1(starty_safe - s1y < h);
261  av_assert1(startx_safe - s2x >= 0); av_assert1(startx_safe - s2x < w);
262  av_assert1(starty_safe - s2y >= 0); av_assert1(starty_safe - s2y < h);
263  if (safe_pw && safe_ph)
264  dsp->compute_safe_ssd_integral_image(ii + starty_safe*ii_linesize_32 + startx_safe, ii_linesize_32,
265  src + (starty_safe - s1y) * linesize + (startx_safe - s1x), linesize,
266  src + (starty_safe - s2y) * linesize + (startx_safe - s2x), linesize,
267  safe_pw, safe_ph);
268 
269  // right part of the integral
270  compute_unsafe_ssd_integral_image(ii, ii_linesize_32,
271  endx_safe, starty_safe,
272  src, linesize,
273  offx, offy, e, w, h,
274  ii_w - endx_safe, safe_ph);
275 
276  // bottom part where only one of s1 and s2 is still readable, or none at all
277  compute_unsafe_ssd_integral_image(ii, ii_linesize_32,
278  0, endy_safe,
279  src, linesize,
280  offx, offy, e, w, h,
281  ii_w, ii_h - endy_safe);
282 }
283 
284 static int config_input(AVFilterLink *inlink)
285 {
286  AVFilterContext *ctx = inlink->dst;
287  NLMeansContext *s = ctx->priv;
289  const int e = FFMAX(s->research_hsize, s->research_hsize_uv)
290  + FFMAX(s->patch_hsize, s->patch_hsize_uv);
291 
292  s->chroma_w = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w);
293  s->chroma_h = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
295 
296  /* Allocate the integral image with extra edges of thickness "e"
297  *
298  * +_+-------------------------------+
299  * |0|0000000000000000000000000000000|
300  * +-x-------------------------------+
301  * |0|\ ^ |
302  * |0| ii | e |
303  * |0| v |
304  * |0| +-----------------------+ |
305  * |0| | | |
306  * |0|<->| | |
307  * |0| e | | |
308  * |0| | | |
309  * |0| +-----------------------+ |
310  * |0| |
311  * |0| |
312  * |0| |
313  * +-+-------------------------------+
314  */
315  s->ii_w = inlink->w + e*2;
316  s->ii_h = inlink->h + e*2;
317 
318  // align to 4 the linesize, "+1" is for the space of the left 0-column
319  s->ii_lz_32 = FFALIGN(s->ii_w + 1, 4);
320 
321  // "+1" is for the space of the top 0-line
322  s->ii_orig = av_mallocz_array(s->ii_h + 1, s->ii_lz_32 * sizeof(*s->ii_orig));
323  if (!s->ii_orig)
324  return AVERROR(ENOMEM);
325 
326  // skip top 0-line and left 0-column
327  s->ii = s->ii_orig + s->ii_lz_32 + 1;
328 
329  // allocate weighted average for every pixel
330  s->wa_linesize = inlink->w;
331  s->wa = av_malloc_array(s->wa_linesize, inlink->h * sizeof(*s->wa));
332  if (!s->wa)
333  return AVERROR(ENOMEM);
334 
335  return 0;
336 }
337 
338 struct thread_data {
339  const uint8_t *src;
340  ptrdiff_t src_linesize;
342  int endx, endy;
343  const uint32_t *ii_start;
344  int p;
345 };
346 
347 static int nlmeans_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
348 {
349  int x, y;
350  NLMeansContext *s = ctx->priv;
351  const struct thread_data *td = arg;
352  const ptrdiff_t src_linesize = td->src_linesize;
353  const int process_h = td->endy - td->starty;
354  const int slice_start = (process_h * jobnr ) / nb_jobs;
355  const int slice_end = (process_h * (jobnr+1)) / nb_jobs;
356  const int starty = td->starty + slice_start;
357  const int endy = td->starty + slice_end;
358  const int p = td->p;
359  const uint32_t *ii = td->ii_start + (starty - p - 1) * s->ii_lz_32 - p - 1;
360  const int dist_b = 2*p + 1;
361  const int dist_d = dist_b * s->ii_lz_32;
362  const int dist_e = dist_d + dist_b;
363 
364  for (y = starty; y < endy; y++) {
365  const uint8_t *src = td->src + y*src_linesize;
366  struct weighted_avg *wa = s->wa + y*s->wa_linesize;
367  for (x = td->startx; x < td->endx; x++) {
368  /*
369  * M is a discrete map where every entry contains the sum of all the entries
370  * in the rectangle from the top-left origin of M to its coordinate. In the
371  * following schema, "i" contains the sum of the whole map:
372  *
373  * M = +----------+-----------------+----+
374  * | | | |
375  * | | | |
376  * | a| b| c|
377  * +----------+-----------------+----+
378  * | | | |
379  * | | | |
380  * | | X | |
381  * | | | |
382  * | d| e| f|
383  * +----------+-----------------+----+
384  * | | | |
385  * | g| h| i|
386  * +----------+-----------------+----+
387  *
388  * The sum of the X box can be calculated with:
389  * X = e-d-b+a
390  *
391  * See https://en.wikipedia.org/wiki/Summed_area_table
392  *
393  * The compute*_ssd functions compute the integral image M where every entry
394  * contains the sum of the squared difference of every corresponding pixels of
395  * two input planes of the same size as M.
396  */
397  const uint32_t a = ii[x];
398  const uint32_t b = ii[x + dist_b];
399  const uint32_t d = ii[x + dist_d];
400  const uint32_t e = ii[x + dist_e];
401  const uint32_t patch_diff_sq = e - d - b + a;
402 
403  if (patch_diff_sq < s->max_meaningful_diff) {
404  const unsigned weight_lut_idx = patch_diff_sq * s->pdiff_lut_scale;
405  const float weight = s->weight_lut[weight_lut_idx]; // exp(-patch_diff_sq * s->pdiff_scale)
406  wa[x].total_weight += weight;
407  wa[x].sum += weight * src[x];
408  }
409  }
410  ii += s->ii_lz_32;
411  }
412  return 0;
413 }
414 
415 static void weight_averages(uint8_t *dst, ptrdiff_t dst_linesize,
416  const uint8_t *src, ptrdiff_t src_linesize,
417  struct weighted_avg *wa, ptrdiff_t wa_linesize,
418  int w, int h)
419 {
420  int x, y;
421 
422  for (y = 0; y < h; y++) {
423  for (x = 0; x < w; x++) {
424  // Also weight the centered pixel
425  wa[x].total_weight += 1.f;
426  wa[x].sum += 1.f * src[x];
427  dst[x] = av_clip_uint8(wa[x].sum / wa[x].total_weight);
428  }
429  dst += dst_linesize;
430  src += src_linesize;
431  wa += wa_linesize;
432  }
433 }
434 
435 static int nlmeans_plane(AVFilterContext *ctx, int w, int h, int p, int r,
436  uint8_t *dst, ptrdiff_t dst_linesize,
437  const uint8_t *src, ptrdiff_t src_linesize)
438 {
439  int offx, offy;
440  NLMeansContext *s = ctx->priv;
441  /* patches center points cover the whole research window so the patches
442  * themselves overflow the research window */
443  const int e = r + p;
444  /* focus an integral pointer on the centered image (s1) */
445  const uint32_t *centered_ii = s->ii + e*s->ii_lz_32 + e;
446 
447  memset(s->wa, 0, s->wa_linesize * h * sizeof(*s->wa));
448 
449  for (offy = -r; offy <= r; offy++) {
450  for (offx = -r; offx <= r; offx++) {
451  if (offx || offy) {
452  struct thread_data td = {
453  .src = src + offy*src_linesize + offx,
454  .src_linesize = src_linesize,
455  .startx = FFMAX(0, -offx),
456  .starty = FFMAX(0, -offy),
457  .endx = FFMIN(w, w - offx),
458  .endy = FFMIN(h, h - offy),
459  .ii_start = centered_ii + offy*s->ii_lz_32 + offx,
460  .p = p,
461  };
462 
464  src, src_linesize,
465  offx, offy, e, w, h);
466  ctx->internal->execute(ctx, nlmeans_slice, &td, NULL,
467  FFMIN(td.endy - td.starty, ff_filter_get_nb_threads(ctx)));
468  }
469  }
470  }
471 
472  weight_averages(dst, dst_linesize, src, src_linesize,
473  s->wa, s->wa_linesize, w, h);
474 
475  return 0;
476 }
477 
478 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
479 {
480  int i;
481  AVFilterContext *ctx = inlink->dst;
482  NLMeansContext *s = ctx->priv;
483  AVFilterLink *outlink = ctx->outputs[0];
484 
485  AVFrame *out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
486  if (!out) {
487  av_frame_free(&in);
488  return AVERROR(ENOMEM);
489  }
490  av_frame_copy_props(out, in);
491 
492  for (i = 0; i < s->nb_planes; i++) {
493  const int w = i ? s->chroma_w : inlink->w;
494  const int h = i ? s->chroma_h : inlink->h;
495  const int p = i ? s->patch_hsize_uv : s->patch_hsize;
496  const int r = i ? s->research_hsize_uv : s->research_hsize;
497  nlmeans_plane(ctx, w, h, p, r,
498  out->data[i], out->linesize[i],
499  in->data[i], in->linesize[i]);
500  }
501 
502  av_frame_free(&in);
503  return ff_filter_frame(outlink, out);
504 }
505 
506 #define CHECK_ODD_FIELD(field, name) do { \
507  if (!(s->field & 1)) { \
508  s->field |= 1; \
509  av_log(ctx, AV_LOG_WARNING, name " size must be odd, " \
510  "setting it to %d\n", s->field); \
511  } \
512 } while (0)
513 
515 {
517 
518  if (ARCH_AARCH64)
520 }
521 
523 {
524  int i;
525  NLMeansContext *s = ctx->priv;
526  const double h = s->sigma * 10.;
527 
528  s->pdiff_scale = 1. / (h * h);
529  s->max_meaningful_diff = -log(1/255.) / s->pdiff_scale;
532  for (i = 0; i < WEIGHT_LUT_SIZE; i++)
533  s->weight_lut[i] = exp(-i / s->pdiff_lut_scale * s->pdiff_scale);
534 
535  CHECK_ODD_FIELD(research_size, "Luma research window");
536  CHECK_ODD_FIELD(patch_size, "Luma patch");
537 
539  if (!s->patch_size_uv) s->patch_size_uv = s->patch_size;
540 
541  CHECK_ODD_FIELD(research_size_uv, "Chroma research window");
542  CHECK_ODD_FIELD(patch_size_uv, "Chroma patch");
543 
544  s->research_hsize = s->research_size / 2;
546  s->patch_hsize = s->patch_size / 2;
547  s->patch_hsize_uv = s->patch_size_uv / 2;
548 
549  av_log(ctx, AV_LOG_INFO, "Research window: %dx%d / %dx%d, patch size: %dx%d / %dx%d\n",
552 
553  ff_nlmeans_init(&s->dsp);
554 
555  return 0;
556 }
557 
559 {
560  NLMeansContext *s = ctx->priv;
561  av_freep(&s->ii_orig);
562  av_freep(&s->wa);
563 }
564 
565 static const AVFilterPad nlmeans_inputs[] = {
566  {
567  .name = "default",
568  .type = AVMEDIA_TYPE_VIDEO,
569  .config_props = config_input,
570  .filter_frame = filter_frame,
571  },
572  { NULL }
573 };
574 
575 static const AVFilterPad nlmeans_outputs[] = {
576  {
577  .name = "default",
578  .type = AVMEDIA_TYPE_VIDEO,
579  },
580  { NULL }
581 };
582 
584  .name = "nlmeans",
585  .description = NULL_IF_CONFIG_SMALL("Non-local means denoiser."),
586  .priv_size = sizeof(NLMeansContext),
587  .init = init,
588  .uninit = uninit,
590  .inputs = nlmeans_inputs,
591  .outputs = nlmeans_outputs,
592  .priv_class = &nlmeans_class,
594 };
#define NULL
Definition: coverity.c:32
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2446
This structure describes decoded (raw) audio or video data.
Definition: frame.h:226
AVOption.
Definition: opt.h:246
int research_hsize_uv
Definition: vf_nlmeans.c:58
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:71
int av_pix_fmt_count_planes(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2486
Main libavfilter public API header.
static const AVFilterPad nlmeans_outputs[]
Definition: vf_nlmeans.c:575
void(* compute_safe_ssd_integral_image)(uint32_t *dst, ptrdiff_t dst_linesize_32, const uint8_t *s1, ptrdiff_t linesize1, const uint8_t *s2, ptrdiff_t linesize2, int w, int h)
Definition: vf_nlmeans.h:26
const char * desc
Definition: nvenc.c:65
static void compute_ssd_integral_image(const NLMeansDSPContext *dsp, uint32_t *ii, ptrdiff_t ii_linesize_32, const uint8_t *src, ptrdiff_t linesize, int offx, int offy, int e, int w, int h)
Definition: vf_nlmeans.c:212
int acc
Definition: yuv2rgb.c:554
#define OFFSET(x)
Definition: vf_nlmeans.c:71
planar GBR 4:4:4 24bpp
Definition: pixfmt.h:168
const char * b
Definition: vf_curves.c:116
#define CHECK_ODD_FIELD(field, name)
Definition: vf_nlmeans.c:506
ptrdiff_t ii_lz_32
Definition: vf_nlmeans.c:62
ptrdiff_t wa_linesize
Definition: vf_nlmeans.c:64
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition: video.c:99
#define src
Definition: vp8dsp.c:254
uint8_t log2_chroma_w
Amount to shift the luma width right to find the chroma width.
Definition: pixdesc.h:92
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:283
#define AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC
Some filters support a generic "enable" expression option that can be used to enable or disable a fil...
Definition: avfilter.h:125
const char * name
Pad name.
Definition: internal.h:60
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1080
uint8_t
#define av_cold
Definition: attributes.h:82
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
Definition: avassert.h:64
AVOptions.
static void compute_safe_ssd_integral_image_c(uint32_t *dst, ptrdiff_t dst_linesize_32, const uint8_t *s1, ptrdiff_t linesize1, const uint8_t *s2, ptrdiff_t linesize2, int w, int h)
Compute squared difference of the safe area (the zone where s1 and s2 overlap).
Definition: vf_nlmeans.c:111
const uint8_t * src
Definition: vf_nlmeans.c:339
static int query_formats(AVFilterContext *ctx)
Definition: vf_nlmeans.c:84
planar YUV 4:4:0 full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV440P and setting color_range...
Definition: pixfmt.h:100
planar YUV 4:2:2, 16bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV422P and setting col...
Definition: pixfmt.h:79
#define FFALIGN(x, a)
Definition: macros.h:48
#define av_log(a,...)
int research_hsize
Definition: vf_nlmeans.c:57
A filter pad used for either input or output.
Definition: internal.h:54
float pdiff_lut_scale
Definition: vf_nlmeans.c:66
static void weight_averages(uint8_t *dst, ptrdiff_t dst_linesize, const uint8_t *src, ptrdiff_t src_linesize, struct weighted_avg *wa, ptrdiff_t wa_linesize, int w, int h)
Definition: vf_nlmeans.c:415
int ff_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
A helper for query_formats() which sets all links to the same list of formats.
Definition: formats.c:568
#define td
Definition: regdef.h:70
uint8_t log2_chroma_h
Amount to shift the luma height right to find the chroma height.
Definition: pixdesc.h:101
#define s2
Definition: regdef.h:39
#define AVERROR(e)
Definition: error.h:43
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:202
double pdiff_scale
Definition: vf_nlmeans.c:53
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:186
uint32_t max_meaningful_diff
Definition: vf_nlmeans.c:67
const char * r
Definition: vf_curves.c:114
void * priv
private data for use by the filter
Definition: avfilter.h:353
#define AVFILTER_FLAG_SLICE_THREADS
The filter supports multithreading by splitting frames into multiple parts and processing them concur...
Definition: avfilter.h:116
const char * arg
Definition: jacosubdec.c:66
static int nlmeans_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
Definition: vf_nlmeans.c:347
simple assert() macros that are a bit more flexible than ISO C assert().
static const AVFilterPad nlmeans_inputs[]
Definition: vf_nlmeans.c:565
#define FFMAX(a, b)
Definition: common.h:94
int8_t exp
Definition: eval.c:72
float weight_lut[WEIGHT_LUT_SIZE]
Definition: vf_nlmeans.c:65
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:70
static void compute_unsafe_ssd_integral_image(uint32_t *dst, ptrdiff_t dst_linesize_32, int startx, int starty, const uint8_t *src, ptrdiff_t linesize, int offx, int offy, int r, int sw, int sh, int w, int h)
Compute squared difference of an unsafe area (the zone nor s1 nor s2 could be readable).
Definition: vf_nlmeans.c:170
av_cold void ff_nlmeans_init_aarch64(NLMeansDSPContext *dsp)
int ff_filter_get_nb_threads(AVFilterContext *ctx)
Get number of threads for current filter instance.
Definition: avfilter.c:802
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition: avassert.h:53
#define FFMIN(a, b)
Definition: common.h:96
planar YUV 4:2:0, 12bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV420P and setting col...
Definition: pixfmt.h:78
NLMeansDSPContext dsp
Definition: vf_nlmeans.c:68
uint8_t w
Definition: llviddspenc.c:38
AVFormatContext * ctx
Definition: movenc.c:48
static int config_input(AVFilterLink *inlink)
Definition: vf_nlmeans.c:284
#define s(width, name)
Definition: cbs_vp9.c:257
static const AVFilterPad inputs[]
Definition: af_acontrast.c:193
#define FLAGS
Definition: vf_nlmeans.c:72
static const AVFilterPad outputs[]
Definition: af_acontrast.c:203
#define FF_ARRAY_ELEMS(a)
static av_cold int init(AVFilterContext *ctx)
Definition: vf_nlmeans.c:522
#define AV_LOG_INFO
Standard information.
Definition: log.h:187
static av_cold void uninit(AVFilterContext *ctx)
Definition: vf_nlmeans.c:558
int research_size_uv
Definition: vf_nlmeans.c:58
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:257
struct weighted_avg * wa
Definition: vf_nlmeans.c:63
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:81
ptrdiff_t src_linesize
Definition: vf_nlmeans.c:340
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31))))#defineSET_CONV_FUNC_GROUP(ofmt, ifmt) staticvoidset_generic_function(AudioConvert *ac){}voidff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, intsample_rate, intapply_map){AudioConvert *ac;intin_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) returnNULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt)>2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);returnNULL;}returnac;}in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}elseif(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;elseac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);returnac;}intff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){intuse_generic=1;intlen=in->nb_samples;intp;if(ac->dc){av_log(ac->avr, AV_LOG_TRACE,"%dsamples-audio_convert:%sto%s(dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));returnff_convert_dither(ac-> in
planar YUV 4:1:0, 9bpp, (1 Cr & Cb sample per 4x4 Y samples)
Definition: pixfmt.h:72
Describe the class of an AVClass context structure.
Definition: log.h:67
Filter definition.
Definition: avfilter.h:144
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
Definition: vf_nlmeans.c:478
const char * name
Filter name.
Definition: avfilter.h:148
#define s1
Definition: regdef.h:38
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:350
static int weight(int i, int blen, int offset)
Definition: diracdec.c:1547
AVFilter ff_vf_nlmeans
Definition: vf_nlmeans.c:583
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:266
#define flags(name, subs,...)
Definition: cbs_av1.c:596
AVFilterInternal * internal
An opaque struct for libavfilter internal use.
Definition: avfilter.h:378
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:240
uint32_t * ii
Definition: vf_nlmeans.c:60
int patch_hsize_uv
Definition: vf_nlmeans.c:56
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:66
Y , 8bpp.
Definition: pixfmt.h:74
planar YUV 4:4:4, 24bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV444P and setting col...
Definition: pixfmt.h:80
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples)
Definition: pixfmt.h:73
avfilter_execute_func * execute
Definition: internal.h:155
uint32_t * ii_orig
Definition: vf_nlmeans.c:59
static int slice_end(AVCodecContext *avctx, AVFrame *pict)
Handle slice ends.
Definition: mpeg12dec.c:2029
static const AVOption nlmeans_options[]
Definition: vf_nlmeans.c:73
#define xf(width, name, var, range_min, range_max, subs,...)
Definition: cbs_av1.c:698
#define WEIGHT_LUT_SIZE
Definition: vf_nlmeans.c:47
A list of supported formats for one end of a filter link.
Definition: formats.h:64
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples) full scale (JPEG), deprecated in favor ...
Definition: pixfmt.h:258
An instance of a filter.
Definition: avfilter.h:338
double sigma
Definition: vf_nlmeans.c:54
float total_weight
Definition: vf_nlmeans.c:42
FILE * out
Definition: movenc.c:54
#define av_freep(p)
planar YUV 4:4:0 (1 Cr & Cb sample per 1x2 Y samples)
Definition: pixfmt.h:99
AVFILTER_DEFINE_CLASS(nlmeans)
#define av_malloc_array(a, b)
internal API functions
void ff_nlmeans_init(NLMeansDSPContext *dsp)
Definition: vf_nlmeans.c:514
static int nlmeans_plane(AVFilterContext *ctx, int w, int h, int p, int r, uint8_t *dst, ptrdiff_t dst_linesize, const uint8_t *src, ptrdiff_t src_linesize)
Definition: vf_nlmeans.c:435
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
for(j=16;j >0;--j)
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition: frame.c:654
const uint32_t * ii_start
Definition: vf_nlmeans.c:343
void * av_mallocz_array(size_t nmemb, size_t size)
Definition: mem.c:191
#define AV_CEIL_RSHIFT(a, b)
Definition: common.h:58