X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=libavfilter%2Fvf_xmedian.c;h=8b6dd69709bc75adc51a4f9be762da6cdd629cce;hb=930391e5988abe126d29c5e9b09fab459e0b8936;hp=52c5b060fa27d640a4cfb8b0a46fecdf8803aaa0;hpb=47773f7979d77c1fcd08b57e85af1ad08d9248c8;p=ffmpeg diff --git a/libavfilter/vf_xmedian.c b/libavfilter/vf_xmedian.c index 52c5b060fa2..8b6dd69709b 100644 --- a/libavfilter/vf_xmedian.c +++ b/libavfilter/vf_xmedian.c @@ -35,9 +35,11 @@ typedef struct XMedianContext { const AVClass *class; const AVPixFmtDescriptor *desc; int nb_inputs; + int nb_frames; int planes; float percentile; + int tmedian; int radius; int index; int depth; @@ -95,7 +97,14 @@ static av_cold int init(AVFilterContext *ctx) XMedianContext *s = ctx->priv; int ret; - s->radius = s->nb_inputs / 2; + s->tmedian = !strcmp(ctx->filter->name, "tmedian"); + + if (!s->tmedian) { + s->radius = s->nb_inputs / 2; + } else { + s->nb_inputs = s->radius * 2 + 1; + } + if (s->nb_inputs & 1) s->index = s->radius * 2.f * s->percentile; else @@ -104,7 +113,7 @@ static av_cold int init(AVFilterContext *ctx) if (!s->frames) return AVERROR(ENOMEM); - for (int i = 0; i < s->nb_inputs; i++) { + for (int i = 0; i < s->nb_inputs && !s->tmedian; i++) { AVFilterPad pad = { 0 }; pad.type = AVMEDIA_TYPE_VIDEO; @@ -235,14 +244,20 @@ static int process_frame(FFFrameSync *fs) return ret; } - out = ff_get_video_buffer(outlink, outlink->w, outlink->h); + if (ctx->is_disabled) { + out = av_frame_clone(in[0]); + } else { + out = ff_get_video_buffer(outlink, outlink->w, outlink->h); + } if (!out) return AVERROR(ENOMEM); out->pts = av_rescale_q(s->fs.pts, s->fs.time_base, outlink->time_base); - td.in = in; - td.out = out; - ctx->internal->execute(ctx, s->median_frames, &td, NULL, FFMIN(s->height[1], ff_filter_get_nb_threads(ctx))); + if (!ctx->is_disabled) { + td.in = in; + td.out = out; + ctx->internal->execute(ctx, s->median_frames, &td, NULL, FFMIN(s->height[1], ff_filter_get_nb_threads(ctx))); + } return ff_filter_frame(outlink, out); } @@ -259,7 +274,7 @@ static int config_output(AVFilterLink *outlink) FFFrameSyncIn *in; int i, ret; - for (int i = 1; i < s->nb_inputs; i++) { + for (int i = 1; i < s->nb_inputs && !s->tmedian; i++) { if (ctx->inputs[i]->h != height || ctx->inputs[i]->w != width) { 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); return AVERROR(EINVAL); @@ -286,6 +301,9 @@ static int config_output(AVFilterLink *outlink) s->height[1] = s->height[2] = AV_CEIL_RSHIFT(inlink->h, s->desc->log2_chroma_h); s->height[0] = s->height[3] = inlink->h; + if (s->tmedian) + return 0; + outlink->w = width; outlink->h = height; outlink->frame_rate = frame_rate; @@ -304,7 +322,7 @@ static int config_output(AVFilterLink *outlink) in[i].time_base = inlink->time_base; in[i].sync = 1; in[i].before = EXT_STOP; - in[i].after = EXT_STOP; + in[i].after = EXT_INFINITY; } ret = ff_framesync_configure(&s->fs); @@ -318,10 +336,12 @@ static av_cold void uninit(AVFilterContext *ctx) XMedianContext *s = ctx->priv; ff_framesync_uninit(&s->fs); - av_freep(&s->frames); - for (int i = 0; i < ctx->nb_inputs; i++) + for (int i = 0; i < ctx->nb_inputs && !s->tmedian; i++) av_freep(&ctx->input_pads[i].name); + for (int i = 0; i < s->nb_frames && s->frames && s->tmedian; i++) + av_frame_free(&s->frames[i]); + av_freep(&s->frames); } static int activate(AVFilterContext *ctx) @@ -330,13 +350,32 @@ static int activate(AVFilterContext *ctx) return ff_framesync_activate(&s->fs); } +static int process_command(AVFilterContext *ctx, const char *cmd, const char *args, + char *res, int res_len, int flags) +{ + XMedianContext *s = ctx->priv; + int ret; + + ret = ff_filter_process_command(ctx, cmd, args, res, res_len, flags); + if (ret < 0) + return ret; + + if (s->nb_inputs & 1) + s->index = s->radius * 2.f * s->percentile; + else + s->index = av_clip(s->radius * 2.f * s->percentile, 1, s->nb_inputs - 1); + + return 0; +} + #define OFFSET(x) offsetof(XMedianContext, x) #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM +#define TFLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_RUNTIME_PARAM static const AVOption xmedian_options[] = { { "inputs", "set number of inputs", OFFSET(nb_inputs), AV_OPT_TYPE_INT, {.i64=3}, 3, 255, .flags = FLAGS }, - { "planes", "set planes to filter", OFFSET(planes), AV_OPT_TYPE_INT, {.i64=15}, 0, 15, .flags = FLAGS }, - { "percentile", "set percentile", OFFSET(percentile),AV_OPT_TYPE_FLOAT,{.dbl=0.5}, 0, 1, .flags = FLAGS }, + { "planes", "set planes to filter", OFFSET(planes), AV_OPT_TYPE_INT, {.i64=15}, 0, 15, .flags =TFLAGS }, + { "percentile", "set percentile", OFFSET(percentile),AV_OPT_TYPE_FLOAT,{.dbl=0.5}, 0, 1, .flags =TFLAGS }, { NULL }, }; @@ -349,7 +388,8 @@ static const AVFilterPad outputs[] = { { NULL } }; -AVFILTER_DEFINE_CLASS(xmedian); +#if CONFIG_XMEDIAN_FILTER +FRAMESYNC_DEFINE_CLASS(xmedian, XMedianContext, fs); AVFilter ff_vf_xmedian = { .name = "xmedian", @@ -358,8 +398,95 @@ AVFilter ff_vf_xmedian = { .priv_class = &xmedian_class, .query_formats = query_formats, .outputs = outputs, + .preinit = xmedian_framesync_preinit, .init = init, .uninit = uninit, .activate = activate, - .flags = AVFILTER_FLAG_DYNAMIC_INPUTS | AVFILTER_FLAG_SLICE_THREADS, + .flags = AVFILTER_FLAG_DYNAMIC_INPUTS | AVFILTER_FLAG_SLICE_THREADS | + AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL, + .process_command = process_command, }; + +#endif /* CONFIG_XMEDIAN_FILTER */ + +#if CONFIG_TMEDIAN_FILTER +static int tmedian_filter_frame(AVFilterLink *inlink, AVFrame *in) +{ + AVFilterContext *ctx = inlink->dst; + AVFilterLink *outlink = ctx->outputs[0]; + XMedianContext *s = ctx->priv; + ThreadData td; + AVFrame *out; + + if (s->nb_frames < s->nb_inputs) { + s->frames[s->nb_frames] = in; + s->nb_frames++; + if (s->nb_frames < s->nb_inputs) + return 0; + } else { + av_frame_free(&s->frames[0]); + memmove(&s->frames[0], &s->frames[1], sizeof(*s->frames) * (s->nb_inputs - 1)); + s->frames[s->nb_inputs - 1] = in; + } + + if (ctx->is_disabled) { + out = av_frame_clone(s->frames[0]); + if (!out) + return AVERROR(ENOMEM); + return ff_filter_frame(outlink, out); + } + + out = ff_get_video_buffer(outlink, outlink->w, outlink->h); + if (!out) + return AVERROR(ENOMEM); + out->pts = s->frames[0]->pts; + + td.out = out; + td.in = s->frames; + ctx->internal->execute(ctx, s->median_frames, &td, NULL, FFMIN(s->height[0], ff_filter_get_nb_threads(ctx))); + + return ff_filter_frame(outlink, out); +} + +static const AVOption tmedian_options[] = { + { "radius", "set median filter radius", OFFSET(radius), AV_OPT_TYPE_INT, {.i64=1}, 1, 127, .flags = FLAGS }, + { "planes", "set planes to filter", OFFSET(planes), AV_OPT_TYPE_INT, {.i64=15}, 0, 15, .flags =TFLAGS }, + { "percentile", "set percentile", OFFSET(percentile), AV_OPT_TYPE_FLOAT, {.dbl=0.5}, 0, 1, .flags =TFLAGS }, + { NULL }, +}; + +static const AVFilterPad tmedian_inputs[] = { + { + .name = "default", + .type = AVMEDIA_TYPE_VIDEO, + .filter_frame = tmedian_filter_frame, + }, + { NULL } +}; + +static const AVFilterPad tmedian_outputs[] = { + { + .name = "default", + .type = AVMEDIA_TYPE_VIDEO, + .config_props = config_output, + }, + { NULL } +}; + +AVFILTER_DEFINE_CLASS(tmedian); + +AVFilter ff_vf_tmedian = { + .name = "tmedian", + .description = NULL_IF_CONFIG_SMALL("Pick median pixels from successive frames."), + .priv_size = sizeof(XMedianContext), + .priv_class = &tmedian_class, + .query_formats = query_formats, + .inputs = tmedian_inputs, + .outputs = tmedian_outputs, + .init = init, + .uninit = uninit, + .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL | AVFILTER_FLAG_SLICE_THREADS, + .process_command = process_command, +}; + +#endif /* CONFIG_TMEDIAN_FILTER */