Message ID | 20200413145215.16949-1-onemda@gmail.com |
---|---|
State | Accepted |
Headers | show |
Series | [FFmpeg-devel] avfilter: add tmedian filter | expand |
Context | Check | Description |
---|---|---|
andriy/default | pending | |
andriy/make | success | Make finished |
andriy/make_fate | success | Make fate finished |
On 4/13/2020 11:52 AM, Paul B Mahol wrote: > Signed-off-by: Paul B Mahol <onemda@gmail.com> > --- > doc/filters.texi | 19 +++++++ > libavfilter/Makefile | 1 + > libavfilter/allfilters.c | 1 + > libavfilter/vf_xmedian.c | 108 +++++++++++++++++++++++++++++++++++++-- > 4 files changed, 124 insertions(+), 5 deletions(-) > > diff --git a/doc/filters.texi b/doc/filters.texi > index a4f99ef376..60c899490a 100644 > --- a/doc/filters.texi > +++ b/doc/filters.texi > @@ -18371,6 +18371,25 @@ enabled for @option{mode} @var{interleave_top} and @var{interleave_bottom}. > > @end table > > +@section tmedian > +Pick median pixels from several successive input video frames. > + > +The filter accepts the following options: > + > +@table @option > +@item radius > +Set radius of median filter. > +Default is 1. Allowed range is from 1 to 127. > + > +@item planes > +Set which planes to filter. Default value is @code{15}, by which all planes are processed. > + > +@item percentile > +Set median percentile. Default value is @code{0.5}. > +Default value of @code{0.5} will pick always median values, while @code{0} will pick > +minimum values, and @code{1} maximum values. > +@end table > + > @section tmix > > Mix successive video frames. > diff --git a/libavfilter/Makefile b/libavfilter/Makefile > index ecbc628868..b54e6cd374 100644 > --- a/libavfilter/Makefile > +++ b/libavfilter/Makefile > @@ -417,6 +417,7 @@ OBJS-$(CONFIG_THUMBNAIL_CUDA_FILTER) += vf_thumbnail_cuda.o vf_thumbnail > OBJS-$(CONFIG_TILE_FILTER) += vf_tile.o > OBJS-$(CONFIG_TINTERLACE_FILTER) += vf_tinterlace.o > OBJS-$(CONFIG_TLUT2_FILTER) += vf_lut2.o framesync.o > +OBJS-$(CONFIG_TMEDIAN_FILTER) += vf_xmedian.o framesync.o > OBJS-$(CONFIG_TMIX_FILTER) += vf_mix.o framesync.o > OBJS-$(CONFIG_TONEMAP_FILTER) += vf_tonemap.o colorspace.o > OBJS-$(CONFIG_TONEMAP_OPENCL_FILTER) += vf_tonemap_opencl.o colorspace.o opencl.o \ > diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c > index fb32bef788..5842196118 100644 > --- a/libavfilter/allfilters.c > +++ b/libavfilter/allfilters.c > @@ -398,6 +398,7 @@ extern AVFilter ff_vf_thumbnail_cuda; > extern AVFilter ff_vf_tile; > extern AVFilter ff_vf_tinterlace; > extern AVFilter ff_vf_tlut2; > +extern AVFilter ff_vf_tmedian; > extern AVFilter ff_vf_tmix; > extern AVFilter ff_vf_tonemap; > extern AVFilter ff_vf_tonemap_opencl; > diff --git a/libavfilter/vf_xmedian.c b/libavfilter/vf_xmedian.c > index 52c5b060fa..15857d6f14 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; > @@ -259,7 +268,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 +295,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; > @@ -318,10 +330,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) > @@ -349,6 +363,7 @@ static const AVFilterPad outputs[] = { > { NULL } > }; > > +#if CONFIG_XMEDIAN_FILTER > AVFILTER_DEFINE_CLASS(xmedian); > > AVFilter ff_vf_xmedian = { > @@ -363,3 +378,86 @@ AVFilter ff_vf_xmedian = { > .activate = activate, > .flags = AVFILTER_FLAG_DYNAMIC_INPUTS | AVFILTER_FLAG_SLICE_THREADS, > }; > + > +#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; I think you can simplify this using the FFBufQueue API instead. ff_bufqueue_add() and ff_bufqueue_get() here, ff_bufqueue_peek() in the s->median_frames functions to read a frame without altering the queue, etc. See vf_atadenoise.c and other users. > + } > + > + 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 = FLAGS }, > + { "percentile", "set percentile", OFFSET(percentile), AV_OPT_TYPE_FLOAT, {.dbl=0.5}, 0, 1, .flags = FLAGS }, > + { 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, > +}; > + > +#endif /* CONFIG_TMEDIAN_FILTER */ >
On 4/13/20, James Almer <jamrial@gmail.com> wrote: > On 4/13/2020 11:52 AM, Paul B Mahol wrote: >> Signed-off-by: Paul B Mahol <onemda@gmail.com> >> --- >> doc/filters.texi | 19 +++++++ >> libavfilter/Makefile | 1 + >> libavfilter/allfilters.c | 1 + >> libavfilter/vf_xmedian.c | 108 +++++++++++++++++++++++++++++++++++++-- >> 4 files changed, 124 insertions(+), 5 deletions(-) >> >> diff --git a/doc/filters.texi b/doc/filters.texi >> index a4f99ef376..60c899490a 100644 >> --- a/doc/filters.texi >> +++ b/doc/filters.texi >> @@ -18371,6 +18371,25 @@ enabled for @option{mode} @var{interleave_top} >> and @var{interleave_bottom}. >> >> @end table >> >> +@section tmedian >> +Pick median pixels from several successive input video frames. >> + >> +The filter accepts the following options: >> + >> +@table @option >> +@item radius >> +Set radius of median filter. >> +Default is 1. Allowed range is from 1 to 127. >> + >> +@item planes >> +Set which planes to filter. Default value is @code{15}, by which all >> planes are processed. >> + >> +@item percentile >> +Set median percentile. Default value is @code{0.5}. >> +Default value of @code{0.5} will pick always median values, while >> @code{0} will pick >> +minimum values, and @code{1} maximum values. >> +@end table >> + >> @section tmix >> >> Mix successive video frames. >> diff --git a/libavfilter/Makefile b/libavfilter/Makefile >> index ecbc628868..b54e6cd374 100644 >> --- a/libavfilter/Makefile >> +++ b/libavfilter/Makefile >> @@ -417,6 +417,7 @@ OBJS-$(CONFIG_THUMBNAIL_CUDA_FILTER) += >> vf_thumbnail_cuda.o vf_thumbnail >> OBJS-$(CONFIG_TILE_FILTER) += vf_tile.o >> OBJS-$(CONFIG_TINTERLACE_FILTER) += vf_tinterlace.o >> OBJS-$(CONFIG_TLUT2_FILTER) += vf_lut2.o framesync.o >> +OBJS-$(CONFIG_TMEDIAN_FILTER) += vf_xmedian.o framesync.o >> OBJS-$(CONFIG_TMIX_FILTER) += vf_mix.o framesync.o >> OBJS-$(CONFIG_TONEMAP_FILTER) += vf_tonemap.o colorspace.o >> OBJS-$(CONFIG_TONEMAP_OPENCL_FILTER) += vf_tonemap_opencl.o >> colorspace.o opencl.o \ >> diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c >> index fb32bef788..5842196118 100644 >> --- a/libavfilter/allfilters.c >> +++ b/libavfilter/allfilters.c >> @@ -398,6 +398,7 @@ extern AVFilter ff_vf_thumbnail_cuda; >> extern AVFilter ff_vf_tile; >> extern AVFilter ff_vf_tinterlace; >> extern AVFilter ff_vf_tlut2; >> +extern AVFilter ff_vf_tmedian; >> extern AVFilter ff_vf_tmix; >> extern AVFilter ff_vf_tonemap; >> extern AVFilter ff_vf_tonemap_opencl; >> diff --git a/libavfilter/vf_xmedian.c b/libavfilter/vf_xmedian.c >> index 52c5b060fa..15857d6f14 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; >> @@ -259,7 +268,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 +295,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; >> @@ -318,10 +330,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) >> @@ -349,6 +363,7 @@ static const AVFilterPad outputs[] = { >> { NULL } >> }; >> >> +#if CONFIG_XMEDIAN_FILTER >> AVFILTER_DEFINE_CLASS(xmedian); >> >> AVFilter ff_vf_xmedian = { >> @@ -363,3 +378,86 @@ AVFilter ff_vf_xmedian = { >> .activate = activate, >> .flags = AVFILTER_FLAG_DYNAMIC_INPUTS | >> AVFILTER_FLAG_SLICE_THREADS, >> }; >> + >> +#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; > > I think you can simplify this using the FFBufQueue API instead. > ff_bufqueue_add() and ff_bufqueue_get() here, ff_bufqueue_peek() in the > s->median_frames functions to read a frame without altering the queue, etc. Nope. I will not use that API as it is not really needed here. Thanks for your comments anyway. > > See vf_atadenoise.c and other users. > >> + } >> + >> + 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 = FLAGS }, >> + { "percentile", "set percentile", OFFSET(percentile), >> AV_OPT_TYPE_FLOAT, {.dbl=0.5}, 0, 1, .flags = FLAGS }, >> + { 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, >> +}; >> + >> +#endif /* CONFIG_TMEDIAN_FILTER */ >> > > _______________________________________________ > ffmpeg-devel mailing list > ffmpeg-devel@ffmpeg.org > https://ffmpeg.org/mailman/listinfo/ffmpeg-devel > > To unsubscribe, visit link above, or email > ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
James Almer (12020-04-13): > I think you can simplify this using the FFBufQueue API instead. > ff_bufqueue_add() and ff_bufqueue_get() here, ff_bufqueue_peek() in the > s->median_frames functions to read a frame without altering the queue, etc. Or even simpler by treating the array as circular. Regards,
On 4/13/20, Paul B Mahol <onemda@gmail.com> wrote: > Signed-off-by: Paul B Mahol <onemda@gmail.com> > --- > doc/filters.texi | 19 +++++++ > libavfilter/Makefile | 1 + > libavfilter/allfilters.c | 1 + > libavfilter/vf_xmedian.c | 108 +++++++++++++++++++++++++++++++++++++-- > 4 files changed, 124 insertions(+), 5 deletions(-) > Will apply.
On 16/04/2020 17:10, Paul B Mahol wrote:
> Will apply.
Your behavior here is still unacceptable. You have dismissed or ignore
both comments without any given reason.
- Derek
On 4/16/20, Derek Buitenhuis <derek.buitenhuis@gmail.com> wrote: > On 16/04/2020 17:10, Paul B Mahol wrote: >> Will apply. > > Your behavior here is still unacceptable. You have dismissed or ignore > both comments without any given reason. I gave reason. They are either complicated or very slow.
Paul B Mahol (12020-04-16):
> I gave reason. They are either complicated or very slow.
I am sorry, but re-reading your answers, I do not see reasons, I see
terse answers that show you can't be bothered to even consider the
suggestion.
This is not how a collaboration project works. If there are remarks
about a patch, the discussion must go on until everybody involved is
satisfied. People being discouraged of discussing is not the same thing.
Which brings to the painful question:
When somebody doesn't work well with the project, including frequently
setting very shorts ultimatums to push patches without reviews or
bypassing comments, should they keep their commit rights?
On 4/16/20, Nicolas George <george@nsup.org> wrote: > Paul B Mahol (12020-04-16): >> I gave reason. They are either complicated or very slow. > > I am sorry, but re-reading your answers, I do not see reasons, I see > terse answers that show you can't be bothered to even consider the > suggestion. > > This is not how a collaboration project works. If there are remarks > about a patch, the discussion must go on until everybody involved is > satisfied. People being discouraged of discussing is not the same thing. > > Which brings to the painful question: > > When somebody doesn't work well with the project, including frequently > setting very shorts ultimatums to push patches without reviews or > bypassing comments, should they keep their commit rights? > Why we should listen to your requests? Who you are to demand changes to code? I'm not bypassing comments, I just state that they are immature, like yours.
On 16/04/2020 18:23, Paul B Mahol wrote: > Why we should listen to your requests? Who you are to demand changes to code? > > I'm not bypassing comments, I just state that they are immature, like yours. That the FFmpeg community continues to put up with this sort of behavior, more often than no remaining entirely silent, aside from a few people, really does speak volumes. Not much has changed since 2016. - Derek
On 4/16/20, Derek Buitenhuis <derek.buitenhuis@gmail.com> wrote: > On 16/04/2020 18:23, Paul B Mahol wrote: >> Why we should listen to your requests? Who you are to demand changes to >> code? >> >> I'm not bypassing comments, I just state that they are immature, like >> yours. > > That the FFmpeg community continues to put up with this sort > of behavior, more often than no remaining entirely silent, aside from > a few people, really does speak volumes. Not much has changed since 2016. > Yes, you are judge, jury and lawyer at same time.
diff --git a/doc/filters.texi b/doc/filters.texi index a4f99ef376..60c899490a 100644 --- a/doc/filters.texi +++ b/doc/filters.texi @@ -18371,6 +18371,25 @@ enabled for @option{mode} @var{interleave_top} and @var{interleave_bottom}. @end table +@section tmedian +Pick median pixels from several successive input video frames. + +The filter accepts the following options: + +@table @option +@item radius +Set radius of median filter. +Default is 1. Allowed range is from 1 to 127. + +@item planes +Set which planes to filter. Default value is @code{15}, by which all planes are processed. + +@item percentile +Set median percentile. Default value is @code{0.5}. +Default value of @code{0.5} will pick always median values, while @code{0} will pick +minimum values, and @code{1} maximum values. +@end table + @section tmix Mix successive video frames. diff --git a/libavfilter/Makefile b/libavfilter/Makefile index ecbc628868..b54e6cd374 100644 --- a/libavfilter/Makefile +++ b/libavfilter/Makefile @@ -417,6 +417,7 @@ OBJS-$(CONFIG_THUMBNAIL_CUDA_FILTER) += vf_thumbnail_cuda.o vf_thumbnail OBJS-$(CONFIG_TILE_FILTER) += vf_tile.o OBJS-$(CONFIG_TINTERLACE_FILTER) += vf_tinterlace.o OBJS-$(CONFIG_TLUT2_FILTER) += vf_lut2.o framesync.o +OBJS-$(CONFIG_TMEDIAN_FILTER) += vf_xmedian.o framesync.o OBJS-$(CONFIG_TMIX_FILTER) += vf_mix.o framesync.o OBJS-$(CONFIG_TONEMAP_FILTER) += vf_tonemap.o colorspace.o OBJS-$(CONFIG_TONEMAP_OPENCL_FILTER) += vf_tonemap_opencl.o colorspace.o opencl.o \ diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c index fb32bef788..5842196118 100644 --- a/libavfilter/allfilters.c +++ b/libavfilter/allfilters.c @@ -398,6 +398,7 @@ extern AVFilter ff_vf_thumbnail_cuda; extern AVFilter ff_vf_tile; extern AVFilter ff_vf_tinterlace; extern AVFilter ff_vf_tlut2; +extern AVFilter ff_vf_tmedian; extern AVFilter ff_vf_tmix; extern AVFilter ff_vf_tonemap; extern AVFilter ff_vf_tonemap_opencl; diff --git a/libavfilter/vf_xmedian.c b/libavfilter/vf_xmedian.c index 52c5b060fa..15857d6f14 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; @@ -259,7 +268,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 +295,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; @@ -318,10 +330,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) @@ -349,6 +363,7 @@ static const AVFilterPad outputs[] = { { NULL } }; +#if CONFIG_XMEDIAN_FILTER AVFILTER_DEFINE_CLASS(xmedian); AVFilter ff_vf_xmedian = { @@ -363,3 +378,86 @@ AVFilter ff_vf_xmedian = { .activate = activate, .flags = AVFILTER_FLAG_DYNAMIC_INPUTS | AVFILTER_FLAG_SLICE_THREADS, }; + +#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 = FLAGS }, + { "percentile", "set percentile", OFFSET(percentile), AV_OPT_TYPE_FLOAT, {.dbl=0.5}, 0, 1, .flags = FLAGS }, + { 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, +}; + +#endif /* CONFIG_TMEDIAN_FILTER */
Signed-off-by: Paul B Mahol <onemda@gmail.com> --- doc/filters.texi | 19 +++++++ libavfilter/Makefile | 1 + libavfilter/allfilters.c | 1 + libavfilter/vf_xmedian.c | 108 +++++++++++++++++++++++++++++++++++++-- 4 files changed, 124 insertions(+), 5 deletions(-)