Signed-off-by: Paul B Mahol <one...@gmail.com> --- doc/filters.texi | 12 +++++ libavfilter/Makefile | 1 + libavfilter/allfilters.c | 1 + libavfilter/vf_random.c | 136 +++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 150 insertions(+) create mode 100644 libavfilter/vf_random.c
diff --git a/doc/filters.texi b/doc/filters.texi index 2679090..04d9333 100644 --- a/doc/filters.texi +++ b/doc/filters.texi @@ -8095,6 +8095,18 @@ qp=2+2*sin(PI*qp) @end example @end itemize +@section random + +Flush video frames from internal cache of frames into a random order. +No frame is discarded. +Inspired by @ref{frei0r} nervous filter. + +@table @option +@item frames +Set size in number of frame of internal cache, in range from 2 to 400. +Default is 30. +@end table + @section removegrain The removegrain filter is a spatial denoiser for progressive video. diff --git a/libavfilter/Makefile b/libavfilter/Makefile index 058b9e9..8150640 100644 --- a/libavfilter/Makefile +++ b/libavfilter/Makefile @@ -182,6 +182,7 @@ OBJS-$(CONFIG_PP7_FILTER) += vf_pp7.o OBJS-$(CONFIG_PSNR_FILTER) += vf_psnr.o dualinput.o framesync.o OBJS-$(CONFIG_PULLUP_FILTER) += vf_pullup.o OBJS-$(CONFIG_QP_FILTER) += vf_qp.o +OBJS-$(CONFIG_RANDOM_FILTER) += vf_random.o OBJS-$(CONFIG_REMOVEGRAIN_FILTER) += vf_removegrain.o OBJS-$(CONFIG_REMOVELOGO_FILTER) += bbox.o lswsutils.o lavfutils.o vf_removelogo.o OBJS-$(CONFIG_REPEATFIELDS_FILTER) += vf_repeatfields.o diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c index b0d8410..d1be38a 100644 --- a/libavfilter/allfilters.c +++ b/libavfilter/allfilters.c @@ -197,6 +197,7 @@ void avfilter_register_all(void) REGISTER_FILTER(PSNR, psnr, vf); REGISTER_FILTER(PULLUP, pullup, vf); REGISTER_FILTER(QP, qp, vf); + REGISTER_FILTER(RANDOM, random, vf); REGISTER_FILTER(REMOVEGRAIN, removegrain, vf); REGISTER_FILTER(REMOVELOGO, removelogo, vf); REGISTER_FILTER(REPEATFIELDS, repeatfields, vf); diff --git a/libavfilter/vf_random.c b/libavfilter/vf_random.c new file mode 100644 index 0000000..ce64915 --- /dev/null +++ b/libavfilter/vf_random.c @@ -0,0 +1,136 @@ +/* + * Copyright (c) 2015 Paul B Mahol + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "libavutil/opt.h" +#include "libavutil/lfg.h" +#include "avfilter.h" +#include "formats.h" +#include "internal.h" +#include "video.h" + +#define MAX_FRAMES 400 + +typedef struct RandomContext { + const AVClass *class; + + AVLFG lfg; + int nb_frames; + int nb_frames_filled; + AVFrame *frames[MAX_FRAMES]; + int64_t pts[MAX_FRAMES]; + int flush_idx; +} RandomContext; + +#define OFFSET(x) offsetof(RandomContext, x) +#define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM + +static const AVOption random_options[] = { + { "frames", "set number of frames in cache", OFFSET(nb_frames), AV_OPT_TYPE_INT, {.i64=30}, 2, MAX_FRAMES, FLAGS }, + { NULL } +}; + +AVFILTER_DEFINE_CLASS(random); + +static av_cold int init(AVFilterContext *ctx) +{ + RandomContext *s = ctx->priv; + + av_lfg_init(&s->lfg, 1337); + + return 0; +} + +static int config_output(AVFilterLink *outlink) +{ + outlink->flags |= FF_LINK_FLAG_REQUEST_LOOP; + return 0; +} + +static int filter_frame(AVFilterLink *inlink, AVFrame *in) +{ + AVFilterContext *ctx = inlink->dst; + RandomContext *s = ctx->priv; + AVFilterLink *outlink = ctx->outputs[0]; + AVFrame *out; + int idx; + + if (s->nb_frames_filled < s->nb_frames) { + s->frames[s->nb_frames_filled] = in; + s->pts[s->nb_frames_filled++] = in->pts; + return 0; + } + + idx = av_lfg_get(&s->lfg) % s->nb_frames; + + out = s->frames[idx]; + out->pts = s->pts[0]; + memmove(&s->pts[0], &s->pts[1], (s->nb_frames - 1) * sizeof(s->pts[0])); + s->frames[idx] = in; + s->pts[s->nb_frames - 1] = in->pts; + + return ff_filter_frame(outlink, out); +} + +static int request_frame(AVFilterLink *outlink) +{ + AVFilterContext *ctx = outlink->src; + RandomContext *s = ctx->priv; + int ret; + + ret = ff_request_frame(ctx->inputs[0]); + + if (ret == AVERROR_EOF && !ctx->is_disabled && s->nb_frames > 0) { + AVFrame *out = s->frames[s->nb_frames - 1]; + out->pts = s->pts[s->flush_idx++]; + ret = ff_filter_frame(outlink, out); + s->nb_frames--; + } + + return ret; +} + +static const AVFilterPad random_inputs[] = { + { + .name = "default", + .type = AVMEDIA_TYPE_VIDEO, + .filter_frame = filter_frame, + }, + { NULL } +}; + +static const AVFilterPad random_outputs[] = { + { + .name = "default", + .type = AVMEDIA_TYPE_VIDEO, + .request_frame = request_frame, + .config_props = config_output, + }, + { NULL } +}; + +AVFilter ff_vf_random = { + .name = "random", + .description = NULL_IF_CONFIG_SMALL("Flush frames in a random way."), + .priv_size = sizeof(RandomContext), + .priv_class = &random_class, + .init = init, + .inputs = random_inputs, + .outputs = random_outputs, +}; -- 1.7.11.2 _______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org http://ffmpeg.org/mailman/listinfo/ffmpeg-devel