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 static void check_size(AVFilterContext *ctx)
60 LoopContext *s = ctx->priv;
63 av_log(ctx, AV_LOG_WARNING, "Number of %s to loop is not set!\n",
64 ctx->input_pads[0].type == AVMEDIA_TYPE_VIDEO ? "frames" : "samples");
67 #if CONFIG_ALOOP_FILTER
69 static int aconfig_input(AVFilterLink *inlink)
71 AVFilterContext *ctx = inlink->dst;
72 LoopContext *s = ctx->priv;
74 s->fifo = av_audio_fifo_alloc(inlink->format, inlink->channels, 8192);
75 s->left = av_audio_fifo_alloc(inlink->format, inlink->channels, 8192);
76 if (!s->fifo || !s->left)
77 return AVERROR(ENOMEM);
84 static av_cold void auninit(AVFilterContext *ctx)
86 LoopContext *s = ctx->priv;
88 av_audio_fifo_free(s->fifo);
89 av_audio_fifo_free(s->left);
92 static int push_samples(AVFilterContext *ctx, int nb_samples)
94 AVFilterLink *outlink = ctx->outputs[0];
95 LoopContext *s = ctx->priv;
99 while (s->loop != 0 && i < nb_samples) {
100 out = ff_get_audio_buffer(outlink, FFMIN(nb_samples, s->nb_samples - s->current_sample));
102 return AVERROR(ENOMEM);
103 ret = av_audio_fifo_peek_at(s->fifo, (void **)out->extended_data, out->nb_samples, s->current_sample);
109 out->nb_samples = ret;
110 s->pts += av_rescale_q(out->nb_samples, (AVRational){1, outlink->sample_rate}, outlink->time_base);
111 i += out->nb_samples;
112 s->current_sample += out->nb_samples;
114 ret = ff_filter_frame(outlink, out);
118 if (s->current_sample >= s->nb_samples) {
119 s->current_sample = 0;
129 static int afilter_frame(AVFilterLink *inlink, AVFrame *frame)
131 AVFilterContext *ctx = inlink->dst;
132 AVFilterLink *outlink = ctx->outputs[0];
133 LoopContext *s = ctx->priv;
136 if (s->ignored_samples + frame->nb_samples > s->start && s->size > 0 && s->loop != 0) {
137 if (s->nb_samples < s->size) {
138 int written = FFMIN(frame->nb_samples, s->size - s->nb_samples);
141 ret = av_audio_fifo_write(s->fifo, (void **)frame->extended_data, written);
144 if (!s->nb_samples) {
145 drain = FFMAX(0, s->start - s->ignored_samples);
147 av_audio_fifo_drain(s->fifo, drain);
148 s->pts += av_rescale_q(s->start - s->ignored_samples, (AVRational){1, outlink->sample_rate}, outlink->time_base);
150 s->nb_samples += ret - drain;
151 drain = frame->nb_samples - written;
152 if (s->nb_samples == s->size && drain > 0) {
155 ret2 = av_audio_fifo_write(s->left, (void **)frame->extended_data, frame->nb_samples);
158 av_audio_fifo_drain(s->left, drain);
160 frame->nb_samples = ret;
161 s->pts += av_rescale_q(ret, (AVRational){1, outlink->sample_rate}, outlink->time_base);
162 ret = ff_filter_frame(outlink, frame);
164 int nb_samples = frame->nb_samples;
166 av_frame_free(&frame);
167 ret = push_samples(ctx, nb_samples);
170 s->ignored_samples += frame->nb_samples;
172 s->pts += av_rescale_q(frame->nb_samples, (AVRational){1, outlink->sample_rate}, outlink->time_base);
173 ret = ff_filter_frame(outlink, frame);
179 static int arequest_frame(AVFilterLink *outlink)
181 AVFilterContext *ctx = outlink->src;
182 LoopContext *s = ctx->priv;
186 (s->nb_samples < s->size) ||
187 (s->nb_samples >= s->size && s->loop == 0)) {
188 int nb_samples = av_audio_fifo_size(s->left);
190 if (s->loop == 0 && nb_samples > 0) {
193 out = ff_get_audio_buffer(outlink, nb_samples);
195 return AVERROR(ENOMEM);
196 av_audio_fifo_read(s->left, (void **)out->extended_data, nb_samples);
198 s->pts += av_rescale_q(nb_samples, (AVRational){1, outlink->sample_rate}, outlink->time_base);
199 ret = ff_filter_frame(outlink, out);
203 ret = ff_request_frame(ctx->inputs[0]);
205 ret = push_samples(ctx, 1024);
208 if (ret == AVERROR_EOF && s->nb_samples > 0 && s->loop != 0) {
209 ret = push_samples(ctx, outlink->sample_rate);
215 static const AVOption aloop_options[] = {
216 { "loop", "number of loops", OFFSET(loop), AV_OPT_TYPE_INT, {.i64 = 0 }, -1, INT_MAX, AFLAGS },
217 { "size", "max number of samples to loop", OFFSET(size), AV_OPT_TYPE_INT64, {.i64 = 0 }, 0, INT32_MAX, AFLAGS },
218 { "start", "set the loop start sample", OFFSET(start), AV_OPT_TYPE_INT64, {.i64 = 0 }, 0, INT64_MAX, AFLAGS },
222 AVFILTER_DEFINE_CLASS(aloop);
224 static const AVFilterPad ainputs[] = {
227 .type = AVMEDIA_TYPE_AUDIO,
228 .filter_frame = afilter_frame,
229 .config_props = aconfig_input,
234 static const AVFilterPad aoutputs[] = {
237 .type = AVMEDIA_TYPE_AUDIO,
238 .request_frame = arequest_frame,
243 AVFilter ff_af_aloop = {
245 .description = NULL_IF_CONFIG_SMALL("Loop audio samples."),
246 .priv_size = sizeof(LoopContext),
247 .priv_class = &aloop_class,
252 #endif /* CONFIG_ALOOP_FILTER */
254 #if CONFIG_LOOP_FILTER
256 static av_cold int init(AVFilterContext *ctx)
258 LoopContext *s = ctx->priv;
260 s->frames = av_calloc(s->size, sizeof(*s->frames));
262 return AVERROR(ENOMEM);
269 static av_cold void uninit(AVFilterContext *ctx)
271 LoopContext *s = ctx->priv;
274 for (i = 0; i < s->nb_frames; i++)
275 av_frame_free(&s->frames[i]);
277 av_freep(&s->frames);
281 static int push_frame(AVFilterContext *ctx)
283 AVFilterLink *outlink = ctx->outputs[0];
284 LoopContext *s = ctx->priv;
285 int64_t pts, duration;
288 AVFrame *out = av_frame_clone(s->frames[s->current_frame]);
291 return AVERROR(ENOMEM);
292 out->pts += s->duration - s->start_pts;
293 if (out->pkt_duration)
294 duration = out->pkt_duration;
296 duration = av_rescale_q(1, av_inv_q(outlink->frame_rate), outlink->time_base);
297 pts = out->pts + duration;
298 ret = ff_filter_frame(outlink, out);
301 if (s->current_frame >= s->nb_frames) {
303 s->current_frame = 0;
312 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
314 AVFilterContext *ctx = inlink->dst;
315 AVFilterLink *outlink = ctx->outputs[0];
316 LoopContext *s = ctx->priv;
320 if (inlink->frame_count_out >= s->start && s->size > 0 && s->loop != 0) {
321 if (s->nb_frames < s->size) {
323 s->start_pts = frame->pts;
324 s->frames[s->nb_frames] = av_frame_clone(frame);
325 if (!s->frames[s->nb_frames]) {
326 av_frame_free(&frame);
327 return AVERROR(ENOMEM);
330 if (frame->pkt_duration)
331 duration = frame->pkt_duration;
333 duration = av_rescale_q(1, av_inv_q(outlink->frame_rate), outlink->time_base);
334 s->duration = frame->pts + duration;
335 ret = ff_filter_frame(outlink, frame);
337 av_frame_free(&frame);
338 ret = push_frame(ctx);
341 frame->pts += s->duration;
342 ret = ff_filter_frame(outlink, frame);
348 static int activate(AVFilterContext *ctx)
350 AVFilterLink *inlink = ctx->inputs[0];
351 AVFilterLink *outlink = ctx->outputs[0];
352 LoopContext *s = ctx->priv;
353 AVFrame *frame = NULL;
357 FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
359 if (!s->eof && (s->nb_frames < s->size || !s->loop || !s->size)) {
360 ret = ff_inlink_consume_frame(inlink, &frame);
364 return filter_frame(inlink, frame);
367 if (!s->eof && ff_inlink_acknowledge_status(inlink, &status, &pts)) {
368 if (status == AVERROR_EOF) {
369 s->size = s->nb_frames;
374 if (s->eof && (!s->loop || !s->size)) {
375 ff_outlink_set_status(outlink, AVERROR_EOF, s->duration);
379 if (!s->eof && (!s->size ||
380 (s->nb_frames < s->size) ||
381 (s->nb_frames >= s->size && s->loop == 0))) {
382 FF_FILTER_FORWARD_WANTED(outlink, inlink);
383 } else if (s->loop && s->nb_frames == s->size) {
384 return push_frame(ctx);
387 return FFERROR_NOT_READY;
390 static const AVOption loop_options[] = {
391 { "loop", "number of loops", OFFSET(loop), AV_OPT_TYPE_INT, {.i64 = 0 }, -1, INT_MAX, VFLAGS },
392 { "size", "max number of frames to loop", OFFSET(size), AV_OPT_TYPE_INT64, {.i64 = 0 }, 0, INT16_MAX, VFLAGS },
393 { "start", "set the loop start frame", OFFSET(start), AV_OPT_TYPE_INT64, {.i64 = 0 }, 0, INT64_MAX, VFLAGS },
397 AVFILTER_DEFINE_CLASS(loop);
399 static const AVFilterPad inputs[] = {
402 .type = AVMEDIA_TYPE_VIDEO,
407 static const AVFilterPad outputs[] = {
410 .type = AVMEDIA_TYPE_VIDEO,
415 AVFilter ff_vf_loop = {
417 .description = NULL_IF_CONFIG_SMALL("Loop video frames."),
418 .priv_size = sizeof(LoopContext),
419 .priv_class = &loop_class,
422 .activate = activate,
426 #endif /* CONFIG_LOOP_FILTER */