FFmpeg
vf_median.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019 Paul B Mahol
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  * Redistribution and use in source and binary forms, with or without modification,
20  * are permitted provided that the following conditions are met:
21  */
22 
23 #include "libavutil/avassert.h"
24 #include "libavutil/imgutils.h"
25 #include "libavutil/opt.h"
26 #include "libavutil/pixdesc.h"
27 #include "avfilter.h"
28 #include "formats.h"
29 #include "internal.h"
30 #include "median.h"
31 #include "video.h"
32 
33 #define DEPTH 8
34 #include "median_template.c"
35 
36 #undef DEPTH
37 #define DEPTH 9
38 #include "median_template.c"
39 
40 #undef DEPTH
41 #define DEPTH 10
42 #include "median_template.c"
43 
44 #undef DEPTH
45 #define DEPTH 12
46 #include "median_template.c"
47 
48 #undef DEPTH
49 #define DEPTH 14
50 #include "median_template.c"
51 
52 #undef DEPTH
53 #define DEPTH 16
54 #include "median_template.c"
55 
56 #define OFFSET(x) offsetof(MedianContext, x)
57 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
58 
59 static const AVOption median_options[] = {
60  { "radius", "set median radius", OFFSET(radius), AV_OPT_TYPE_INT, {.i64=1}, 1, 127, FLAGS },
61  { "planes", "set planes to filter", OFFSET(planes), AV_OPT_TYPE_INT, {.i64=0xF}, 0, 0xF, FLAGS },
62  { "radiusV", "set median vertical radius", OFFSET(radiusV), AV_OPT_TYPE_INT, {.i64=0},0, 127, FLAGS },
63  { NULL }
64 };
65 
66 AVFILTER_DEFINE_CLASS(median);
67 
68 static void hadd(htype *dst, const htype *src, int bins)
69 {
70  for (int i = 0; i < bins; i++)
71  dst[i] += src[i];
72 }
73 
74 static void hsub(htype *dst, const htype *src, int bins)
75 {
76  for (int i = 0; i < bins; i++)
77  dst[i] -= src[i];
78 }
79 
80 static void hmuladd(htype *dst, const htype *src, int f, int bins)
81 {
82  for (int i = 0; i < bins; i++)
83  dst[i] += f * src[i];
84 }
85 
87 {
88  static const enum AVPixelFormat pix_fmts[] = {
108  };
109 
110  return ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
111 }
112 
114 {
115  for (int i = 0; i < s->nb_planes; i++) {
116  if (!(s->planes & (1 << i)))
117  continue;
118 
119  if (s->planewidth[i] < s->radius * 2 + 1) {
120  av_log(inlink->dst, AV_LOG_WARNING, "The %d plane width %d must be not less than %d, clipping radius.\n", i, s->planewidth[i], s->radius * 2 + 1);
121  s->radius = (s->planewidth[i] - 1) / 2;
122  }
123 
124  if (s->planeheight[i] < s->radiusV * 2 + 1) {
125  av_log(inlink->dst, AV_LOG_WARNING, "The %d plane height %d must be not less than %d, clipping radiusV.\n", i, s->planeheight[i], s->radiusV * 2 + 1);
126  s->radiusV = (s->planeheight[i] - 1) / 2;
127  }
128  }
129 
130  s->t = 2 * s->radius * s->radiusV + 2 * s->radius;
131 }
132 
134 {
136  MedianContext *s = inlink->dst->priv;
137 
138  s->depth = desc->comp[0].depth;
139  s->planewidth[1] = s->planewidth[2] = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w);
140  s->planewidth[0] = s->planewidth[3] = inlink->w;
141  s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
142  s->planeheight[0] = s->planeheight[3] = inlink->h;
143 
144  s->radiusV = !s->radiusV ? s->radius : s->radiusV;
146 
147  check_params(s, inlink);
148 
149  s->nb_threads = FFMAX(1, FFMIN(s->planeheight[1] / (s->radiusV + 1), ff_filter_get_nb_threads(inlink->dst)));
150  s->bins = 1 << ((s->depth + 1) / 2);
151  s->fine_size = s->bins * s->bins * inlink->w;
152  s->coarse_size = s->bins * inlink->w;
153  s->coarse = av_calloc(s->nb_threads, sizeof(*s->coarse));
154  s->fine = av_calloc(s->nb_threads, sizeof(*s->fine));
155  if (!s->coarse || !s->fine)
156  return AVERROR(ENOMEM);
157  for (int i = 0; i < s->nb_threads; i++) {
158  s->coarse[i] = av_malloc_array(s->coarse_size, sizeof(**s->coarse));
159  s->fine[i] = av_malloc_array(s->fine_size, sizeof(**s->fine));
160  if (!s->coarse[i] || !s->fine[i])
161  return AVERROR(ENOMEM);
162  }
163 
164  s->hadd = hadd;
165  s->hsub = hsub;
166  s->hmuladd = hmuladd;
167 
168  switch (s->depth) {
169  case 8: s->filter_plane = filter_plane_8; break;
170  case 9: s->filter_plane = filter_plane_9; break;
171  case 10: s->filter_plane = filter_plane_10; break;
172  case 12: s->filter_plane = filter_plane_12; break;
173  case 14: s->filter_plane = filter_plane_14; break;
174  case 16: s->filter_plane = filter_plane_16; break;
175  }
176 
177  return 0;
178 }
179 
180 typedef struct ThreadData {
181  AVFrame *in, *out;
182 } ThreadData;
183 
184 static int filter_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
185 {
186  MedianContext *s = ctx->priv;
187  ThreadData *td = arg;
188  AVFrame *in = td->in;
189  AVFrame *out = td->out;
190 
191  for (int plane = 0; plane < s->nb_planes; plane++) {
192  const int h = s->planeheight[plane];
193  const int w = s->planewidth[plane];
194  const int slice_h_start = (h * jobnr) / nb_jobs;
195  const int slice_h_end = (h * (jobnr+1)) / nb_jobs;
196 
197  if (!(s->planes & (1 << plane))) {
198  av_image_copy_plane(out->data[plane] + slice_h_start * out->linesize[plane],
199  out->linesize[plane],
200  in->data[plane] + slice_h_start * in->linesize[plane],
201  in->linesize[plane],
202  w * ((s->depth + 7) / 8),
203  slice_h_end - slice_h_start);
204  continue;
205  }
206 
207  s->filter_plane(ctx, in->data[plane],
208  in->linesize[plane],
209  out->data[plane] + slice_h_start * out->linesize[plane],
210  out->linesize[plane], w, h,
211  slice_h_start, slice_h_end, jobnr);
212  }
213 
214  return 0;
215 }
216 
218 {
219  AVFilterContext *ctx = inlink->dst;
220  MedianContext *s = ctx->priv;
221  AVFilterLink *outlink = ctx->outputs[0];
222  ThreadData td;
223  AVFrame *out;
224 
225  out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
226  if (!out) {
227  av_frame_free(&in);
228  return AVERROR(ENOMEM);
229  }
230  av_frame_copy_props(out, in);
231 
232  td.in = in; td.out = out;
233  ctx->internal->execute(ctx, filter_slice, &td, NULL, s->nb_threads);
234 
235  av_frame_free(&in);
236  return ff_filter_frame(outlink, out);
237 }
238 
240 {
241  MedianContext *s = ctx->priv;
242 
243  for (int i = 0; i < s->nb_threads && s->coarse && s->fine; i++) {
244  av_freep(&s->coarse[i]);
245  av_freep(&s->fine[i]);
246  }
247 
248  av_freep(&s->coarse);
249  av_freep(&s->fine);
250 }
251 
252 static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
253  char *res, int res_len, int flags)
254 {
255  MedianContext *s = ctx->priv;
256  int ret;
257 
258  ret = ff_filter_process_command(ctx, cmd, args, res, res_len, flags);
259  if (ret < 0)
260  return ret;
261 
262  if (!s->radiusV)
263  s->radiusV = s->radius;
264  check_params(s, ctx->inputs[0]);
265 
266  return 0;
267 }
268 
269 static const AVFilterPad median_inputs[] = {
270  {
271  .name = "default",
272  .type = AVMEDIA_TYPE_VIDEO,
273  .config_props = config_input,
274  .filter_frame = filter_frame,
275  },
276  { NULL }
277 };
278 
279 static const AVFilterPad median_outputs[] = {
280  {
281  .name = "default",
282  .type = AVMEDIA_TYPE_VIDEO,
283  },
284  { NULL }
285 };
286 
288  .name = "median",
289  .description = NULL_IF_CONFIG_SMALL("Apply Median filter."),
290  .priv_size = sizeof(MedianContext),
291  .priv_class = &median_class,
292  .uninit = uninit,
294  .inputs = median_inputs,
295  .outputs = median_outputs,
298 };
#define NULL
Definition: coverity.c:32
#define AV_PIX_FMT_YUVA422P16
Definition: pixfmt.h:430
AVFrame * out
Definition: af_adeclick.c:488
#define AV_PIX_FMT_YUVA422P9
Definition: pixfmt.h:422
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2522
This structure describes decoded (raw) audio or video data.
Definition: frame.h:295
AVOption.
Definition: opt.h:246
#define AV_PIX_FMT_YUVA420P10
Definition: pixfmt.h:424
#define AV_PIX_FMT_YUV444P14
Definition: pixfmt.h:397
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
Definition: vf_median.c:217
#define AV_PIX_FMT_GBRAP10
Definition: pixfmt.h:407
#define AV_PIX_FMT_YUVA422P10
Definition: pixfmt.h:425
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:71
misc image utilities
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
int av_pix_fmt_count_planes(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2562
Main libavfilter public API header.
const char * desc
Definition: nvenc.c:68
planar GBR 4:4:4 24bpp
Definition: pixfmt.h:168
#define AV_PIX_FMT_GBRP10
Definition: pixfmt.h:403
#define AV_PIX_FMT_GRAY9
Definition: pixfmt.h:367
#define AV_PIX_FMT_YUV420P12
Definition: pixfmt.h:391
static int process_command(AVFilterContext *ctx, const char *cmd, const char *args, char *res, int res_len, int flags)
Definition: vf_median.c:252
int nb_threads
Definition: median.h:39
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
static const struct @322 planes[]
uint8_t log2_chroma_w
Amount to shift the luma width right to find the chroma width.
Definition: pixdesc.h:92
void * av_calloc(size_t nmemb, size_t size)
Non-inlined equivalent of av_mallocz_array().
Definition: mem.c:244
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:283
#define AV_PIX_FMT_GRAY10
Definition: pixfmt.h:368
#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_PIX_FMT_GRAY12
Definition: pixfmt.h:369
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:346
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1093
planar YUV 4:2:0, 20bpp, (1 Cr & Cb sample per 2x2 Y & A samples)
Definition: pixfmt.h:101
AVComponentDescriptor comp[4]
Parameters that describe how pixels are packed.
Definition: pixdesc.h:117
#define av_cold
Definition: attributes.h:82
uint16_t ** coarse
Definition: median.h:41
AVOptions.
#define f(width, name)
Definition: cbs_vp9.c:255
int planeheight[4]
Definition: median.h:36
static int config_input(AVFilterLink *inlink)
Definition: vf_median.c:133
#define AV_PIX_FMT_YUVA420P9
Definition: pixfmt.h:421
#define AV_PIX_FMT_GBRP9
Definition: pixfmt.h:402
int plane
Definition: vf_blend.c:57
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
static void hmuladd(htype *dst, const htype *src, int f, int bins)
Definition: vf_median.c:80
#define AV_PIX_FMT_YUV444P16
Definition: pixfmt.h:400
#define AV_PIX_FMT_YUV422P12
Definition: pixfmt.h:392
#define AV_PIX_FMT_YUVA420P16
Definition: pixfmt.h:429
static const AVFilterPad median_outputs[]
Definition: vf_median.c:279
static const AVOption median_options[]
Definition: vf_median.c:59
#define av_log(a,...)
A filter pad used for either input or output.
Definition: internal.h:54
planar YUV 4:2:2 24bpp, (1 Cr & Cb sample per 2x1 Y & A samples)
Definition: pixfmt.h:176
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:259
void(* filter_plane)(AVFilterContext *ctx, const uint8_t *ssrc, int src_linesize, uint8_t *ddst, int dst_linesize, int width, int height, int slice_h_start, int slice_h_end, int jobnr)
Definition: median.h:50
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:569
#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
uint16_t ** fine
Definition: median.h:41
int radius
Definition: median.h:32
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:202
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:186
int ff_filter_process_command(AVFilterContext *ctx, const char *cmd, const char *arg, char *res, int res_len, int flags)
Generic processing of user supplied commands that are set in the same way as the filter options...
Definition: avfilter.c:887
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
#define AV_PIX_FMT_YUVA444P16
Definition: pixfmt.h:431
const char * arg
Definition: jacosubdec.c:66
#define AV_PIX_FMT_GBRAP12
Definition: pixfmt.h:408
simple assert() macros that are a bit more flexible than ISO C assert().
#define AV_PIX_FMT_YUV444P10
Definition: pixfmt.h:390
static void hadd(htype *dst, const htype *src, int bins)
Definition: vf_median.c:68
int fine_size
Definition: median.h:42
void(* hmuladd)(uint16_t *dst, const uint16_t *src, int f, int bins)
Definition: median.h:48
#define FFMAX(a, b)
Definition: common.h:94
#define AV_PIX_FMT_GBRAP16
Definition: pixfmt.h:409
static int filter_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
Definition: vf_median.c:184
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:70
#define AV_PIX_FMT_YUV422P9
Definition: pixfmt.h:385
#define AV_PIX_FMT_GBRP16
Definition: pixfmt.h:406
int ff_filter_get_nb_threads(AVFilterContext *ctx)
Get number of threads for current filter instance.
Definition: avfilter.c:802
#define AV_PIX_FMT_GRAY16
Definition: pixfmt.h:371
#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
#define AV_PIX_FMT_YUVA444P12
Definition: pixfmt.h:428
void(* hadd)(uint16_t *dst, const uint16_t *src, int bins)
Definition: median.h:46
int bins
Definition: median.h:43
AVFormatContext * ctx
Definition: movenc.c:48
#define s(width, name)
Definition: cbs_vp9.c:257
#define AV_PIX_FMT_YUVA444P10
Definition: pixfmt.h:426
#define AV_PIX_FMT_YUV444P9
Definition: pixfmt.h:386
#define AV_PIX_FMT_GBRP14
Definition: pixfmt.h:405
static const AVFilterPad outputs[]
Definition: af_acontrast.c:203
#define AV_PIX_FMT_YUV420P16
Definition: pixfmt.h:398
AVFilter ff_vf_median
Definition: vf_median.c:287
#define AV_PIX_FMT_YUV420P14
Definition: pixfmt.h:395
Used for passing data between threads.
Definition: dsddec.c:64
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:326
planar YUV 4:4:4 32bpp, (1 Cr & Cb sample per 1x1 Y & A samples)
Definition: pixfmt.h:177
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:81
#define AV_PIX_FMT_GRAY14
Definition: pixfmt.h:370
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several inputs
#define AV_PIX_FMT_YUV420P10
Definition: pixfmt.h:387
planar YUV 4:1:0, 9bpp, (1 Cr & Cb sample per 4x4 Y samples)
Definition: pixfmt.h:72
Filter definition.
Definition: avfilter.h:144
int planes
Definition: median.h:31
int planewidth[4]
Definition: median.h:35
int radiusV
Definition: median.h:33
#define htype
int depth
Definition: median.h:37
int nb_planes
Definition: median.h:38
const char * name
Filter name.
Definition: avfilter.h:148
#define AV_PIX_FMT_YUV440P12
Definition: pixfmt.h:393
#define AV_PIX_FMT_YUV420P9
Definition: pixfmt.h:384
#define OFFSET(x)
Definition: vf_median.c:56
AVFILTER_DEFINE_CLASS(median)
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:350
int coarse_size
Definition: median.h:42
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:275
static int query_formats(AVFilterContext *ctx)
Definition: vf_median.c:86
#define AV_PIX_FMT_YUV422P14
Definition: pixfmt.h:396
#define AV_PIX_FMT_GBRP12
Definition: pixfmt.h:404
#define flags(name, subs,...)
Definition: cbs_av1.c:561
AVFilterInternal * internal
An opaque struct for libavfilter internal use.
Definition: avfilter.h:378
#define AV_PIX_FMT_YUV422P10
Definition: pixfmt.h:388
#define AV_PIX_FMT_YUV444P12
Definition: pixfmt.h:394
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:309
static void check_params(MedianContext *s, AVFilterLink *inlink)
Definition: vf_median.c:113
The exact code depends on how similar the blocks are and how related they are to the and needs to apply these operations to the correct inlink or outlink if there are several Macros are available to factor that when no extra processing is inlink
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 GBRA 4:4:4:4 32bpp
Definition: pixfmt.h:215
#define AV_PIX_FMT_YUVA444P9
Definition: pixfmt.h:423
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
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples) full scale (JPEG), deprecated in favor ...
Definition: pixfmt.h:258
#define FLAGS
Definition: vf_median.c:57
An instance of a filter.
Definition: avfilter.h:338
#define av_freep(p)
planar YUV 4:4:0 (1 Cr & Cb sample per 1x2 Y samples)
Definition: pixfmt.h:99
#define av_malloc_array(a, b)
AVFrame * in
Definition: af_afftdn.c:1083
void av_image_copy_plane(uint8_t *dst, int dst_linesize, const uint8_t *src, int src_linesize, int bytewidth, int height)
Copy image plane from src to dst.
Definition: imgutils.c:338
AVFilterLink * inlink
Definition: vf_blend.c:56
internal API functions
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later.That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another.Frame references ownership and permissions
static const AVFilterPad median_inputs[]
Definition: vf_median.c:269
int depth
Number of bits in the component.
Definition: pixdesc.h:58
void(* hsub)(uint16_t *dst, const uint16_t *src, int bins)
Definition: median.h:47
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
static void hsub(htype *dst, const htype *src, int bins)
Definition: vf_median.c:74
#define AV_PIX_FMT_YUV422P16
Definition: pixfmt.h:399
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition: frame.c:654
#define AV_PIX_FMT_YUVA422P12
Definition: pixfmt.h:427
#define AV_CEIL_RSHIFT(a, b)
Definition: common.h:58
static av_cold void uninit(AVFilterContext *ctx)
Definition: vf_median.c:239