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
21 #include "libavutil/avstring.h"
22 #include "libavutil/imgutils.h"
23 #include "libavutil/intreadwrite.h"
24 #include "libavutil/opt.h"
25 #include "libavutil/pixdesc.h"
26 #include "libavutil/qsort.h"
31 #include "framesync.h"
34 typedef struct XMedianContext {
36 const AVPixFmtDescriptor *desc;
51 int (*median_frames)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs);
54 static int query_formats(AVFilterContext *ctx)
56 static const enum AVPixelFormat pixel_fmts[] = {
63 AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P,
64 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P,
65 AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P,
66 AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P,
67 AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ444P,
69 AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
70 AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
72 AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV420P12,
74 AV_PIX_FMT_YUV444P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV420P14,
75 AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
76 AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
77 AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
80 AVFilterFormats *formats = ff_make_format_list(pixel_fmts);
82 return AVERROR(ENOMEM);
83 return ff_set_common_formats(ctx, formats);
86 static av_cold int init(AVFilterContext *ctx)
88 XMedianContext *s = ctx->priv;
91 s->radius = s->nb_inputs / 2;
92 s->frames = av_calloc(s->nb_inputs, sizeof(*s->frames));
94 return AVERROR(ENOMEM);
96 for (int i = 0; i < s->nb_inputs; i++) {
97 AVFilterPad pad = { 0 };
99 pad.type = AVMEDIA_TYPE_VIDEO;
100 pad.name = av_asprintf("input%d", i);
102 return AVERROR(ENOMEM);
104 if ((ret = ff_insert_inpad(ctx, i, &pad)) < 0) {
113 typedef struct ThreadData {
117 static int comparei(const void *p1, const void *p2)
119 int left = *(const int *)p1;
120 int right = *(const int *)p2;
121 return FFDIFFSIGN(left, right);
124 static int median_frames16(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
126 XMedianContext *s = ctx->priv;
127 ThreadData *td = arg;
128 AVFrame **in = td->in;
129 AVFrame *out = td->out;
130 const int nb_inputs = s->nb_inputs;
131 const int radius = s->radius;
134 for (int p = 0; p < s->nb_planes; p++) {
135 const int slice_start = (s->height[p] * jobnr) / nb_jobs;
136 const int slice_end = (s->height[p] * (jobnr+1)) / nb_jobs;
137 uint16_t *dst = (uint16_t *)(out->data[p] + slice_start * out->linesize[p]);
139 if (!((1 << p) & s->planes)) {
140 av_image_copy_plane((uint8_t *)dst, out->linesize[p],
141 in[0]->data[p] + slice_start * in[radius]->linesize[p],
143 s->linesize[p], slice_end - slice_start);
147 for (int y = slice_start; y < slice_end; y++) {
148 for (int x = 0; x < s->width[p]; x++) {
149 for (int i = 0; i < nb_inputs; i++) {
150 const uint16_t *src = (const uint16_t *)(in[i]->data[p] + y * in[i]->linesize[p]);
154 AV_QSORT(values, nb_inputs, int, comparei);
156 dst[x] = values[radius];
158 dst[x] = (values[radius] + values[radius - 1]) >> 1;
161 dst += out->linesize[p] / 2;
168 static int median_frames8(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
170 XMedianContext *s = ctx->priv;
171 ThreadData *td = arg;
172 AVFrame **in = td->in;
173 AVFrame *out = td->out;
174 const int nb_inputs = s->nb_inputs;
175 const int radius = s->radius;
178 for (int p = 0; p < s->nb_planes; p++) {
179 const int slice_start = (s->height[p] * jobnr) / nb_jobs;
180 const int slice_end = (s->height[p] * (jobnr+1)) / nb_jobs;
181 uint8_t *dst = out->data[p] + slice_start * out->linesize[p];
183 if (!((1 << p) & s->planes)) {
184 av_image_copy_plane(dst, out->linesize[p],
185 in[0]->data[p] + slice_start * in[0]->linesize[p],
187 s->linesize[p], slice_end - slice_start);
191 for (int y = slice_start; y < slice_end; y++) {
192 for (int x = 0; x < s->width[p]; x++) {
193 for (int i = 0; i < nb_inputs; i++)
194 values[i] = in[i]->data[p][y * in[i]->linesize[p] + x];
196 AV_QSORT(values, nb_inputs, int, comparei);
198 dst[x] = values[radius];
200 dst[x] = (values[radius] + values[radius - 1]) >> 1;
203 dst += out->linesize[p];
210 static int process_frame(FFFrameSync *fs)
212 AVFilterContext *ctx = fs->parent;
213 AVFilterLink *outlink = ctx->outputs[0];
214 XMedianContext *s = fs->opaque;
215 AVFrame **in = s->frames;
220 for (i = 0; i < s->nb_inputs; i++) {
221 if ((ret = ff_framesync_get_frame(&s->fs, i, &in[i], 0)) < 0)
225 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
227 return AVERROR(ENOMEM);
228 out->pts = av_rescale_q(s->fs.pts, s->fs.time_base, outlink->time_base);
232 ctx->internal->execute(ctx, s->median_frames, &td, NULL, FFMIN(s->height[1], ff_filter_get_nb_threads(ctx)));
234 return ff_filter_frame(outlink, out);
237 static int config_output(AVFilterLink *outlink)
239 AVFilterContext *ctx = outlink->src;
240 XMedianContext *s = ctx->priv;
241 AVRational frame_rate = ctx->inputs[0]->frame_rate;
242 AVRational sar = ctx->inputs[0]->sample_aspect_ratio;
243 AVFilterLink *inlink = ctx->inputs[0];
244 int height = ctx->inputs[0]->h;
245 int width = ctx->inputs[0]->w;
249 for (int i = 1; i < s->nb_inputs; i++) {
250 if (ctx->inputs[i]->h != height || ctx->inputs[i]->w != width) {
251 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);
252 return AVERROR(EINVAL);
256 s->desc = av_pix_fmt_desc_get(outlink->format);
259 s->nb_planes = av_pix_fmt_count_planes(outlink->format);
260 s->depth = s->desc->comp[0].depth;
261 s->max = (1 << s->depth) - 1;
264 s->median_frames = median_frames8;
266 s->median_frames = median_frames16;
268 if ((ret = av_image_fill_linesizes(s->linesize, inlink->format, inlink->w)) < 0)
271 s->width[1] = s->width[2] = AV_CEIL_RSHIFT(inlink->w, s->desc->log2_chroma_w);
272 s->width[0] = s->width[3] = inlink->w;
273 s->height[1] = s->height[2] = AV_CEIL_RSHIFT(inlink->h, s->desc->log2_chroma_h);
274 s->height[0] = s->height[3] = inlink->h;
278 outlink->frame_rate = frame_rate;
279 outlink->sample_aspect_ratio = sar;
281 if ((ret = ff_framesync_init(&s->fs, ctx, s->nb_inputs)) < 0)
286 s->fs.on_event = process_frame;
288 for (i = 0; i < s->nb_inputs; i++) {
289 AVFilterLink *inlink = ctx->inputs[i];
291 in[i].time_base = inlink->time_base;
293 in[i].before = EXT_STOP;
294 in[i].after = EXT_STOP;
297 ret = ff_framesync_configure(&s->fs);
298 outlink->time_base = s->fs.time_base;
303 static av_cold void uninit(AVFilterContext *ctx)
305 XMedianContext *s = ctx->priv;
307 ff_framesync_uninit(&s->fs);
308 av_freep(&s->frames);
310 for (int i = 0; i < ctx->nb_inputs; i++)
311 av_freep(&ctx->input_pads[i].name);
314 static int activate(AVFilterContext *ctx)
316 XMedianContext *s = ctx->priv;
317 return ff_framesync_activate(&s->fs);
320 #define OFFSET(x) offsetof(XMedianContext, x)
321 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
323 static const AVOption xmedian_options[] = {
324 { "inputs", "set number of inputs", OFFSET(nb_inputs), AV_OPT_TYPE_INT, {.i64=3}, 3, 255, .flags = FLAGS },
325 { "planes", "set planes to filter", OFFSET(planes), AV_OPT_TYPE_INT, {.i64=15}, 0, 15, .flags = FLAGS },
329 static const AVFilterPad outputs[] = {
332 .type = AVMEDIA_TYPE_VIDEO,
333 .config_props = config_output,
338 AVFILTER_DEFINE_CLASS(xmedian);
340 AVFilter ff_vf_xmedian = {
342 .description = NULL_IF_CONFIG_SMALL("Pick median pixels from several video inputs."),
343 .priv_size = sizeof(XMedianContext),
344 .priv_class = &xmedian_class,
345 .query_formats = query_formats,
349 .activate = activate,
350 .flags = AVFILTER_FLAG_DYNAMIC_INPUTS | AVFILTER_FLAG_SLICE_THREADS,