FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/vf_median.c
Date: 2024-04-19 17:50:32
Exec Total Coverage
Lines: 80 107 74.8%
Functions: 8 9 88.9%
Branches: 30 55 54.5%

Line Branch Exec Source
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 "internal.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
58 static 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
66 AVFILTER_DEFINE_CLASS(median);
67
68 2976197 static void hadd(htype *dst, const htype *src, int bins)
69 {
70
2/2
✓ Branch 0 taken 47619152 times.
✓ Branch 1 taken 2976197 times.
50595349 for (int i = 0; i < bins; i++)
71 47619152 dst[i] += src[i];
72 2976197 }
73
74 1653336 static void hsub(htype *dst, const htype *src, int bins)
75 {
76
2/2
✓ Branch 0 taken 26453376 times.
✓ Branch 1 taken 1653336 times.
28106712 for (int i = 0; i < bins; i++)
77 26453376 dst[i] -= src[i];
78 1653336 }
79
80 51292 static void hmuladd(htype *dst, const htype *src, int f, int bins)
81 {
82
2/2
✓ Branch 0 taken 820672 times.
✓ Branch 1 taken 51292 times.
871964 for (int i = 0; i < bins; i++)
83 820672 dst[i] += f * src[i];
84 51292 }
85
86 static const enum AVPixelFormat pix_fmts[] = {
87 AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV440P,
88 AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P,
89 AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUV420P,
90 AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
91 AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
92 AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP, AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY9,
93 AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9, AV_PIX_FMT_GBRP9,
94 AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA444P9,
95 AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
96 AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV440P12,
97 AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV444P14,
98 AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
99 AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA444P10,
100 AV_PIX_FMT_YUVA422P12, AV_PIX_FMT_YUVA444P12,
101 AV_PIX_FMT_YUVA420P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA444P16,
102 AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
103 AV_PIX_FMT_GBRAP10, AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16,
104 AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY14, AV_PIX_FMT_GRAY16,
105 AV_PIX_FMT_NONE
106 };
107
108 1 static void check_params(MedianContext *s, AVFilterLink *inlink)
109 {
110
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
4 for (int i = 0; i < s->nb_planes; i++) {
111
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (!(s->planes & (1 << i)))
112 continue;
113
114
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 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
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 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 1 s->t = (2 * s->radius * s->radiusV + s->radiusV + s->radius) * 2.f * s->percentile;
126 1 }
127
128 1 static int config_input(AVFilterLink *inlink)
129 {
130 1 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
131 1 MedianContext *s = inlink->dst->priv;
132
133 1 s->depth = desc->comp[0].depth;
134 1 s->planewidth[1] = s->planewidth[2] = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w);
135 1 s->planewidth[0] = s->planewidth[3] = inlink->w;
136 1 s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
137 1 s->planeheight[0] = s->planeheight[3] = inlink->h;
138
139
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 s->radiusV = !s->radiusV ? s->radius : s->radiusV;
140 1 s->nb_planes = av_pix_fmt_count_planes(inlink->format);
141
142 1 check_params(s, inlink);
143
144
3/6
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
1 s->nb_threads = FFMAX(1, FFMIN(s->planeheight[1] / (s->radiusV + 1), ff_filter_get_nb_threads(inlink->dst)));
145 1 s->bins = 1 << ((s->depth + 1) / 2);
146 1 s->fine_size = s->bins * s->bins * inlink->w;
147 1 s->coarse_size = s->bins * inlink->w;
148 1 s->coarse = av_calloc(s->nb_threads, sizeof(*s->coarse));
149 1 s->fine = av_calloc(s->nb_threads, sizeof(*s->fine));
150
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 if (!s->coarse || !s->fine)
151 return AVERROR(ENOMEM);
152
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 for (int i = 0; i < s->nb_threads; i++) {
153 1 s->coarse[i] = av_malloc_array(s->coarse_size, sizeof(**s->coarse));
154 1 s->fine[i] = av_malloc_array(s->fine_size, sizeof(**s->fine));
155
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 if (!s->coarse[i] || !s->fine[i])
156 return AVERROR(ENOMEM);
157 }
158
159 1 s->hadd = hadd;
160 1 s->hsub = hsub;
161 1 s->hmuladd = hmuladd;
162
163
1/7
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
1 switch (s->depth) {
164 1 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 1 return 0;
173 }
174
175 typedef struct ThreadData {
176 AVFrame *in, *out;
177 } ThreadData;
178
179 5 static int filter_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
180 {
181 5 MedianContext *s = ctx->priv;
182 5 ThreadData *td = arg;
183 5 AVFrame *in = td->in;
184 5 AVFrame *out = td->out;
185
186
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 5 times.
20 for (int plane = 0; plane < s->nb_planes; plane++) {
187 15 const int h = s->planeheight[plane];
188 15 const int w = s->planewidth[plane];
189 15 const int slice_h_start = (h * jobnr) / nb_jobs;
190 15 const int slice_h_end = (h * (jobnr+1)) / nb_jobs;
191
192
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
15 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 15 s->filter_plane(ctx, in->data[plane],
203 in->linesize[plane],
204 15 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 5 return 0;
210 }
211
212 5 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
213 {
214 5 AVFilterContext *ctx = inlink->dst;
215 5 MedianContext *s = ctx->priv;
216 5 AVFilterLink *outlink = ctx->outputs[0];
217 ThreadData td;
218 AVFrame *out;
219
220 5 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
221
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 if (!out) {
222 av_frame_free(&in);
223 return AVERROR(ENOMEM);
224 }
225 5 av_frame_copy_props(out, in);
226
227 5 td.in = in; td.out = out;
228 5 ff_filter_execute(ctx, filter_slice, &td, NULL, s->nb_threads);
229
230 5 av_frame_free(&in);
231 5 return ff_filter_frame(outlink, out);
232 }
233
234 2 static av_cold void uninit(AVFilterContext *ctx)
235 {
236 2 MedianContext *s = ctx->priv;
237
238
4/6
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
3 for (int i = 0; i < s->nb_threads && s->coarse && s->fine; i++) {
239 1 av_freep(&s->coarse[i]);
240 1 av_freep(&s->fine[i]);
241 }
242
243 2 av_freep(&s->coarse);
244 2 av_freep(&s->fine);
245 2 }
246
247 static 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
264 static 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
273 const AVFilter ff_vf_median = {
274 .name = "median",
275 .description = NULL_IF_CONFIG_SMALL("Apply Median filter."),
276 .priv_size = sizeof(MedianContext),
277 .priv_class = &median_class,
278 .uninit = uninit,
279 FILTER_INPUTS(median_inputs),
280 FILTER_OUTPUTS(ff_video_default_filterpad),
281 FILTER_PIXFMTS_ARRAY(pix_fmts),
282 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
283 .process_command = process_command,
284 };
285