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"
33 typedef struct LoopContext {
43 int64_t current_sample;
45 int64_t ignored_samples;
54 #define AFLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
55 #define VFLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
56 #define OFFSET(x) offsetof(LoopContext, x)
58 #if CONFIG_ALOOP_FILTER
60 static int aconfig_input(AVFilterLink *inlink)
62 AVFilterContext *ctx = inlink->dst;
63 LoopContext *s = ctx->priv;
65 s->fifo = av_audio_fifo_alloc(inlink->format, inlink->channels, 8192);
66 s->left = av_audio_fifo_alloc(inlink->format, inlink->channels, 8192);
67 if (!s->fifo || !s->left)
68 return AVERROR(ENOMEM);
73 static av_cold void auninit(AVFilterContext *ctx)
75 LoopContext *s = ctx->priv;
77 av_audio_fifo_free(s->fifo);
78 av_audio_fifo_free(s->left);
81 static int push_samples(AVFilterContext *ctx, int nb_samples)
83 AVFilterLink *outlink = ctx->outputs[0];
84 LoopContext *s = ctx->priv;
88 while (s->loop != 0 && i < nb_samples) {
89 out = ff_get_audio_buffer(outlink, FFMIN(nb_samples, s->nb_samples - s->current_sample));
91 return AVERROR(ENOMEM);
92 ret = av_audio_fifo_peek_at(s->fifo, (void **)out->extended_data, out->nb_samples, s->current_sample);
98 out->nb_samples = ret;
99 s->pts += out->nb_samples;
100 i += out->nb_samples;
101 s->current_sample += out->nb_samples;
103 ret = ff_filter_frame(outlink, out);
107 if (s->current_sample >= s->nb_samples) {
108 s->current_sample = 0;
118 static int afilter_frame(AVFilterLink *inlink, AVFrame *frame)
120 AVFilterContext *ctx = inlink->dst;
121 AVFilterLink *outlink = ctx->outputs[0];
122 LoopContext *s = ctx->priv;
125 if (s->ignored_samples + frame->nb_samples > s->start && s->size > 0 && s->loop != 0) {
126 if (s->nb_samples < s->size) {
127 int written = FFMIN(frame->nb_samples, s->size - s->nb_samples);
130 ret = av_audio_fifo_write(s->fifo, (void **)frame->extended_data, written);
133 if (!s->nb_samples) {
134 drain = FFMAX(0, s->start - s->ignored_samples);
136 av_audio_fifo_drain(s->fifo, drain);
137 s->pts += s->start - s->ignored_samples;
139 s->nb_samples += ret - drain;
140 drain = frame->nb_samples - written;
141 if (s->nb_samples == s->size && drain > 0) {
144 ret2 = av_audio_fifo_write(s->left, (void **)frame->extended_data, frame->nb_samples);
147 av_audio_fifo_drain(s->left, drain);
149 frame->nb_samples = ret;
151 ret = ff_filter_frame(outlink, frame);
153 int nb_samples = frame->nb_samples;
155 av_frame_free(&frame);
156 ret = push_samples(ctx, nb_samples);
159 s->ignored_samples += frame->nb_samples;
161 s->pts += frame->nb_samples;
162 ret = ff_filter_frame(outlink, frame);
168 static int arequest_frame(AVFilterLink *outlink)
170 AVFilterContext *ctx = outlink->src;
171 LoopContext *s = ctx->priv;
175 (s->nb_samples < s->size) ||
176 (s->nb_samples >= s->size && s->loop == 0)) {
177 int nb_samples = av_audio_fifo_size(s->left);
179 if (s->loop == 0 && nb_samples > 0) {
182 out = ff_get_audio_buffer(outlink, nb_samples);
184 return AVERROR(ENOMEM);
185 av_audio_fifo_read(s->left, (void **)out->extended_data, nb_samples);
187 s->pts += nb_samples;
188 ret = ff_filter_frame(outlink, out);
192 ret = ff_request_frame(ctx->inputs[0]);
194 ret = push_samples(ctx, 1024);
197 if (ret == AVERROR_EOF && s->nb_samples > 0 && s->loop != 0) {
198 ret = push_samples(ctx, outlink->sample_rate);
204 static const AVOption aloop_options[] = {
205 { "loop", "number of loops", OFFSET(loop), AV_OPT_TYPE_INT, {.i64 = 0 }, -1, INT_MAX, AFLAGS },
206 { "size", "max number of samples to loop", OFFSET(size), AV_OPT_TYPE_INT64, {.i64 = 0 }, 0, INT32_MAX, AFLAGS },
207 { "start", "set the loop start sample", OFFSET(start), AV_OPT_TYPE_INT64, {.i64 = 0 }, 0, INT64_MAX, AFLAGS },
211 AVFILTER_DEFINE_CLASS(aloop);
213 static const AVFilterPad ainputs[] = {
216 .type = AVMEDIA_TYPE_AUDIO,
217 .filter_frame = afilter_frame,
218 .config_props = aconfig_input,
223 static const AVFilterPad aoutputs[] = {
226 .type = AVMEDIA_TYPE_AUDIO,
227 .request_frame = arequest_frame,
232 AVFilter ff_af_aloop = {
234 .description = NULL_IF_CONFIG_SMALL("Loop audio samples."),
235 .priv_size = sizeof(LoopContext),
236 .priv_class = &aloop_class,
241 #endif /* CONFIG_ALOOP_FILTER */
243 #if CONFIG_LOOP_FILTER
245 static av_cold int init(AVFilterContext *ctx)
247 LoopContext *s = ctx->priv;
249 s->frames = av_calloc(s->size, sizeof(*s->frames));
251 return AVERROR(ENOMEM);
256 static av_cold void uninit(AVFilterContext *ctx)
258 LoopContext *s = ctx->priv;
261 for (i = 0; i < s->nb_frames; i++)
262 av_frame_free(&s->frames[i]);
264 av_freep(&s->frames);
268 static int push_frame(AVFilterContext *ctx)
270 AVFilterLink *outlink = ctx->outputs[0];
271 LoopContext *s = ctx->priv;
272 int64_t pts, duration;
275 AVFrame *out = av_frame_clone(s->frames[s->current_frame]);
278 return AVERROR(ENOMEM);
279 out->pts += s->duration - s->start_pts;
280 if (out->pkt_duration)
281 duration = out->pkt_duration;
283 duration = av_rescale_q(1, av_inv_q(outlink->frame_rate), outlink->time_base);
284 pts = out->pts + duration;
285 ret = ff_filter_frame(outlink, out);
288 if (s->current_frame >= s->nb_frames) {
290 s->current_frame = 0;
299 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
301 AVFilterContext *ctx = inlink->dst;
302 AVFilterLink *outlink = ctx->outputs[0];
303 LoopContext *s = ctx->priv;
307 if (inlink->frame_count_out >= s->start && s->size > 0 && s->loop != 0) {
308 if (s->nb_frames < s->size) {
310 s->start_pts = frame->pts;
311 s->frames[s->nb_frames] = av_frame_clone(frame);
312 if (!s->frames[s->nb_frames]) {
313 av_frame_free(&frame);
314 return AVERROR(ENOMEM);
317 if (frame->pkt_duration)
318 duration = frame->pkt_duration;
320 duration = av_rescale_q(1, av_inv_q(outlink->frame_rate), outlink->time_base);
321 s->duration = frame->pts + duration;
322 ret = ff_filter_frame(outlink, frame);
324 av_frame_free(&frame);
325 ret = push_frame(ctx);
328 frame->pts += s->duration;
329 ret = ff_filter_frame(outlink, frame);
335 static int activate(AVFilterContext *ctx)
337 AVFilterLink *inlink = ctx->inputs[0];
338 AVFilterLink *outlink = ctx->outputs[0];
339 LoopContext *s = ctx->priv;
340 AVFrame *frame = NULL;
344 FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
346 if (!s->eof && (s->nb_frames < s->size || !s->loop)) {
347 ret = ff_inlink_consume_frame(inlink, &frame);
351 return filter_frame(inlink, frame);
354 if (!s->eof && ff_inlink_acknowledge_status(inlink, &status, &pts)) {
355 if (status == AVERROR_EOF)
359 if (s->eof && (s->loop == 0 || s->nb_frames < s->size)) {
360 ff_outlink_set_status(outlink, AVERROR_EOF, s->duration);
364 if (!s->eof && (!s->size ||
365 (s->nb_frames < s->size) ||
366 (s->nb_frames >= s->size && s->loop == 0))) {
367 FF_FILTER_FORWARD_WANTED(outlink, inlink);
368 } else if (s->loop && s->nb_frames == s->size) {
369 return push_frame(ctx);
372 return FFERROR_NOT_READY;
375 static const AVOption loop_options[] = {
376 { "loop", "number of loops", OFFSET(loop), AV_OPT_TYPE_INT, {.i64 = 0 }, -1, INT_MAX, VFLAGS },
377 { "size", "max number of frames to loop", OFFSET(size), AV_OPT_TYPE_INT64, {.i64 = 0 }, 0, INT16_MAX, VFLAGS },
378 { "start", "set the loop start frame", OFFSET(start), AV_OPT_TYPE_INT64, {.i64 = 0 }, 0, INT64_MAX, VFLAGS },
382 AVFILTER_DEFINE_CLASS(loop);
384 static const AVFilterPad inputs[] = {
387 .type = AVMEDIA_TYPE_VIDEO,
392 static const AVFilterPad outputs[] = {
395 .type = AVMEDIA_TYPE_VIDEO,
400 AVFilter ff_vf_loop = {
402 .description = NULL_IF_CONFIG_SMALL("Loop video frames."),
403 .priv_size = sizeof(LoopContext),
404 .priv_class = &loop_class,
407 .activate = activate,
411 #endif /* CONFIG_LOOP_FILTER */