2 * Copyright (c) 2019 Paul B Mahol
4 * This file is part of FFmpeg.
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.
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.
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:
23 #include "libavutil/avassert.h"
24 #include "libavutil/imgutils.h"
25 #include "libavutil/opt.h"
26 #include "libavutil/pixdesc.h"
34 #include "median_template.c"
38 #include "median_template.c"
42 #include "median_template.c"
46 #include "median_template.c"
50 #include "median_template.c"
54 #include "median_template.c"
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
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 { "percentile", "set median percentile", OFFSET(percentile), AV_OPT_TYPE_FLOAT, {.dbl=.5}, 0., 1., FLAGS },
67 AVFILTER_DEFINE_CLASS(median);
69 static void hadd(htype *dst, const htype *src, int bins)
71 for (int i = 0; i < bins; i++)
75 static void hsub(htype *dst, const htype *src, int bins)
77 for (int i = 0; i < bins; i++)
81 static void hmuladd(htype *dst, const htype *src, int f, int bins)
83 for (int i = 0; i < bins; i++)
87 static int query_formats(AVFilterContext *ctx)
89 static const enum AVPixelFormat pix_fmts[] = {
90 AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV440P,
91 AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P,
92 AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUV420P,
93 AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
94 AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
95 AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP, AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY9,
96 AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9, AV_PIX_FMT_GBRP9,
97 AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA444P9,
98 AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
99 AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV440P12,
100 AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV444P14,
101 AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
102 AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA444P10,
103 AV_PIX_FMT_YUVA422P12, AV_PIX_FMT_YUVA444P12,
104 AV_PIX_FMT_YUVA420P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA444P16,
105 AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
106 AV_PIX_FMT_GBRAP10, AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16,
107 AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY14, AV_PIX_FMT_GRAY16,
111 return ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
114 static void check_params(MedianContext *s, AVFilterLink *inlink)
116 for (int i = 0; i < s->nb_planes; i++) {
117 if (!(s->planes & (1 << i)))
120 if (s->planewidth[i] < s->radius * 2 + 1) {
121 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);
122 s->radius = (s->planewidth[i] - 1) / 2;
125 if (s->planeheight[i] < s->radiusV * 2 + 1) {
126 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);
127 s->radiusV = (s->planeheight[i] - 1) / 2;
131 s->t = (2 * s->radius * s->radiusV + s->radiusV + s->radius) * 2.f * s->percentile;
134 static int config_input(AVFilterLink *inlink)
136 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
137 MedianContext *s = inlink->dst->priv;
139 s->depth = desc->comp[0].depth;
140 s->planewidth[1] = s->planewidth[2] = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w);
141 s->planewidth[0] = s->planewidth[3] = inlink->w;
142 s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
143 s->planeheight[0] = s->planeheight[3] = inlink->h;
145 s->radiusV = !s->radiusV ? s->radius : s->radiusV;
146 s->nb_planes = av_pix_fmt_count_planes(inlink->format);
148 check_params(s, inlink);
150 s->nb_threads = FFMAX(1, FFMIN(s->planeheight[1] / (s->radiusV + 1), ff_filter_get_nb_threads(inlink->dst)));
151 s->bins = 1 << ((s->depth + 1) / 2);
152 s->fine_size = s->bins * s->bins * inlink->w;
153 s->coarse_size = s->bins * inlink->w;
154 s->coarse = av_calloc(s->nb_threads, sizeof(*s->coarse));
155 s->fine = av_calloc(s->nb_threads, sizeof(*s->fine));
156 if (!s->coarse || !s->fine)
157 return AVERROR(ENOMEM);
158 for (int i = 0; i < s->nb_threads; i++) {
159 s->coarse[i] = av_malloc_array(s->coarse_size, sizeof(**s->coarse));
160 s->fine[i] = av_malloc_array(s->fine_size, sizeof(**s->fine));
161 if (!s->coarse[i] || !s->fine[i])
162 return AVERROR(ENOMEM);
167 s->hmuladd = hmuladd;
170 case 8: s->filter_plane = filter_plane_8; break;
171 case 9: s->filter_plane = filter_plane_9; break;
172 case 10: s->filter_plane = filter_plane_10; break;
173 case 12: s->filter_plane = filter_plane_12; break;
174 case 14: s->filter_plane = filter_plane_14; break;
175 case 16: s->filter_plane = filter_plane_16; break;
181 typedef struct ThreadData {
185 static int filter_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
187 MedianContext *s = ctx->priv;
188 ThreadData *td = arg;
189 AVFrame *in = td->in;
190 AVFrame *out = td->out;
192 for (int plane = 0; plane < s->nb_planes; plane++) {
193 const int h = s->planeheight[plane];
194 const int w = s->planewidth[plane];
195 const int slice_h_start = (h * jobnr) / nb_jobs;
196 const int slice_h_end = (h * (jobnr+1)) / nb_jobs;
198 if (!(s->planes & (1 << plane))) {
199 av_image_copy_plane(out->data[plane] + slice_h_start * out->linesize[plane],
200 out->linesize[plane],
201 in->data[plane] + slice_h_start * in->linesize[plane],
203 w * ((s->depth + 7) / 8),
204 slice_h_end - slice_h_start);
208 s->filter_plane(ctx, in->data[plane],
210 out->data[plane] + slice_h_start * out->linesize[plane],
211 out->linesize[plane], w, h,
212 slice_h_start, slice_h_end, jobnr);
218 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
220 AVFilterContext *ctx = inlink->dst;
221 MedianContext *s = ctx->priv;
222 AVFilterLink *outlink = ctx->outputs[0];
226 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
229 return AVERROR(ENOMEM);
231 av_frame_copy_props(out, in);
233 td.in = in; td.out = out;
234 ctx->internal->execute(ctx, filter_slice, &td, NULL, s->nb_threads);
237 return ff_filter_frame(outlink, out);
240 static av_cold void uninit(AVFilterContext *ctx)
242 MedianContext *s = ctx->priv;
244 for (int i = 0; i < s->nb_threads && s->coarse && s->fine; i++) {
245 av_freep(&s->coarse[i]);
246 av_freep(&s->fine[i]);
249 av_freep(&s->coarse);
253 static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
254 char *res, int res_len, int flags)
256 MedianContext *s = ctx->priv;
259 ret = ff_filter_process_command(ctx, cmd, args, res, res_len, flags);
264 s->radiusV = s->radius;
265 check_params(s, ctx->inputs[0]);
270 static const AVFilterPad median_inputs[] = {
273 .type = AVMEDIA_TYPE_VIDEO,
274 .config_props = config_input,
275 .filter_frame = filter_frame,
280 static const AVFilterPad median_outputs[] = {
283 .type = AVMEDIA_TYPE_VIDEO,
288 AVFilter ff_vf_median = {
290 .description = NULL_IF_CONFIG_SMALL("Apply Median filter."),
291 .priv_size = sizeof(MedianContext),
292 .priv_class = &median_class,
294 .query_formats = query_formats,
295 .inputs = median_inputs,
296 .outputs = median_outputs,
297 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
298 .process_command = process_command,