]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_median.c
avfilter/vf_median: add support for 12bit yuva formats
[ffmpeg] / libavfilter / vf_median.c
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
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
86 static int query_formats(AVFilterContext *ctx)
87 {
88     static const enum AVPixelFormat pix_fmts[] = {
89         AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV440P,
90         AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P,
91         AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUV420P,
92         AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
93         AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
94         AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP, AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY9,
95         AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9, AV_PIX_FMT_GBRP9,
96         AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA444P9,
97         AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
98         AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV440P12,
99         AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV444P14,
100         AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
101         AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA444P10,
102         AV_PIX_FMT_YUVA422P12, AV_PIX_FMT_YUVA444P12,
103         AV_PIX_FMT_YUVA420P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA444P16,
104         AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
105         AV_PIX_FMT_GBRAP10, AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16,
106         AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY14, AV_PIX_FMT_GRAY16,
107         AV_PIX_FMT_NONE
108     };
109
110     return ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
111 }
112
113 static int config_input(AVFilterLink *inlink)
114 {
115     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
116     MedianContext *s = inlink->dst->priv;
117
118     s->depth = desc->comp[0].depth;
119     s->planewidth[1] = s->planewidth[2] = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w);
120     s->planewidth[0] = s->planewidth[3] = inlink->w;
121     s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
122     s->planeheight[0] = s->planeheight[3] = inlink->h;
123
124     s->radiusV = !s->radiusV ? s->radius : s->radiusV;
125     s->nb_planes = av_pix_fmt_count_planes(inlink->format);
126     s->t = 2 * s->radius * s->radiusV + 2 * s->radius;
127
128     for (int i = 0; i < s->nb_planes; i++) {
129         if (!(s->planes & (1 << i)))
130             continue;
131
132         if (s->planewidth[i] < s->radius * 2 + 1) {
133             av_log(inlink->dst, AV_LOG_ERROR, "The %d plane width %d must be not less than %d\n", i, s->planewidth[i], s->radius * 2 + 1);
134             return AVERROR(EINVAL);
135         }
136
137         if (s->planeheight[i] < s->radiusV * 2 + 1) {
138             av_log(inlink->dst, AV_LOG_ERROR, "The %d plane height %d must be not less than %d\n", i, s->planeheight[i], s->radiusV * 2 + 1);
139             return AVERROR(EINVAL);
140         }
141     }
142
143     s->nb_threads = FFMAX(1, FFMIN(s->planeheight[1] / (s->radiusV + 1), ff_filter_get_nb_threads(inlink->dst)));
144     s->bins   = 1 << ((s->depth + 1) / 2);
145     s->fine_size = s->bins * s->bins * inlink->w;
146     s->coarse_size = s->bins * inlink->w;
147     s->coarse = av_calloc(s->nb_threads, sizeof(*s->coarse));
148     s->fine   = av_calloc(s->nb_threads, sizeof(*s->fine));
149     if (!s->coarse || !s->fine)
150         return AVERROR(ENOMEM);
151     for (int i = 0; i < s->nb_threads; i++) {
152         s->coarse[i] = av_malloc_array(s->coarse_size, sizeof(**s->coarse));
153         s->fine[i]   = av_malloc_array(s->fine_size, sizeof(**s->fine));
154         if (!s->coarse[i] || !s->fine[i])
155             return AVERROR(ENOMEM);
156     }
157
158     s->hadd = hadd;
159     s->hsub = hsub;
160     s->hmuladd = hmuladd;
161
162     switch (s->depth) {
163     case  8: s->filter_plane = filter_plane_8;  break;
164     case  9: s->filter_plane = filter_plane_9;  break;
165     case 10: s->filter_plane = filter_plane_10; break;
166     case 12: s->filter_plane = filter_plane_12; break;
167     case 14: s->filter_plane = filter_plane_14; break;
168     case 16: s->filter_plane = filter_plane_16; break;
169     }
170
171     return 0;
172 }
173
174 typedef struct ThreadData {
175     AVFrame *in, *out;
176 } ThreadData;
177
178 static int filter_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
179 {
180     MedianContext *s = ctx->priv;
181     ThreadData *td = arg;
182     AVFrame *in = td->in;
183     AVFrame *out = td->out;
184
185     for (int plane = 0; plane < s->nb_planes; plane++) {
186         const int h = s->planeheight[plane];
187         const int w = s->planewidth[plane];
188         const int slice_h_start = (h * jobnr) / nb_jobs;
189         const int slice_h_end = (h * (jobnr+1)) / nb_jobs;
190
191         if (!(s->planes & (1 << plane))) {
192             av_image_copy_plane(out->data[plane] + slice_h_start * out->linesize[plane],
193                                 out->linesize[plane],
194                                 in->data[plane] + slice_h_start * in->linesize[plane],
195                                 in->linesize[plane],
196                                 w * ((s->depth + 7) / 8),
197                                 slice_h_end - slice_h_start);
198             continue;
199         }
200
201         s->filter_plane(ctx, in->data[plane],
202                         in->linesize[plane],
203                         out->data[plane] + slice_h_start * out->linesize[plane],
204                         out->linesize[plane], w, h,
205                         slice_h_start, slice_h_end, jobnr);
206     }
207
208     return 0;
209 }
210
211 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
212 {
213     AVFilterContext *ctx = inlink->dst;
214     MedianContext *s = ctx->priv;
215     AVFilterLink *outlink = ctx->outputs[0];
216     ThreadData td;
217     AVFrame *out;
218
219     out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
220     if (!out) {
221         av_frame_free(&in);
222         return AVERROR(ENOMEM);
223     }
224     av_frame_copy_props(out, in);
225
226     td.in = in; td.out = out;
227     ctx->internal->execute(ctx, filter_slice, &td, NULL, s->nb_threads);
228
229     av_frame_free(&in);
230     return ff_filter_frame(outlink, out);
231 }
232
233 static av_cold void uninit(AVFilterContext *ctx)
234 {
235     MedianContext *s = ctx->priv;
236
237     for (int i = 0; i < s->nb_threads && s->coarse && s->fine; i++) {
238         av_freep(&s->coarse[i]);
239         av_freep(&s->fine[i]);
240     }
241
242     av_freep(&s->coarse);
243     av_freep(&s->fine);
244 }
245
246 static const AVFilterPad median_inputs[] = {
247     {
248         .name         = "default",
249         .type         = AVMEDIA_TYPE_VIDEO,
250         .config_props = config_input,
251         .filter_frame = filter_frame,
252     },
253     { NULL }
254 };
255
256 static const AVFilterPad median_outputs[] = {
257     {
258         .name = "default",
259         .type = AVMEDIA_TYPE_VIDEO,
260     },
261     { NULL }
262 };
263
264 AVFilter ff_vf_median = {
265     .name          = "median",
266     .description   = NULL_IF_CONFIG_SMALL("Apply Median filter."),
267     .priv_size     = sizeof(MedianContext),
268     .priv_class    = &median_class,
269     .uninit        = uninit,
270     .query_formats = query_formats,
271     .inputs        = median_inputs,
272     .outputs       = median_outputs,
273     .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
274 };