2 * Copyright (c) 2016 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/audio_fifo.h"
22 #include "libavutil/avassert.h"
23 #include "libavutil/fifo.h"
24 #include "libavutil/internal.h"
25 #include "libavutil/opt.h"
32 typedef struct LoopContext {
42 int64_t current_sample;
44 int64_t ignored_samples;
52 #define AFLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
53 #define VFLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
54 #define OFFSET(x) offsetof(LoopContext, x)
56 #if CONFIG_ALOOP_FILTER
58 static int aconfig_input(AVFilterLink *inlink)
60 AVFilterContext *ctx = inlink->dst;
61 LoopContext *s = ctx->priv;
63 s->fifo = av_audio_fifo_alloc(inlink->format, inlink->channels, 8192);
64 s->left = av_audio_fifo_alloc(inlink->format, inlink->channels, 8192);
65 if (!s->fifo || !s->left)
66 return AVERROR(ENOMEM);
71 static av_cold void auninit(AVFilterContext *ctx)
73 LoopContext *s = ctx->priv;
75 av_audio_fifo_free(s->fifo);
76 av_audio_fifo_free(s->left);
79 static int push_samples(AVFilterContext *ctx, int nb_samples)
81 AVFilterLink *outlink = ctx->outputs[0];
82 LoopContext *s = ctx->priv;
86 while (s->loop != 0 && i < nb_samples) {
87 out = ff_get_audio_buffer(outlink, FFMIN(nb_samples, s->nb_samples - s->current_sample));
89 return AVERROR(ENOMEM);
90 ret = av_audio_fifo_peek_at(s->fifo, (void **)out->extended_data, out->nb_samples, s->current_sample);
94 out->nb_samples = ret;
95 s->pts += out->nb_samples;
97 s->current_sample += out->nb_samples;
99 ret = ff_filter_frame(outlink, out);
103 if (s->current_sample >= s->nb_samples) {
104 s->current_sample = 0;
114 static int afilter_frame(AVFilterLink *inlink, AVFrame *frame)
116 AVFilterContext *ctx = inlink->dst;
117 AVFilterLink *outlink = ctx->outputs[0];
118 LoopContext *s = ctx->priv;
121 if (s->ignored_samples + frame->nb_samples > s->start && s->size > 0 && s->loop != 0) {
122 if (s->nb_samples < s->size) {
123 int written = FFMIN(frame->nb_samples, s->size - s->nb_samples);
126 ret = av_audio_fifo_write(s->fifo, (void **)frame->extended_data, written);
129 if (!s->nb_samples) {
130 drain = FFMAX(0, s->start - s->ignored_samples);
132 av_audio_fifo_drain(s->fifo, drain);
133 s->pts += s->start - s->ignored_samples;
135 s->nb_samples += ret - drain;
136 drain = frame->nb_samples - written;
137 if (s->nb_samples == s->size && drain > 0) {
140 ret2 = av_audio_fifo_write(s->left, (void **)frame->extended_data, frame->nb_samples);
143 av_audio_fifo_drain(s->left, drain);
145 frame->nb_samples = ret;
147 ret = ff_filter_frame(outlink, frame);
149 int nb_samples = frame->nb_samples;
151 av_frame_free(&frame);
152 ret = push_samples(ctx, nb_samples);
155 s->ignored_samples += frame->nb_samples;
157 s->pts += frame->nb_samples;
158 ret = ff_filter_frame(outlink, frame);
164 static int arequest_frame(AVFilterLink *outlink)
166 AVFilterContext *ctx = outlink->src;
167 LoopContext *s = ctx->priv;
171 (s->nb_samples < s->size) ||
172 (s->nb_samples >= s->size && s->loop == 0)) {
173 int nb_samples = av_audio_fifo_size(s->left);
175 if (s->loop == 0 && nb_samples > 0) {
178 out = ff_get_audio_buffer(outlink, nb_samples);
180 return AVERROR(ENOMEM);
181 av_audio_fifo_read(s->left, (void **)out->extended_data, nb_samples);
183 s->pts += nb_samples;
184 ret = ff_filter_frame(outlink, out);
188 ret = ff_request_frame(ctx->inputs[0]);
190 ret = push_samples(ctx, 1024);
193 if (ret == AVERROR_EOF && s->nb_samples > 0 && s->loop != 0) {
194 ret = push_samples(ctx, outlink->sample_rate);
200 static const AVOption aloop_options[] = {
201 { "loop", "number of loops", OFFSET(loop), AV_OPT_TYPE_INT, {.i64 = 0 }, -1, INT_MAX, AFLAGS },
202 { "size", "max number of samples to loop", OFFSET(size), AV_OPT_TYPE_INT64, {.i64 = 0 }, 0, INT32_MAX, AFLAGS },
203 { "start", "set the loop start sample", OFFSET(start), AV_OPT_TYPE_INT64, {.i64 = 0 }, 0, INT64_MAX, AFLAGS },
207 AVFILTER_DEFINE_CLASS(aloop);
209 static const AVFilterPad ainputs[] = {
212 .type = AVMEDIA_TYPE_AUDIO,
213 .filter_frame = afilter_frame,
214 .config_props = aconfig_input,
219 static const AVFilterPad aoutputs[] = {
222 .type = AVMEDIA_TYPE_AUDIO,
223 .request_frame = arequest_frame,
228 AVFilter ff_af_aloop = {
230 .description = NULL_IF_CONFIG_SMALL("Loop audio samples."),
231 .priv_size = sizeof(LoopContext),
232 .priv_class = &aloop_class,
234 .query_formats = ff_query_formats_all,
238 #endif /* CONFIG_ALOOP_FILTER */
240 #if CONFIG_LOOP_FILTER
242 static av_cold int init(AVFilterContext *ctx)
244 LoopContext *s = ctx->priv;
246 s->frames = av_calloc(s->size, sizeof(*s->frames));
248 return AVERROR(ENOMEM);
253 static av_cold void uninit(AVFilterContext *ctx)
255 LoopContext *s = ctx->priv;
258 for (i = 0; i < s->nb_frames; i++)
259 av_frame_free(&s->frames[i]);
261 av_freep(&s->frames);
265 static int push_frame(AVFilterContext *ctx)
267 AVFilterLink *outlink = ctx->outputs[0];
268 LoopContext *s = ctx->priv;
272 AVFrame *out = av_frame_clone(s->frames[s->current_frame]);
275 return AVERROR(ENOMEM);
276 out->pts += s->duration - s->start_pts;
277 pts = out->pts + av_frame_get_pkt_duration(out);
278 ret = ff_filter_frame(outlink, out);
281 if (s->current_frame >= s->nb_frames) {
283 s->current_frame = 0;
292 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
294 AVFilterContext *ctx = inlink->dst;
295 AVFilterLink *outlink = ctx->outputs[0];
296 LoopContext *s = ctx->priv;
299 if (inlink->frame_count >= s->start && s->size > 0 && s->loop != 0) {
300 if (s->nb_frames < s->size) {
302 s->start_pts = frame->pts;
303 s->frames[s->nb_frames] = av_frame_clone(frame);
304 if (!s->frames[s->nb_frames]) {
305 av_frame_free(&frame);
306 return AVERROR(ENOMEM);
309 s->duration = frame->pts + av_frame_get_pkt_duration(frame);
310 ret = ff_filter_frame(outlink, frame);
312 av_frame_free(&frame);
313 ret = push_frame(ctx);
316 frame->pts += s->duration;
317 ret = ff_filter_frame(outlink, frame);
323 static int request_frame(AVFilterLink *outlink)
325 AVFilterContext *ctx = outlink->src;
326 LoopContext *s = ctx->priv;
330 (s->nb_frames < s->size) ||
331 (s->nb_frames >= s->size && s->loop == 0)) {
332 ret = ff_request_frame(ctx->inputs[0]);
334 ret = push_frame(ctx);
337 if (ret == AVERROR_EOF && s->nb_frames > 0 && s->loop != 0) {
338 ret = push_frame(ctx);
344 static const AVOption loop_options[] = {
345 { "loop", "number of loops", OFFSET(loop), AV_OPT_TYPE_INT, {.i64 = 0 }, -1, INT_MAX, VFLAGS },
346 { "size", "max number of frames to loop", OFFSET(size), AV_OPT_TYPE_INT64, {.i64 = 0 }, 0, INT16_MAX, VFLAGS },
347 { "start", "set the loop start frame", OFFSET(start), AV_OPT_TYPE_INT64, {.i64 = 0 }, 0, INT64_MAX, VFLAGS },
351 AVFILTER_DEFINE_CLASS(loop);
353 static const AVFilterPad inputs[] = {
356 .type = AVMEDIA_TYPE_VIDEO,
357 .filter_frame = filter_frame,
362 static const AVFilterPad outputs[] = {
365 .type = AVMEDIA_TYPE_VIDEO,
366 .request_frame = request_frame,
371 AVFilter ff_vf_loop = {
373 .description = NULL_IF_CONFIG_SMALL("Loop video frames."),
374 .priv_size = sizeof(LoopContext),
375 .priv_class = &loop_class,
381 #endif /* CONFIG_LOOP_FILTER */