FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/vf_xmedian.c
Date: 2024-02-16 17:37:06
Exec Total Coverage
Lines: 0 154 0.0%
Functions: 0 14 0.0%
Branches: 0 190 0.0%

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 */
20
21 #include "config_components.h"
22
23 #include "libavutil/avstring.h"
24 #include "libavutil/imgutils.h"
25 #include "libavutil/opt.h"
26 #include "libavutil/pixdesc.h"
27 #include "libavutil/qsort.h"
28
29 #include "avfilter.h"
30 #include "internal.h"
31 #include "framesync.h"
32 #include "video.h"
33
34 typedef struct XMedianContext {
35 const AVClass *class;
36 const AVPixFmtDescriptor *desc;
37 int nb_inputs;
38 int nb_frames;
39 int nb_threads;
40 int planes;
41 float percentile;
42
43 int xmedian;
44 int radius;
45 int index;
46 int depth;
47 int max;
48 int nb_planes;
49 int linesizes[4];
50 int width[4];
51 int height[4];
52
53 uint8_t **data;
54 int *linesize;
55
56 AVFrame **frames;
57 FFFrameSync fs;
58
59 int (*median_frames)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs);
60 } XMedianContext;
61
62 static const enum AVPixelFormat pixel_fmts[] = {
63 AV_PIX_FMT_GRAY8,
64 AV_PIX_FMT_GRAY9,
65 AV_PIX_FMT_GRAY10,
66 AV_PIX_FMT_GRAY12,
67 AV_PIX_FMT_GRAY14,
68 AV_PIX_FMT_GRAY16,
69 AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P,
70 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P,
71 AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P,
72 AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P,
73 AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ444P,
74 AV_PIX_FMT_YUVJ411P,
75 AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
76 AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
77 AV_PIX_FMT_YUV440P10,
78 AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV420P12,
79 AV_PIX_FMT_YUV440P12,
80 AV_PIX_FMT_YUV444P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV420P14,
81 AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
82 AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
83 AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
84 AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA444P,
85 AV_PIX_FMT_YUVA444P9, AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUVA444P12, AV_PIX_FMT_YUVA444P16,
86 AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA422P12, AV_PIX_FMT_YUVA422P16,
87 AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA420P16,
88 AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRAP10, AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16,
89 AV_PIX_FMT_NONE
90 };
91
92 static av_cold int init(AVFilterContext *ctx)
93 {
94 XMedianContext *s = ctx->priv;
95
96 if (!s->xmedian)
97 s->nb_inputs = s->radius * 2 + 1;
98
99 if (s->nb_inputs & 1)
100 s->index = s->radius * 2.f * s->percentile;
101 else
102 s->index = av_clip(s->radius * 2.f * s->percentile, 1, s->nb_inputs - 1);
103 s->frames = av_calloc(s->nb_inputs, sizeof(*s->frames));
104 if (!s->frames)
105 return AVERROR(ENOMEM);
106
107 return 0;
108 }
109
110 typedef struct ThreadData {
111 AVFrame **in, *out;
112 } ThreadData;
113
114 static int compare8(const void *p1, const void *p2)
115 {
116 int left = *(const uint8_t *)p1;
117 int right = *(const uint8_t *)p2;
118 return FFDIFFSIGN(left, right);
119 }
120
121 static int compare16(const void *p1, const void *p2)
122 {
123 int left = *(const uint16_t *)p1;
124 int right = *(const uint16_t *)p2;
125 return FFDIFFSIGN(left, right);
126 }
127
128 #define MEDIAN_SLICE(name, type, comparei) \
129 static int median_frames ## name(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) \
130 { \
131 XMedianContext *s = ctx->priv; \
132 ThreadData *td = arg; \
133 AVFrame **in = td->in; \
134 AVFrame *out = td->out; \
135 const int nb_inputs = s->nb_inputs; \
136 uint8_t **srcf = s->data + jobnr * nb_inputs; \
137 int *linesize = s->linesize + jobnr * nb_inputs; \
138 const int radius = s->radius; \
139 const int index = s->index; \
140 type values[256]; \
141 \
142 for (int p = 0; p < s->nb_planes; p++) { \
143 const int slice_start = (s->height[p] * jobnr) / nb_jobs; \
144 const int slice_end = (s->height[p] * (jobnr+1)) / nb_jobs; \
145 const int width = s->width[p]; \
146 type *dst = (type *)(out->data[p] + slice_start * out->linesize[p]); \
147 ptrdiff_t dst_linesize = out->linesize[p] / sizeof(type); \
148 \
149 if (!((1 << p) & s->planes)) { \
150 av_image_copy_plane((uint8_t *)dst, out->linesize[p], \
151 in[radius]->data[p] + slice_start * in[radius]->linesize[p], \
152 in[radius]->linesize[p], \
153 s->linesizes[p], slice_end - slice_start); \
154 continue; \
155 } \
156 \
157 for (int i = 0; i < nb_inputs; i++) \
158 linesize[i] = in[i]->linesize[p]; \
159 \
160 for (int i = 0; i < nb_inputs; i++) \
161 srcf[i] = in[i]->data[p] + slice_start * linesize[i]; \
162 \
163 for (int y = slice_start; y < slice_end; y++) { \
164 for (int x = 0; x < width; x++) { \
165 for (int i = 0; i < nb_inputs; i++) { \
166 const type *src = (const type *)srcf[i]; \
167 values[i] = src[x]; \
168 } \
169 \
170 AV_QSORT(values, nb_inputs, type, comparei); \
171 if (nb_inputs & 1) \
172 dst[x] = values[index]; \
173 else \
174 dst[x] = (values[index] + values[index - 1]) >> 1; \
175 } \
176 \
177 dst += dst_linesize; \
178 for (int i = 0; i < nb_inputs; i++) \
179 srcf[i] += linesize[i]; \
180 } \
181 } \
182 \
183 return 0; \
184 }
185
186 MEDIAN_SLICE(8, uint8_t, compare8)
187 MEDIAN_SLICE(16, uint16_t, compare16)
188
189 static void update_index(XMedianContext *s)
190 {
191 if (s->nb_inputs & 1)
192 s->index = s->radius * 2.f * s->percentile;
193 else
194 s->index = av_clip(s->radius * 2.f * s->percentile, 1, s->nb_inputs - 1);
195 }
196
197 static int process_frame(FFFrameSync *fs)
198 {
199 AVFilterContext *ctx = fs->parent;
200 AVFilterLink *outlink = ctx->outputs[0];
201 XMedianContext *s = fs->opaque;
202 AVFrame **in = s->frames;
203 AVFrame *out;
204 ThreadData td;
205 int i, ret;
206
207 update_index(s);
208
209 for (i = 0; i < s->nb_inputs; i++) {
210 if ((ret = ff_framesync_get_frame(&s->fs, i, &in[i], 0)) < 0)
211 return ret;
212 }
213
214 if (ctx->is_disabled) {
215 out = av_frame_clone(in[0]);
216 } else {
217 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
218 }
219 if (!out)
220 return AVERROR(ENOMEM);
221 out->pts = av_rescale_q(s->fs.pts, s->fs.time_base, outlink->time_base);
222
223 if (!ctx->is_disabled) {
224 td.in = in;
225 td.out = out;
226 ff_filter_execute(ctx, s->median_frames, &td, NULL,
227 FFMIN(s->height[1], s->nb_threads));
228 }
229
230 return ff_filter_frame(outlink, out);
231 }
232
233 static int config_output(AVFilterLink *outlink)
234 {
235 AVFilterContext *ctx = outlink->src;
236 XMedianContext *s = ctx->priv;
237 AVRational frame_rate = ctx->inputs[0]->frame_rate;
238 AVRational sar = ctx->inputs[0]->sample_aspect_ratio;
239 AVFilterLink *inlink = ctx->inputs[0];
240 int height = ctx->inputs[0]->h;
241 int width = ctx->inputs[0]->w;
242 FFFrameSyncIn *in;
243 int i, ret;
244
245 for (int i = 1; i < s->nb_inputs && s->xmedian; i++) {
246 if (ctx->inputs[i]->h != height || ctx->inputs[i]->w != width) {
247 av_log(ctx, AV_LOG_ERROR, "Input %d size (%dx%d) does not match input %d size (%dx%d).\n", i, ctx->inputs[i]->w, ctx->inputs[i]->h, 0, width, height);
248 return AVERROR(EINVAL);
249 }
250 }
251
252 s->desc = av_pix_fmt_desc_get(outlink->format);
253 if (!s->desc)
254 return AVERROR_BUG;
255 s->nb_planes = av_pix_fmt_count_planes(outlink->format);
256 s->depth = s->desc->comp[0].depth;
257 s->max = (1 << s->depth) - 1;
258 s->nb_threads = ff_filter_get_nb_threads(ctx);
259
260 if (s->depth <= 8)
261 s->median_frames = median_frames8;
262 else
263 s->median_frames = median_frames16;
264
265 if ((ret = av_image_fill_linesizes(s->linesizes, inlink->format, inlink->w)) < 0)
266 return ret;
267
268 s->width[1] = s->width[2] = AV_CEIL_RSHIFT(inlink->w, s->desc->log2_chroma_w);
269 s->width[0] = s->width[3] = inlink->w;
270 s->height[1] = s->height[2] = AV_CEIL_RSHIFT(inlink->h, s->desc->log2_chroma_h);
271 s->height[0] = s->height[3] = inlink->h;
272
273 s->data = av_calloc(s->nb_threads * s->nb_inputs, sizeof(*s->data));
274 if (!s->data)
275 return AVERROR(ENOMEM);
276
277 s->linesize = av_calloc(s->nb_threads * s->nb_inputs, sizeof(*s->linesize));
278 if (!s->linesize)
279 return AVERROR(ENOMEM);
280
281 if (!s->xmedian)
282 return 0;
283
284 outlink->w = width;
285 outlink->h = height;
286 outlink->frame_rate = frame_rate;
287 outlink->sample_aspect_ratio = sar;
288
289 if ((ret = ff_framesync_init(&s->fs, ctx, s->nb_inputs)) < 0)
290 return ret;
291
292 in = s->fs.in;
293 s->fs.opaque = s;
294 s->fs.on_event = process_frame;
295
296 for (i = 0; i < s->nb_inputs; i++) {
297 AVFilterLink *inlink = ctx->inputs[i];
298
299 in[i].time_base = inlink->time_base;
300 in[i].sync = 1;
301 in[i].before = EXT_STOP;
302 in[i].after = EXT_INFINITY;
303 }
304
305 ret = ff_framesync_configure(&s->fs);
306 outlink->time_base = s->fs.time_base;
307
308 return ret;
309 }
310
311 static av_cold void uninit(AVFilterContext *ctx)
312 {
313 XMedianContext *s = ctx->priv;
314
315 ff_framesync_uninit(&s->fs);
316
317 for (int i = 0; i < s->nb_frames && s->frames && !s->xmedian; i++)
318 av_frame_free(&s->frames[i]);
319 av_freep(&s->frames);
320 av_freep(&s->data);
321 av_freep(&s->linesize);
322 }
323
324 static int activate(AVFilterContext *ctx)
325 {
326 XMedianContext *s = ctx->priv;
327 return ff_framesync_activate(&s->fs);
328 }
329
330 static const AVFilterPad outputs[] = {
331 {
332 .name = "default",
333 .type = AVMEDIA_TYPE_VIDEO,
334 .config_props = config_output,
335 },
336 };
337
338 #if CONFIG_XMEDIAN_FILTER
339 static av_cold int xmedian_init(AVFilterContext *ctx)
340 {
341 XMedianContext *s = ctx->priv;
342 int ret;
343
344 s->xmedian = 1;
345
346 s->radius = s->nb_inputs / 2;
347
348 for (int i = 0; i < s->nb_inputs; i++) {
349 AVFilterPad pad = { 0 };
350
351 pad.type = AVMEDIA_TYPE_VIDEO;
352 pad.name = av_asprintf("input%d", i);
353 if (!pad.name)
354 return AVERROR(ENOMEM);
355
356 if ((ret = ff_append_inpad_free_name(ctx, &pad)) < 0)
357 return ret;
358 }
359
360 return init(ctx);
361 }
362
363 #define OFFSET(x) offsetof(XMedianContext, x)
364 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
365 #define TFLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_RUNTIME_PARAM
366
367 static const AVOption xmedian_options[] = {
368 { "inputs", "set number of inputs", OFFSET(nb_inputs), AV_OPT_TYPE_INT, {.i64=3}, 3, 255, .flags = FLAGS },
369 { "planes", "set planes to filter", OFFSET(planes), AV_OPT_TYPE_INT, {.i64=15}, 0, 15, .flags =TFLAGS },
370 { "percentile", "set percentile", OFFSET(percentile),AV_OPT_TYPE_FLOAT,{.dbl=0.5}, 0, 1, .flags =TFLAGS },
371 { NULL },
372 };
373
374 FRAMESYNC_DEFINE_CLASS(xmedian, XMedianContext, fs);
375
376 const AVFilter ff_vf_xmedian = {
377 .name = "xmedian",
378 .description = NULL_IF_CONFIG_SMALL("Pick median pixels from several video inputs."),
379 .priv_size = sizeof(XMedianContext),
380 .priv_class = &xmedian_class,
381 FILTER_OUTPUTS(outputs),
382 FILTER_PIXFMTS_ARRAY(pixel_fmts),
383 .preinit = xmedian_framesync_preinit,
384 .init = xmedian_init,
385 .uninit = uninit,
386 .activate = activate,
387 .flags = AVFILTER_FLAG_DYNAMIC_INPUTS | AVFILTER_FLAG_SLICE_THREADS |
388 AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,
389 .process_command = ff_filter_process_command,
390 };
391
392 #endif /* CONFIG_XMEDIAN_FILTER */
393
394 #if CONFIG_TMEDIAN_FILTER
395 static int tmedian_filter_frame(AVFilterLink *inlink, AVFrame *in)
396 {
397 AVFilterContext *ctx = inlink->dst;
398 AVFilterLink *outlink = ctx->outputs[0];
399 XMedianContext *s = ctx->priv;
400 ThreadData td;
401 AVFrame *out;
402
403 update_index(s);
404
405 if (s->nb_frames < s->nb_inputs) {
406 s->frames[s->nb_frames] = in;
407 s->nb_frames++;
408 if (s->nb_frames < s->nb_inputs)
409 return 0;
410 } else {
411 av_frame_free(&s->frames[0]);
412 memmove(&s->frames[0], &s->frames[1], sizeof(*s->frames) * (s->nb_inputs - 1));
413 s->frames[s->nb_inputs - 1] = in;
414 }
415
416 if (ctx->is_disabled) {
417 out = av_frame_clone(s->frames[0]);
418 if (!out)
419 return AVERROR(ENOMEM);
420 return ff_filter_frame(outlink, out);
421 }
422
423 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
424 if (!out)
425 return AVERROR(ENOMEM);
426 out->pts = s->frames[0]->pts;
427
428 td.out = out;
429 td.in = s->frames;
430 ff_filter_execute(ctx, s->median_frames, &td, NULL,
431 FFMIN(s->height[1], s->nb_threads));
432
433 return ff_filter_frame(outlink, out);
434 }
435
436 static const AVOption tmedian_options[] = {
437 { "radius", "set median filter radius", OFFSET(radius), AV_OPT_TYPE_INT, {.i64=1}, 1, 127, .flags = FLAGS },
438 { "planes", "set planes to filter", OFFSET(planes), AV_OPT_TYPE_INT, {.i64=15}, 0, 15, .flags =TFLAGS },
439 { "percentile", "set percentile", OFFSET(percentile), AV_OPT_TYPE_FLOAT, {.dbl=0.5}, 0, 1, .flags =TFLAGS },
440 { NULL },
441 };
442
443 static const AVFilterPad tmedian_inputs[] = {
444 {
445 .name = "default",
446 .type = AVMEDIA_TYPE_VIDEO,
447 .filter_frame = tmedian_filter_frame,
448 },
449 };
450
451 AVFILTER_DEFINE_CLASS(tmedian);
452
453 const AVFilter ff_vf_tmedian = {
454 .name = "tmedian",
455 .description = NULL_IF_CONFIG_SMALL("Pick median pixels from successive frames."),
456 .priv_size = sizeof(XMedianContext),
457 .priv_class = &tmedian_class,
458 FILTER_INPUTS(tmedian_inputs),
459 FILTER_OUTPUTS(outputs),
460 FILTER_PIXFMTS_ARRAY(pixel_fmts),
461 .init = init,
462 .uninit = uninit,
463 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL | AVFILTER_FLAG_SLICE_THREADS,
464 .process_command = ff_filter_process_command,
465 };
466
467 #endif /* CONFIG_TMEDIAN_FILTER */
468