Hi, patch attached.
From 1fd9636e9040b2a6b321cdd16ebd392c8db2de1b Mon Sep 17 00:00:00 2001 From: Paul B Mahol <one...@gmail.com> Date: Thu, 11 Feb 2016 22:05:54 +0100 Subject: [PATCH] avfilter: add loop filters
Signed-off-by: Paul B Mahol <one...@gmail.com> --- libavfilter/Makefile | 2 + libavfilter/allfilters.c | 2 + libavfilter/f_loop.c | 220 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 224 insertions(+) create mode 100644 libavfilter/f_loop.c diff --git a/libavfilter/Makefile b/libavfilter/Makefile index 8916588..35ac53a 100644 --- a/libavfilter/Makefile +++ b/libavfilter/Makefile @@ -38,6 +38,7 @@ OBJS-$(CONFIG_AGATE_FILTER) += af_agate.o OBJS-$(CONFIG_AINTERLEAVE_FILTER) += f_interleave.o OBJS-$(CONFIG_ALIMITER_FILTER) += af_alimiter.o OBJS-$(CONFIG_ALLPASS_FILTER) += af_biquads.o +OBJS-$(CONFIG_ALOOP_FILTER) += f_loop.o OBJS-$(CONFIG_AMERGE_FILTER) += af_amerge.o OBJS-$(CONFIG_AMETADATA_FILTER) += f_metadata.o OBJS-$(CONFIG_AMIX_FILTER) += af_amix.o @@ -180,6 +181,7 @@ OBJS-$(CONFIG_INTERLACE_FILTER) += vf_interlace.o OBJS-$(CONFIG_INTERLEAVE_FILTER) += f_interleave.o OBJS-$(CONFIG_KERNDEINT_FILTER) += vf_kerndeint.o OBJS-$(CONFIG_LENSCORRECTION_FILTER) += vf_lenscorrection.o +OBJS-$(CONFIG_LOOP_FILTER) += f_loop.o OBJS-$(CONFIG_LUT3D_FILTER) += vf_lut3d.o OBJS-$(CONFIG_LUT_FILTER) += vf_lut.o OBJS-$(CONFIG_LUTRGB_FILTER) += vf_lut.o diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c index fa7d304..6331fe5 100644 --- a/libavfilter/allfilters.c +++ b/libavfilter/allfilters.c @@ -58,6 +58,7 @@ void avfilter_register_all(void) REGISTER_FILTER(AINTERLEAVE, ainterleave, af); REGISTER_FILTER(ALIMITER, alimiter, af); REGISTER_FILTER(ALLPASS, allpass, af); + REGISTER_FILTER(ALOOP, aloop, af); REGISTER_FILTER(AMERGE, amerge, af); REGISTER_FILTER(AMETADATA, ametadata, af); REGISTER_FILTER(AMIX, amix, af); @@ -201,6 +202,7 @@ void avfilter_register_all(void) REGISTER_FILTER(INTERLEAVE, interleave, vf); REGISTER_FILTER(KERNDEINT, kerndeint, vf); REGISTER_FILTER(LENSCORRECTION, lenscorrection, vf); + REGISTER_FILTER(LOOP, loop, vf); REGISTER_FILTER(LUT3D, lut3d, vf); REGISTER_FILTER(LUT, lut, vf); REGISTER_FILTER(LUTRGB, lutrgb, vf); diff --git a/libavfilter/f_loop.c b/libavfilter/f_loop.c new file mode 100644 index 0000000..beef4fc --- /dev/null +++ b/libavfilter/f_loop.c @@ -0,0 +1,220 @@ +/* + * Copyright (c) 2016 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/avassert.h" +#include "libavutil/fifo.h" +#include "libavutil/internal.h" +#include "libavutil/opt.h" +#include "avfilter.h" +#include "audio.h" +#include "formats.h" +#include "internal.h" +#include "video.h" + +typedef struct LoopContext { + const AVClass *class; + + AVFrame **frames; + int nb_frames; + int current_frame; + int64_t start_pts; + int64_t duration; + + int loop; + int size; + int64_t start; +} LoopContext; + +#define OFFSET(x) offsetof(LoopContext, x) +#define DEFINE_OPTIONS(filt_name, FLAGS) \ +static const AVOption filt_name##_options[] = { \ + { "loop", "number of loops", OFFSET(loop), AV_OPT_TYPE_INT, {.i64 = 0 }, -1, INT_MAX, FLAGS }, \ + { "size", "max number of frames to loop", OFFSET(size), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, INT16_MAX, FLAGS }, \ + { "start", "set the loop start frame", OFFSET(start), AV_OPT_TYPE_INT64, {.i64 = 0 }, 0, INT64_MAX, FLAGS }, \ + { NULL } \ +} + +static av_cold int init(AVFilterContext *ctx) +{ + LoopContext *s = ctx->priv; + + s->frames = av_calloc(s->size, sizeof(*s->frames)); + if (!s->frames) + return AVERROR(ENOMEM); + + return 0; +} + +static av_cold void uninit(AVFilterContext *ctx) +{ + LoopContext *s = ctx->priv; + int i; + + for (i = 0; i < s->nb_frames; i++) + av_frame_free(&s->frames[i]); + + av_freep(&s->frames); + s->nb_frames = 0; +} + +static int push_frame(AVFilterContext *ctx) +{ + AVFilterLink *outlink = ctx->outputs[0]; + LoopContext *s = ctx->priv; + int64_t pts; + int ret; + + AVFrame *out = av_frame_clone(s->frames[s->current_frame]); + + if (!out) + return AVERROR(ENOMEM); + out->pts += s->duration - s->start_pts; + pts = out->pts + out->pkt_duration; + ret = ff_filter_frame(outlink, out); + s->current_frame++; + + if (s->current_frame >= s->nb_frames) { + s->duration = pts; + s->current_frame = 0; + + if (s->loop > 0) + s->loop--; + } + + return ret; +} + +static int filter_frame(AVFilterLink *inlink, AVFrame *frame) +{ + AVFilterContext *ctx = inlink->dst; + AVFilterLink *outlink = ctx->outputs[0]; + LoopContext *s = ctx->priv; + int ret = 0; + + if (inlink->frame_count >= s->start && s->size > 0 && s->loop != 0) { + if (s->nb_frames < s->size) { + if (!s->nb_frames) + s->start_pts = frame->pts; + s->frames[s->nb_frames] = av_frame_clone(frame); + if (!s->frames[s->nb_frames]) { + av_frame_free(&frame); + return AVERROR(ENOMEM); + } + s->nb_frames++; + s->duration = frame->pts + frame->pkt_duration; + ret = ff_filter_frame(outlink, frame); + } else { + av_frame_free(&frame); + ret = push_frame(ctx); + } + } else { + ret = ff_filter_frame(outlink, frame); + } + + return ret; +} + +static int request_frame(AVFilterLink *outlink) +{ + AVFilterContext *ctx = outlink->src; + LoopContext *s = ctx->priv; + int ret; + + ret = ff_request_frame(ctx->inputs[0]); + + if (ret == AVERROR_EOF && s->nb_frames > 0 && s->loop != 0) { + ret = push_frame(ctx); + } + + return ret; +} + +#if CONFIG_ALOOP_FILTER + +DEFINE_OPTIONS(aloop, AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM); +AVFILTER_DEFINE_CLASS(aloop); + +static const AVFilterPad ainputs[] = { + { + .name = "default", + .type = AVMEDIA_TYPE_AUDIO, + .filter_frame = filter_frame, + }, + { NULL } +}; + +static const AVFilterPad aoutputs[] = { + { + .name = "default", + .type = AVMEDIA_TYPE_AUDIO, + .request_frame = request_frame, + }, + { NULL } +}; + +AVFilter ff_af_aloop = { + .name = "aloop", + .description = NULL_IF_CONFIG_SMALL("Loop audio frames."), + .priv_size = sizeof(LoopContext), + .priv_class = &aloop_class, + .init = init, + .uninit = uninit, + .query_formats = ff_query_formats_all, + .inputs = ainputs, + .outputs = aoutputs, + .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC, +}; +#endif /* CONFIG_ALOOP_FILTER */ + +#if CONFIG_LOOP_FILTER + +DEFINE_OPTIONS(loop, AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM); +AVFILTER_DEFINE_CLASS(loop); + +static const AVFilterPad inputs[] = { + { + .name = "default", + .type = AVMEDIA_TYPE_VIDEO, + .filter_frame = filter_frame, + }, + { NULL } +}; + +static const AVFilterPad outputs[] = { + { + .name = "default", + .type = AVMEDIA_TYPE_VIDEO, + .request_frame = request_frame, + }, + { NULL } +}; + +AVFilter ff_vf_loop = { + .name = "loop", + .description = NULL_IF_CONFIG_SMALL("Loop video frames."), + .priv_size = sizeof(LoopContext), + .priv_class = &loop_class, + .init = init, + .uninit = uninit, + .inputs = inputs, + .outputs = outputs, + .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC, +}; +#endif /* CONFIG_LOOP_FILTER */ -- 1.9.1
_______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org http://ffmpeg.org/mailman/listinfo/ffmpeg-devel