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  * - SIMD for compute_safe_ssd_integral_image
24  * - SIMD for final weighted averaging
25  * - better automatic defaults? see "Parameters" @ http://www.ipol.im/pub/art/2011/bcm_nlm/
26  * - temporal support (probably doesn't need any displacement according to
27  * "Denoising image sequences does not require motion estimation")
28  * - Bayer pixel format support for at least raw photos? (DNG support would be
29  * handy here)
30  * - FATE test (probably needs visual threshold test mechanism due to the use
31  * of floats)
32  */
33 
34 #include "libavutil/avassert.h"
35 #include "libavutil/opt.h"
36 #include "libavutil/pixdesc.h"
37 #include "avfilter.h"
38 #include "formats.h"
39 #include "internal.h"
40 #include "video.h"
41 
42 struct weighted_avg {
43  double total_weight;
44  double sum;
45 };
46 
47 #define WEIGHT_LUT_NBITS 9
48 #define WEIGHT_LUT_SIZE (1<<WEIGHT_LUT_NBITS)
49 
50 typedef struct {
51  const AVClass *class;
52  int nb_planes;
53  int chroma_w, chroma_h;
54  double pdiff_scale; // invert of the filtering parameter (sigma*10) squared
55  double sigma; // denoising strength
56  int patch_size, patch_hsize; // patch size and half size
57  int patch_size_uv, patch_hsize_uv; // patch size and half size for chroma planes
58  int research_size, research_hsize; // research size and half size
59  int research_size_uv, research_hsize_uv; // research size and half size for chroma planes
60  uint32_t *ii_orig; // integral image
61  uint32_t *ii; // integral image starting after the 0-line and 0-column
62  int ii_w, ii_h; // width and height of the integral image
63  int ii_lz_32; // linesize in 32-bit units of the integral image
64  struct weighted_avg *wa; // weighted average of every pixel
65  int wa_linesize; // linesize for wa in struct size unit
66  double weight_lut[WEIGHT_LUT_SIZE]; // lookup table mapping (scaled) patch differences to their associated weights
67  double pdiff_lut_scale; // scale factor for patch differences before looking into the LUT
68  int 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  * M is a discrete map where every entry contains the sum of all the entries
105  * in the rectangle from the top-left origin of M to its coordinate. In the
106  * following schema, "i" contains the sum of the whole map:
107  *
108  * M = +----------+-----------------+----+
109  * | | | |
110  * | | | |
111  * | a| b| c|
112  * +----------+-----------------+----+
113  * | | | |
114  * | | | |
115  * | | X | |
116  * | | | |
117  * | d| e| f|
118  * +----------+-----------------+----+
119  * | | | |
120  * | g| h| i|
121  * +----------+-----------------+----+
122  *
123  * The sum of the X box can be calculated with:
124  * X = e-d-b+a
125  *
126  * See https://en.wikipedia.org/wiki/Summed_area_table
127  *
128  * The compute*_ssd functions compute the integral image M where every entry
129  * contains the sum of the squared difference of every corresponding pixels of
130  * two input planes of the same size as M.
131  */
132 static inline int get_integral_patch_value(const uint32_t *ii, int ii_lz_32, int x, int y, int p)
133 {
134  const int e = ii[(y + p ) * ii_lz_32 + (x + p )];
135  const int d = ii[(y + p ) * ii_lz_32 + (x - p - 1)];
136  const int b = ii[(y - p - 1) * ii_lz_32 + (x + p )];
137  const int a = ii[(y - p - 1) * ii_lz_32 + (x - p - 1)];
138  return e - d - b + a;
139 }
140 
141 /**
142  * Compute squared difference of the safe area (the zone where s1 and s2
143  * overlap). It is likely the largest integral zone, so it is interesting to do
144  * as little checks as possible; contrary to the unsafe version of this
145  * function, we do not need any clipping here.
146  *
147  * The line above dst and the column to its left are always readable.
148  *
149  * This C version computes the SSD integral image using a scalar accumulator,
150  * while for SIMD implementation it is likely more interesting to use the
151  * two-loops algorithm variant.
152  */
153 static void compute_safe_ssd_integral_image_c(uint32_t *dst, int dst_linesize_32,
154  const uint8_t *s1, int linesize1,
155  const uint8_t *s2, int linesize2,
156  int w, int h)
157 {
158  int x, y;
159 
160  for (y = 0; y < h; y++) {
161  uint32_t acc = dst[-1] - dst[-dst_linesize_32 - 1];
162 
163  for (x = 0; x < w; x++) {
164  const int d = s1[x] - s2[x];
165  acc += d * d;
166  dst[x] = dst[-dst_linesize_32 + x] + acc;
167  }
168  s1 += linesize1;
169  s2 += linesize2;
170  dst += dst_linesize_32;
171  }
172 }
173 
174 /**
175  * Compute squared difference of an unsafe area (the zone nor s1 nor s2 could
176  * be readable).
177  *
178  * On the other hand, the line above dst and the column to its left are always
179  * readable.
180  *
181  * There is little point in having this function SIMDified as it is likely too
182  * complex and only handle small portions of the image.
183  *
184  * @param dst integral image
185  * @param dst_linesize_32 integral image linesize (in 32-bit integers unit)
186  * @param startx integral starting x position
187  * @param starty integral starting y position
188  * @param src source plane buffer
189  * @param linesize source plane linesize
190  * @param offx source offsetting in x
191  * @param offy source offsetting in y
192  * @paran r absolute maximum source offsetting
193  * @param sw source width
194  * @param sh source height
195  * @param w width to compute
196  * @param h height to compute
197  */
198 static inline void compute_unsafe_ssd_integral_image(uint32_t *dst, int dst_linesize_32,
199  int startx, int starty,
200  const uint8_t *src, int linesize,
201  int offx, int offy, int r, int sw, int sh,
202  int w, int h)
203 {
204  int x, y;
205 
206  for (y = starty; y < starty + h; y++) {
207  uint32_t acc = dst[y*dst_linesize_32 + startx - 1] - dst[(y-1)*dst_linesize_32 + startx - 1];
208  const int s1y = av_clip(y - r, 0, sh - 1);
209  const int s2y = av_clip(y - (r + offy), 0, sh - 1);
210 
211  for (x = startx; x < startx + w; x++) {
212  const int s1x = av_clip(x - r, 0, sw - 1);
213  const int s2x = av_clip(x - (r + offx), 0, sw - 1);
214  const uint8_t v1 = src[s1y*linesize + s1x];
215  const uint8_t v2 = src[s2y*linesize + s2x];
216  const int d = v1 - v2;
217  acc += d * d;
218  dst[y*dst_linesize_32 + x] = dst[(y-1)*dst_linesize_32 + x] + acc;
219  }
220  }
221 }
222 
223 /*
224  * Compute the sum of squared difference integral image
225  * http://www.ipol.im/pub/art/2014/57/
226  * Integral Images for Block Matching - Gabriele Facciolo, Nicolas Limare, Enric Meinhardt-Llopis
227  *
228  * @param ii integral image of dimension (w+e*2) x (h+e*2) with
229  * an additional zeroed top line and column already
230  * "applied" to the pointer value
231  * @param ii_linesize_32 integral image linesize (in 32-bit integers unit)
232  * @param src source plane buffer
233  * @param linesize source plane linesize
234  * @param offx x-offsetting ranging in [-e;e]
235  * @param offy y-offsetting ranging in [-e;e]
236  * @param w source width
237  * @param h source height
238  * @param e research padding edge
239  */
240 static void compute_ssd_integral_image(uint32_t *ii, int ii_linesize_32,
241  const uint8_t *src, int linesize, int offx, int offy,
242  int e, int w, int h)
243 {
244  // ii has a surrounding padding of thickness "e"
245  const int ii_w = w + e*2;
246  const int ii_h = h + e*2;
247 
248  // we center the first source
249  const int s1x = e;
250  const int s1y = e;
251 
252  // 2nd source is the frame with offsetting
253  const int s2x = e + offx;
254  const int s2y = e + offy;
255 
256  // get the dimension of the overlapping rectangle where it is always safe
257  // to compare the 2 sources pixels
258  const int startx_safe = FFMAX(s1x, s2x);
259  const int starty_safe = FFMAX(s1y, s2y);
260  const int endx_safe = FFMIN(s1x + w, s2x + w);
261  const int endy_safe = FFMIN(s1y + h, s2y + h);
262 
263  // top part where only one of s1 and s2 is still readable, or none at all
264  compute_unsafe_ssd_integral_image(ii, ii_linesize_32,
265  0, 0,
266  src, linesize,
267  offx, offy, e, w, h,
268  ii_w, starty_safe);
269 
270  // fill the left column integral required to compute the central
271  // overlapping one
272  compute_unsafe_ssd_integral_image(ii, ii_linesize_32,
273  0, starty_safe,
274  src, linesize,
275  offx, offy, e, w, h,
276  startx_safe, endy_safe - starty_safe);
277 
278  // main and safe part of the integral
279  av_assert1(startx_safe - s1x >= 0); av_assert1(startx_safe - s1x < w);
280  av_assert1(starty_safe - s1y >= 0); av_assert1(starty_safe - s1y < h);
281  av_assert1(startx_safe - s2x >= 0); av_assert1(startx_safe - s2x < w);
282  av_assert1(starty_safe - s2y >= 0); av_assert1(starty_safe - s2y < h);
283  compute_safe_ssd_integral_image_c(ii + starty_safe*ii_linesize_32 + startx_safe, ii_linesize_32,
284  src + (starty_safe - s1y) * linesize + (startx_safe - s1x), linesize,
285  src + (starty_safe - s2y) * linesize + (startx_safe - s2x), linesize,
286  endx_safe - startx_safe, endy_safe - starty_safe);
287 
288  // right part of the integral
289  compute_unsafe_ssd_integral_image(ii, ii_linesize_32,
290  endx_safe, starty_safe,
291  src, linesize,
292  offx, offy, e, w, h,
293  ii_w - endx_safe, endy_safe - starty_safe);
294 
295  // bottom part where only one of s1 and s2 is still readable, or none at all
296  compute_unsafe_ssd_integral_image(ii, ii_linesize_32,
297  0, endy_safe,
298  src, linesize,
299  offx, offy, e, w, h,
300  ii_w, ii_h - endy_safe);
301 }
302 
303 static int config_input(AVFilterLink *inlink)
304 {
305  AVFilterContext *ctx = inlink->dst;
306  NLMeansContext *s = ctx->priv;
308  const int e = FFMAX(s->research_hsize, s->research_hsize_uv)
309  + FFMAX(s->patch_hsize, s->patch_hsize_uv);
310 
311  s->chroma_w = FF_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w);
312  s->chroma_h = FF_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
314 
315  /* Allocate the integral image with extra edges of thickness "e"
316  *
317  * +_+-------------------------------+
318  * |0|0000000000000000000000000000000|
319  * +-x-------------------------------+
320  * |0|\ ^ |
321  * |0| ii | e |
322  * |0| v |
323  * |0| +-----------------------+ |
324  * |0| | | |
325  * |0|<->| | |
326  * |0| e | | |
327  * |0| | | |
328  * |0| +-----------------------+ |
329  * |0| |
330  * |0| |
331  * |0| |
332  * +-+-------------------------------+
333  */
334  s->ii_w = inlink->w + e*2;
335  s->ii_h = inlink->h + e*2;
336 
337  // align to 4 the linesize, "+1" is for the space of the left 0-column
338  s->ii_lz_32 = FFALIGN(s->ii_w + 1, 4);
339 
340  // "+1" is for the space of the top 0-line
341  s->ii_orig = av_mallocz_array(s->ii_h + 1, s->ii_lz_32 * sizeof(*s->ii_orig));
342  if (!s->ii_orig)
343  return AVERROR(ENOMEM);
344 
345  // skip top 0-line and left 0-column
346  s->ii = s->ii_orig + s->ii_lz_32 + 1;
347 
348  // allocate weighted average for every pixel
349  s->wa_linesize = inlink->w;
350  s->wa = av_malloc_array(s->wa_linesize, inlink->h * sizeof(*s->wa));
351  if (!s->wa)
352  return AVERROR(ENOMEM);
353 
354  return 0;
355 }
356 
357 struct thread_data {
358  const uint8_t *src;
361  int endx, endy;
362  const uint32_t *ii_start;
363  int p;
364 };
365 
366 static int nlmeans_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
367 {
368  int x, y;
369  NLMeansContext *s = ctx->priv;
370  const struct thread_data *td = arg;
371  const uint8_t *src = td->src;
372  const int src_linesize = td->src_linesize;
373  const int process_h = td->endy - td->starty;
374  const int slice_start = (process_h * jobnr ) / nb_jobs;
375  const int slice_end = (process_h * (jobnr+1)) / nb_jobs;
376  const int starty = td->starty + slice_start;
377  const int endy = td->starty + slice_end;
378 
379  for (y = starty; y < endy; y++) {
380  for (x = td->startx; x < td->endx; x++) {
381  const int patch_diff_sq = get_integral_patch_value(td->ii_start, s->ii_lz_32, x, y, td->p);
382  if (patch_diff_sq < s->max_meaningful_diff) {
383  struct weighted_avg *wa = &s->wa[y*s->wa_linesize + x];
384  const int weight_lut_idx = patch_diff_sq * s->pdiff_lut_scale;
385  const double weight = s->weight_lut[weight_lut_idx]; // exp(-patch_diff_sq * s->pdiff_scale)
386  wa->total_weight += weight;
387  wa->sum += weight * src[y*src_linesize + x];
388  }
389  }
390  }
391  return 0;
392 }
393 
394 static int nlmeans_plane(AVFilterContext *ctx, int w, int h, int p, int r,
395  uint8_t *dst, int dst_linesize,
396  const uint8_t *src, int src_linesize)
397 {
398  int x, y;
399  int offx, offy;
400  NLMeansContext *s = ctx->priv;
401  /* patches center points cover the whole research window so the patches
402  * themselves overflow the research window */
403  const int e = r + p;
404  /* focus an integral pointer on the centered image (s1) */
405  const uint32_t *centered_ii = s->ii + e*s->ii_lz_32 + e;
406 
407  memset(s->wa, 0, s->wa_linesize * h * sizeof(*s->wa));
408 
409  for (offy = -r; offy <= r; offy++) {
410  for (offx = -r; offx <= r; offx++) {
411  if (offx || offy) {
412  struct thread_data td = {
413  .src = src + offy*src_linesize + offx,
414  .src_linesize = src_linesize,
415  .startx = FFMAX(0, -offx),
416  .starty = FFMAX(0, -offy),
417  .endx = FFMIN(w, w - offx),
418  .endy = FFMIN(h, h - offy),
419  .ii_start = centered_ii + offy*s->ii_lz_32 + offx,
420  .p = p,
421  };
422 
424  src, src_linesize,
425  offx, offy, e, w, h);
426  ctx->internal->execute(ctx, nlmeans_slice, &td, NULL,
427  FFMIN(td.endy - td.starty, ff_filter_get_nb_threads(ctx)));
428  }
429  }
430  }
431  for (y = 0; y < h; y++) {
432  for (x = 0; x < w; x++) {
433  struct weighted_avg *wa = &s->wa[y*s->wa_linesize + x];
434 
435  // Also weight the centered pixel
436  wa->total_weight += 1.0;
437  wa->sum += 1.0 * src[y*src_linesize + x];
438 
439  dst[y*dst_linesize + x] = av_clip_uint8(wa->sum / wa->total_weight);
440  }
441  }
442  return 0;
443 }
444 
445 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
446 {
447  int i;
448  AVFilterContext *ctx = inlink->dst;
449  NLMeansContext *s = ctx->priv;
450  AVFilterLink *outlink = ctx->outputs[0];
451 
452  AVFrame *out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
453  if (!out) {
454  av_frame_free(&in);
455  return AVERROR(ENOMEM);
456  }
457  av_frame_copy_props(out, in);
458 
459  for (i = 0; i < s->nb_planes; i++) {
460  const int w = i ? s->chroma_w : inlink->w;
461  const int h = i ? s->chroma_h : inlink->h;
462  const int p = i ? s->patch_hsize_uv : s->patch_hsize;
463  const int r = i ? s->research_hsize_uv : s->research_hsize;
464  nlmeans_plane(ctx, w, h, p, r,
465  out->data[i], out->linesize[i],
466  in->data[i], in->linesize[i]);
467  }
468 
469  av_frame_free(&in);
470  return ff_filter_frame(outlink, out);
471 }
472 
473 #define CHECK_ODD_FIELD(field, name) do { \
474  if (!(s->field & 1)) { \
475  s->field |= 1; \
476  av_log(ctx, AV_LOG_WARNING, name " size must be odd, " \
477  "setting it to %d\n", s->field); \
478  } \
479 } while (0)
480 
482 {
483  int i;
484  NLMeansContext *s = ctx->priv;
485  const double h = s->sigma * 10.;
486 
487  s->pdiff_scale = 1. / (h * h);
488  s->max_meaningful_diff = -log(1/255.) / s->pdiff_scale;
491  for (i = 0; i < WEIGHT_LUT_SIZE; i++)
492  s->weight_lut[i] = exp(-i / s->pdiff_lut_scale * s->pdiff_scale);
493 
494  CHECK_ODD_FIELD(research_size, "Luma research window");
495  CHECK_ODD_FIELD(patch_size, "Luma patch");
496 
498  if (!s->patch_size_uv) s->patch_size_uv = s->patch_size;
499 
500  CHECK_ODD_FIELD(research_size_uv, "Chroma research window");
501  CHECK_ODD_FIELD(patch_size_uv, "Chroma patch");
502 
503  s->research_hsize = s->research_size / 2;
505  s->patch_hsize = s->patch_size / 2;
506  s->patch_hsize_uv = s->patch_size_uv / 2;
507 
508  av_log(ctx, AV_LOG_INFO, "Research window: %dx%d / %dx%d, patch size: %dx%d / %dx%d\n",
511 
512  return 0;
513 }
514 
516 {
517  NLMeansContext *s = ctx->priv;
518  av_freep(&s->ii_orig);
519  av_freep(&s->wa);
520 }
521 
522 static const AVFilterPad nlmeans_inputs[] = {
523  {
524  .name = "default",
525  .type = AVMEDIA_TYPE_VIDEO,
526  .config_props = config_input,
527  .filter_frame = filter_frame,
528  },
529  { NULL }
530 };
531 
532 static const AVFilterPad nlmeans_outputs[] = {
533  {
534  .name = "default",
535  .type = AVMEDIA_TYPE_VIDEO,
536  },
537  { NULL }
538 };
539 
541  .name = "nlmeans",
542  .description = NULL_IF_CONFIG_SMALL("Non-local means denoiser."),
543  .priv_size = sizeof(NLMeansContext),
544  .init = init,
545  .uninit = uninit,
547  .inputs = nlmeans_inputs,
548  .outputs = nlmeans_outputs,
549  .priv_class = &nlmeans_class,
551 };
#define NULL
Definition: coverity.c:32
const char * s
Definition: avisynth_c.h:768
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2266
This structure describes decoded (raw) audio or video data.
Definition: frame.h:184
AVOption.
Definition: opt.h:245
int research_hsize_uv
Definition: vf_nlmeans.c:59
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:67
int av_pix_fmt_count_planes(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2306
Main libavfilter public API header.
const char * desc
Definition: nvenc.c:101
AVFILTER_DEFINE_CLASS(nlmeans)
int acc
Definition: yuv2rgb.c:546
planar GBR 4:4:4 24bpp
Definition: pixfmt.h:180
const char * b
Definition: vf_curves.c:113
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition: video.c:76
double total_weight
Definition: vf_nlmeans.c:43
uint8_t log2_chroma_w
Amount to shift the luma width right to find the chroma width.
Definition: pixdesc.h:92
static const AVFilterPad nlmeans_outputs[]
Definition: vf_nlmeans.c:532
static void compute_safe_ssd_integral_image_c(uint32_t *dst, int dst_linesize_32, const uint8_t *s1, int linesize1, const uint8_t *s2, int linesize2, int w, int h)
Compute squared difference of the safe area (the zone where s1 and s2 overlap).
Definition: vf_nlmeans.c:153
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:59
static const AVOption nlmeans_options[]
Definition: vf_nlmeans.c:73
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
static const AVFilterPad nlmeans_inputs[]
Definition: vf_nlmeans.c:522
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1189
uint8_t
#define av_cold
Definition: attributes.h:82
AVOptions.
const uint8_t * src
Definition: vf_nlmeans.c:358
static int config_input(AVFilterLink *inlink)
Definition: vf_nlmeans.c:303
planar YUV 4:4:0 full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV440P and setting color_range...
Definition: pixfmt.h:101
planar YUV 4:2:2, 16bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV422P and setting col...
Definition: pixfmt.h:75
static int query_formats(AVFilterContext *ctx)
Definition: vf_nlmeans.c:84
#define FFALIGN(x, a)
Definition: macros.h:48
#define av_log(a,...)
int research_hsize
Definition: vf_nlmeans.c:58
A filter pad used for either input or output.
Definition: internal.h:53
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:158
double pdiff_scale
Definition: vf_nlmeans.c:54
#define FF_CEIL_RSHIFT
Definition: common.h:61
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:176
const char * r
Definition: vf_curves.c:111
void * priv
private data for use by the filter
Definition: avfilter.h:322
#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
simple assert() macros that are a bit more flexible than ISO C assert().
#define OFFSET(x)
Definition: vf_nlmeans.c:71
#define FFMAX(a, b)
Definition: common.h:94
static void * av_mallocz_array(size_t nmemb, size_t size)
Definition: mem.h:226
int8_t exp
Definition: eval.c:64
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:66
static void compute_ssd_integral_image(uint32_t *ii, int ii_linesize_32, const uint8_t *src, int linesize, int offx, int offy, int e, int w, int h)
Definition: vf_nlmeans.c:240
int ff_filter_get_nb_threads(AVFilterContext *ctx)
Get number of threads for current filter instance.
Definition: avfilter.c:786
#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
static int nlmeans_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
Definition: vf_nlmeans.c:366
planar YUV 4:2:0, 12bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV420P and setting col...
Definition: pixfmt.h:74
AVFilter ff_vf_nlmeans
Definition: vf_nlmeans.c:540
#define CHECK_ODD_FIELD(field, name)
Definition: vf_nlmeans.c:473
AVFormatContext * ctx
Definition: movenc.c:48
int src_linesize
Definition: vf_nlmeans.c:359
#define FLAGS
Definition: vf_nlmeans.c:72
double weight_lut[WEIGHT_LUT_SIZE]
Definition: vf_nlmeans.c:66
static const AVFilterPad outputs[]
Definition: af_afftfilt.c:386
#define src
Definition: vp9dsp.c:530
#define FF_ARRAY_ELEMS(a)
#define AV_LOG_INFO
Standard information.
Definition: log.h:187
static const AVFilterPad inputs[]
Definition: af_afftfilt.c:376
int research_size_uv
Definition: vf_nlmeans.c:59
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:215
struct weighted_avg * wa
Definition: vf_nlmeans.c:64
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:81
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
static void compute_unsafe_ssd_integral_image(uint32_t *dst, int dst_linesize_32, int startx, int starty, const uint8_t *src, int 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:198
planar YUV 4:1:0, 9bpp, (1 Cr & Cb sample per 4x4 Y samples)
Definition: pixfmt.h:68
Describe the class of an AVClass context structure.
Definition: log.h:67
Filter definition.
Definition: avfilter.h:144
const char * name
Filter name.
Definition: avfilter.h:148
#define s1
Definition: regdef.h:38
double sum
Definition: vf_nlmeans.c:44
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:319
static int weight(int i, int blen, int offset)
Definition: diracdec.c:1506
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:262
AVFilterInternal * internal
An opaque struct for libavfilter internal use.
Definition: avfilter.h:347
static int flags
Definition: cpu.c:47
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:198
uint32_t * ii
Definition: vf_nlmeans.c:61
int patch_hsize_uv
Definition: vf_nlmeans.c:57
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:62
Y , 8bpp.
Definition: pixfmt.h:70
static av_cold int init(AVFilterContext *ctx)
Definition: vf_nlmeans.c:481
double pdiff_lut_scale
Definition: vf_nlmeans.c:67
planar YUV 4:4:4, 24bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV444P and setting col...
Definition: pixfmt.h:76
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples)
Definition: pixfmt.h:69
avfilter_execute_func * execute
Definition: internal.h:153
uint32_t * ii_orig
Definition: vf_nlmeans.c:60
static int slice_end(AVCodecContext *avctx, AVFrame *pict)
Handle slice ends.
Definition: mpeg12dec.c:2051
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:272
An instance of a filter.
Definition: avfilter.h:307
double sigma
Definition: vf_nlmeans.c:55
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
Definition: vf_nlmeans.c:445
static int get_integral_patch_value(const uint32_t *ii, int ii_lz_32, int x, int y, int p)
Definition: vf_nlmeans.c:132
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:100
#define av_malloc_array(a, b)
#define WEIGHT_LUT_SIZE
Definition: vf_nlmeans.c:48
internal API functions
static av_cold void uninit(AVFilterContext *ctx)
Definition: vf_nlmeans.c:515
AVPixelFormat
Pixel format.
Definition: pixfmt.h:60
int max_meaningful_diff
Definition: vf_nlmeans.c:68
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition: frame.c:589
const uint32_t * ii_start
Definition: vf_nlmeans.c:362
static int nlmeans_plane(AVFilterContext *ctx, int w, int h, int p, int r, uint8_t *dst, int dst_linesize, const uint8_t *src, int src_linesize)
Definition: vf_nlmeans.c:394