FFmpeg
Loading...
Searching...
No Matches
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/imgutils.h"
24#include "libavutil/mem.h"
25#include "libavutil/opt.h"
26#include "libavutil/pixdesc.h"
27#include "avfilter.h"
28#include "filters.h"
29#include "median.h"
30#include "video.h"
31
32#define DEPTH 8
33#include "median_template.c"
34
35#undef DEPTH
36#define DEPTH 9
37#include "median_template.c"
38
39#undef DEPTH
40#define DEPTH 10
41#include "median_template.c"
42
43#undef DEPTH
44#define DEPTH 12
45#include "median_template.c"
46
47#undef DEPTH
48#define DEPTH 14
49#include "median_template.c"
50
51#undef DEPTH
52#define DEPTH 16
53#include "median_template.c"
54
55#define OFFSET(x) offsetof(MedianContext, x)
56#define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
57
58static const AVOption median_options[] = {
59 { "radius", "set median radius", OFFSET(radius), AV_OPT_TYPE_INT, {.i64=1}, 1, 127, FLAGS },
60 { "planes", "set planes to filter", OFFSET(planes), AV_OPT_TYPE_INT, {.i64=0xF}, 0, 0xF, FLAGS },
61 { "radiusV", "set median vertical radius", OFFSET(radiusV), AV_OPT_TYPE_INT, {.i64=0},0, 127, FLAGS },
62 { "percentile", "set median percentile", OFFSET(percentile), AV_OPT_TYPE_FLOAT, {.dbl=.5}, 0., 1., FLAGS },
63 { NULL }
64};
65
67
68static 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
74static 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
80static 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
107
109{
110 for (int i = 0; i < s->nb_planes; i++) {
111 if (!(s->planes & (1 << i)))
112 continue;
113
114 if (s->planewidth[i] < s->radius * 2 + 1) {
115 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);
116 s->radius = (s->planewidth[i] - 1) / 2;
117 }
118
119 if (s->planeheight[i] < s->radiusV * 2 + 1) {
120 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);
121 s->radiusV = (s->planeheight[i] - 1) / 2;
122 }
123 }
124
125 s->t = (2 * s->radius * s->radiusV + s->radiusV + s->radius) * 2.f * s->percentile;
126}
127
128static int config_input(AVFilterLink *inlink)
129{
131 MedianContext *s = inlink->dst->priv;
132
133 s->depth = desc->comp[0].depth;
134 s->planewidth[1] = s->planewidth[2] = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w);
135 s->planewidth[0] = s->planewidth[3] = inlink->w;
136 s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
137 s->planeheight[0] = s->planeheight[3] = inlink->h;
138
139 s->radiusV = !s->radiusV ? s->radius : s->radiusV;
140 s->nb_planes = av_pix_fmt_count_planes(inlink->format);
141
142 check_params(s, inlink);
143
144 s->nb_threads = FFMAX(1, FFMIN(s->planeheight[1] / (s->radiusV + 1), ff_filter_get_nb_threads(inlink->dst)));
145 s->bins = 1 << ((s->depth + 1) / 2);
146 s->fine_size = s->bins * s->bins * inlink->w;
147 s->coarse_size = s->bins * inlink->w;
148 s->coarse = av_calloc(s->nb_threads, sizeof(*s->coarse));
149 s->fine = av_calloc(s->nb_threads, sizeof(*s->fine));
150 if (!s->coarse || !s->fine)
151 return AVERROR(ENOMEM);
152 for (int i = 0; i < s->nb_threads; i++) {
153 s->coarse[i] = av_malloc_array(s->coarse_size, sizeof(**s->coarse));
154 s->fine[i] = av_malloc_array(s->fine_size, sizeof(**s->fine));
155 if (!s->coarse[i] || !s->fine[i])
156 return AVERROR(ENOMEM);
157 }
158
159 s->hadd = hadd;
160 s->hsub = hsub;
161 s->hmuladd = hmuladd;
162
163 switch (s->depth) {
164 case 8: s->filter_plane = filter_plane_8; break;
165 case 9: s->filter_plane = filter_plane_9; break;
166 case 10: s->filter_plane = filter_plane_10; break;
167 case 12: s->filter_plane = filter_plane_12; break;
168 case 14: s->filter_plane = filter_plane_14; break;
169 case 16: s->filter_plane = filter_plane_16; break;
170 }
171
172 return 0;
173}
174
175typedef struct ThreadData {
176 AVFrame *in, *out;
177} ThreadData;
178
179static int filter_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
180{
181 MedianContext *s = ctx->priv;
182 ThreadData *td = arg;
183 AVFrame *in = td->in;
184 AVFrame *out = td->out;
185
186 for (int plane = 0; plane < s->nb_planes; plane++) {
187 const int h = s->planeheight[plane];
188 const int w = s->planewidth[plane];
189 const int slice_h_start = ff_slice_pos(h, jobnr, nb_jobs);
190 const int slice_h_end = ff_slice_pos(h, jobnr + 1, nb_jobs);
191
192 if (!(s->planes & (1 << plane))) {
193 av_image_copy_plane(out->data[plane] + slice_h_start * out->linesize[plane],
194 out->linesize[plane],
195 in->data[plane] + slice_h_start * in->linesize[plane],
196 in->linesize[plane],
197 w * ((s->depth + 7) / 8),
198 slice_h_end - slice_h_start);
199 continue;
200 }
201
202 s->filter_plane(ctx, in->data[plane],
203 in->linesize[plane],
204 out->data[plane] + slice_h_start * out->linesize[plane],
205 out->linesize[plane], w, h,
206 slice_h_start, slice_h_end, jobnr);
207 }
208
209 return 0;
210}
211
212static int filter_frame(AVFilterLink *inlink, AVFrame *in)
213{
214 AVFilterContext *ctx = inlink->dst;
215 MedianContext *s = ctx->priv;
216 AVFilterLink *outlink = ctx->outputs[0];
217 ThreadData td;
218 AVFrame *out;
219
220 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
221 if (!out) {
222 av_frame_free(&in);
223 return AVERROR(ENOMEM);
224 }
226
227 td.in = in; td.out = out;
228 ff_filter_execute(ctx, filter_slice, &td, NULL, s->nb_threads);
229
230 av_frame_free(&in);
231 return ff_filter_frame(outlink, out);
232}
233
235{
236 MedianContext *s = ctx->priv;
237
238 for (int i = 0; i < s->nb_threads && s->coarse && s->fine; i++) {
239 av_freep(&s->coarse[i]);
240 av_freep(&s->fine[i]);
241 }
242
243 av_freep(&s->coarse);
244 av_freep(&s->fine);
245}
246
247static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
248 char *res, int res_len, int flags)
249{
250 MedianContext *s = ctx->priv;
251 int ret;
252
253 ret = ff_filter_process_command(ctx, cmd, args, res, res_len, flags);
254 if (ret < 0)
255 return ret;
256
257 if (!s->radiusV)
258 s->radiusV = s->radius;
259 check_params(s, ctx->inputs[0]);
260
261 return 0;
262}
263
264static const AVFilterPad median_inputs[] = {
265 {
266 .name = "default",
267 .type = AVMEDIA_TYPE_VIDEO,
268 .config_props = config_input,
269 .filter_frame = filter_frame,
270 },
271};
272
274 .p.name = "median",
275 .p.description = NULL_IF_CONFIG_SMALL("Apply Median filter."),
276 .p.priv_class = &median_class,
278 .priv_size = sizeof(MedianContext),
279 .uninit = uninit,
283 .process_command = process_command,
284};
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:87
static int config_input(AVFilterLink *inlink)
static int process_command(AVFilterContext *ctx, const char *cmd, const char *args, char *res, int res_len, int flags)
const FFFilter ff_vf_median
Definition vf_median.c:273
static FILE * out
static AVFormatContext * ctx
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition avfilter.c:1068
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:906
int ff_filter_execute(AVFilterContext *ctx, avfilter_action_func *func, void *arg, int *ret, int nb_jobs)
Definition avfilter.c:1696
int ff_filter_get_nb_threads(AVFilterContext *ctx)
Get number of threads for current filter instance.
Definition avfilter.c:846
Main libavfilter public API header.
#define flags(name, subs,...)
Definition cbs_h264.c:74
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define f(width, name)
Definition cbs_vp8.c:236
#define s(width, name)
Definition cbs_vp9.c:198
#define FLAGS
Definition cmdutils.c:598
#define AV_CEIL_RSHIFT(a, b)
Definition common.h:60
#define NULL
Definition coverity.c:32
static int filter_frame(DBEDecodeContext *s, AVFrame *frame)
Definition dolby_e.c:1067
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition opt.h:258
@ AV_OPT_TYPE_FLOAT
Underlying C type is float.
Definition opt.h:270
#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:196
#define AVFILTER_FLAG_SLICE_THREADS
The filter supports multithreading by splitting frames into multiple parts and processing them concur...
Definition avfilter.h:166
#define AVERROR(e)
Definition error.h:45
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition frame.c:64
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition frame.c:599
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition log.h:216
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
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:374
misc image utilities
static av_cold void uninit(AVBitStreamFilterContext *ctx)
const char * arg
Definition jacosubdec.c:65
#define FILTER_INPUTS(array)
Definition filters.h:264
#define FILTER_OUTPUTS(array)
Definition filters.h:265
static int ff_slice_pos(int total, int jobnr, int nb_jobs)
Compute the boundary index for a slice when work of size total is split into nb_jobs slices.
Definition filters.h:763
#define FILTER_PIXFMTS_ARRAY(array)
Definition filters.h:244
#define AVFILTER_DEFINE_CLASS(fname)
Definition filters.h:478
#define av_cold
Definition attributes.h:117
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
static enum AVPixelFormat pix_fmts[]
Definition libkvazaar.c:296
const char * desc
Definition libsvtav1.c:83
static const struct @257111027162314367033347246032313251342043035002 planes[]
uint8_t w
Definition llvidencdsp.c:39
#define FFMIN(a, b)
Definition macros.h:49
#define FFMAX(a, b)
Definition macros.h:47
#define htype
void * av_calloc(size_t nmemb, size_t size)
Definition mem.c:264
Memory handling functions.
AVOptions.
int av_pix_fmt_count_planes(enum AVPixelFormat pix_fmt)
Definition pixdesc.c:3500
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition pixdesc.c:3460
#define AV_PIX_FMT_GBRAP12
Definition pixfmt.h:569
#define AV_PIX_FMT_YUV420P16
Definition pixfmt.h:556
#define AV_PIX_FMT_YUV444P12
Definition pixfmt.h:552
#define AV_PIX_FMT_YUV444P9
Definition pixfmt.h:544
#define AV_PIX_FMT_YUV420P10
Definition pixfmt.h:545
#define AV_PIX_FMT_YUV440P12
Definition pixfmt.h:551
#define AV_PIX_FMT_GRAY9
Definition pixfmt.h:524
#define AV_PIX_FMT_GBRAP16
Definition pixfmt.h:571
#define AV_PIX_FMT_GBRP9
Definition pixfmt.h:563
#define AV_PIX_FMT_YUV422P9
Definition pixfmt.h:543
#define AV_PIX_FMT_YUVA444P10
Definition pixfmt.h:598
#define AV_PIX_FMT_YUVA420P16
Definition pixfmt.h:601
#define AV_PIX_FMT_YUV420P12
Definition pixfmt.h:549
#define AV_PIX_FMT_YUVA420P10
Definition pixfmt.h:596
#define AV_PIX_FMT_YUVA422P9
Definition pixfmt.h:594
#define AV_PIX_FMT_YUV422P12
Definition pixfmt.h:550
#define AV_PIX_FMT_GBRP10
Definition pixfmt.h:564
#define AV_PIX_FMT_YUV422P10
Definition pixfmt.h:546
#define AV_PIX_FMT_GRAY12
Definition pixfmt.h:526
#define AV_PIX_FMT_GBRP12
Definition pixfmt.h:565
#define AV_PIX_FMT_YUV420P9
Definition pixfmt.h:542
#define AV_PIX_FMT_YUVA420P9
Definition pixfmt.h:593
#define AV_PIX_FMT_YUVA422P10
Definition pixfmt.h:597
#define AV_PIX_FMT_YUV420P14
Definition pixfmt.h:553
AVPixelFormat
Pixel format.
Definition pixfmt.h:71
@ AV_PIX_FMT_NONE
Definition pixfmt.h:72
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition pixfmt.h:73
@ AV_PIX_FMT_YUV440P
planar YUV 4:4:0 (1 Cr & Cb sample per 1x2 Y samples)
Definition pixfmt.h:106
@ AV_PIX_FMT_YUV422P
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition pixfmt.h:77
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition pixfmt.h:81
@ AV_PIX_FMT_YUVA420P
planar YUV 4:2:0, 20bpp, (1 Cr & Cb sample per 2x2 Y & A samples)
Definition pixfmt.h:108
@ AV_PIX_FMT_YUVJ440P
planar YUV 4:4:0 full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV440P and setting color_range
Definition pixfmt.h:107
@ AV_PIX_FMT_YUV410P
planar YUV 4:1:0, 9bpp, (1 Cr & Cb sample per 4x4 Y samples)
Definition pixfmt.h:79
@ AV_PIX_FMT_YUV411P
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples)
Definition pixfmt.h:80
@ AV_PIX_FMT_YUV444P
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition pixfmt.h:78
@ AV_PIX_FMT_YUVA444P
planar YUV 4:4:4 32bpp, (1 Cr & Cb sample per 1x1 Y & A samples)
Definition pixfmt.h:174
@ AV_PIX_FMT_YUVJ411P
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples) full scale (JPEG), deprecated in favor ...
Definition pixfmt.h:283
@ AV_PIX_FMT_GBRAP
planar GBRA 4:4:4:4 32bpp
Definition pixfmt.h:212
@ AV_PIX_FMT_YUVJ422P
planar YUV 4:2:2, 16bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV422P and setting col...
Definition pixfmt.h:86
@ AV_PIX_FMT_YUVA422P
planar YUV 4:2:2 24bpp, (1 Cr & Cb sample per 2x1 Y & A samples)
Definition pixfmt.h:173
@ AV_PIX_FMT_GBRP
planar GBR 4:4:4 24bpp
Definition pixfmt.h:165
@ AV_PIX_FMT_YUVJ444P
planar YUV 4:4:4, 24bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV444P and setting col...
Definition pixfmt.h:87
@ AV_PIX_FMT_YUVJ420P
planar YUV 4:2:0, 12bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV420P and setting col...
Definition pixfmt.h:85
#define AV_PIX_FMT_YUVA422P12
Definition pixfmt.h:599
#define AV_PIX_FMT_YUV422P14
Definition pixfmt.h:554
#define AV_PIX_FMT_GRAY10
Definition pixfmt.h:525
#define AV_PIX_FMT_GRAY14
Definition pixfmt.h:527
#define AV_PIX_FMT_YUV422P16
Definition pixfmt.h:557
#define AV_PIX_FMT_GRAY16
Definition pixfmt.h:528
#define AV_PIX_FMT_GBRAP10
Definition pixfmt.h:568
#define AV_PIX_FMT_YUVA444P16
Definition pixfmt.h:603
#define AV_PIX_FMT_YUVA422P16
Definition pixfmt.h:602
#define AV_PIX_FMT_GBRP16
Definition pixfmt.h:567
#define AV_PIX_FMT_YUV444P14
Definition pixfmt.h:555
#define AV_PIX_FMT_YUVA444P9
Definition pixfmt.h:595
#define AV_PIX_FMT_GBRP14
Definition pixfmt.h:566
#define AV_PIX_FMT_YUVA444P12
Definition pixfmt.h:600
#define AV_PIX_FMT_YUV444P16
Definition pixfmt.h:558
#define AV_PIX_FMT_YUV444P10
Definition pixfmt.h:548
An instance of a filter.
Definition avfilter.h:273
void * priv
private data for use by the filter
Definition avfilter.h:288
A filter pad used for either input or output.
Definition filters.h:40
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition frame.h:493
int linesize[AV_NUM_DATA_POINTERS]
For video, a positive or negative value, which is typically indicating the size in bytes of each pict...
Definition frame.h:517
AVOption.
Definition opt.h:428
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition pixdesc.h:69
Used for passing data between threads.
Definition dsddec.c:71
AVFrame * out
#define av_malloc_array(a, b)
#define av_freep(p)
#define av_log(a,...)
#define src
Definition vp8dsp.c:248
static int filter_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
static int filter_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
Definition vf_median.c:179
static void check_params(MedianContext *s, AVFilterLink *inlink)
Definition vf_median.c:108
static int config_input(AVFilterLink *inlink)
Definition vf_median.c:128
static void hmuladd(htype *dst, const htype *src, int f, int bins)
Definition vf_median.c:80
static const AVOption median_options[]
Definition vf_median.c:58
static const AVFilterPad median_inputs[]
Definition vf_median.c:264
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
Definition vf_median.c:212
static int process_command(AVFilterContext *ctx, const char *cmd, const char *args, char *res, int res_len, int flags)
Definition vf_median.c:247
static av_cold void uninit(AVFilterContext *ctx)
Definition vf_median.c:234
static void hsub(htype *dst, const htype *src, int bins)
Definition vf_median.c:74
#define OFFSET(x)
Definition vf_median.c:55
static void hadd(htype *dst, const htype *src, int bins)
Definition vf_median.c:68
const AVFilterPad ff_video_default_filterpad[1]
An AVFilterPad array whose only entry has name "default" and is of type AVMEDIA_TYPE_VIDEO.
Definition video.c:37
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition video.c:89