summaryrefslogtreecommitdiff
path: root/libavfilter/f_loop.c (plain)
blob: 5a3280772e12271f43085d04d1a26cf670c84bf6
1/*
2 * Copyright (c) 2016 Paul B Mahol
3 *
4 * This file is part of FFmpeg.
5 *
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.
10 *
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.
15 *
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
19 */
20
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"
26#include "avfilter.h"
27#include "audio.h"
28#include "formats.h"
29#include "internal.h"
30#include "video.h"
31
32typedef struct LoopContext {
33 const AVClass *class;
34
35 AVAudioFifo *fifo;
36 AVAudioFifo *left;
37 AVFrame **frames;
38 int nb_frames;
39 int current_frame;
40 int64_t start_pts;
41 int64_t duration;
42 int64_t current_sample;
43 int64_t nb_samples;
44 int64_t ignored_samples;
45
46 int loop;
47 int64_t size;
48 int64_t start;
49 int64_t pts;
50} LoopContext;
51
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)
55
56#if CONFIG_ALOOP_FILTER
57
58static int aconfig_input(AVFilterLink *inlink)
59{
60 AVFilterContext *ctx = inlink->dst;
61 LoopContext *s = ctx->priv;
62
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);
67
68 return 0;
69}
70
71static av_cold void auninit(AVFilterContext *ctx)
72{
73 LoopContext *s = ctx->priv;
74
75 av_audio_fifo_free(s->fifo);
76 av_audio_fifo_free(s->left);
77}
78
79static int push_samples(AVFilterContext *ctx, int nb_samples)
80{
81 AVFilterLink *outlink = ctx->outputs[0];
82 LoopContext *s = ctx->priv;
83 AVFrame *out;
84 int ret, i = 0;
85
86 while (s->loop != 0 && i < nb_samples) {
87 out = ff_get_audio_buffer(outlink, FFMIN(nb_samples, s->nb_samples - s->current_sample));
88 if (!out)
89 return AVERROR(ENOMEM);
90 ret = av_audio_fifo_peek_at(s->fifo, (void **)out->extended_data, out->nb_samples, s->current_sample);
91 if (ret < 0) {
92 av_frame_free(&out);
93 return ret;
94 }
95 out->pts = s->pts;
96 out->nb_samples = ret;
97 s->pts += out->nb_samples;
98 i += out->nb_samples;
99 s->current_sample += out->nb_samples;
100
101 ret = ff_filter_frame(outlink, out);
102 if (ret < 0)
103 return ret;
104
105 if (s->current_sample >= s->nb_samples) {
106 s->current_sample = 0;
107
108 if (s->loop > 0)
109 s->loop--;
110 }
111 }
112
113 return ret;
114}
115
116static int afilter_frame(AVFilterLink *inlink, AVFrame *frame)
117{
118 AVFilterContext *ctx = inlink->dst;
119 AVFilterLink *outlink = ctx->outputs[0];
120 LoopContext *s = ctx->priv;
121 int ret = 0;
122
123 if (s->ignored_samples + frame->nb_samples > s->start && s->size > 0 && s->loop != 0) {
124 if (s->nb_samples < s->size) {
125 int written = FFMIN(frame->nb_samples, s->size - s->nb_samples);
126 int drain = 0;
127
128 ret = av_audio_fifo_write(s->fifo, (void **)frame->extended_data, written);
129 if (ret < 0)
130 return ret;
131 if (!s->nb_samples) {
132 drain = FFMAX(0, s->start - s->ignored_samples);
133 s->pts = frame->pts;
134 av_audio_fifo_drain(s->fifo, drain);
135 s->pts += s->start - s->ignored_samples;
136 }
137 s->nb_samples += ret - drain;
138 drain = frame->nb_samples - written;
139 if (s->nb_samples == s->size && drain > 0) {
140 int ret2;
141
142 ret2 = av_audio_fifo_write(s->left, (void **)frame->extended_data, frame->nb_samples);
143 if (ret2 < 0)
144 return ret2;
145 av_audio_fifo_drain(s->left, drain);
146 }
147 frame->nb_samples = ret;
148 s->pts += ret;
149 ret = ff_filter_frame(outlink, frame);
150 } else {
151 int nb_samples = frame->nb_samples;
152
153 av_frame_free(&frame);
154 ret = push_samples(ctx, nb_samples);
155 }
156 } else {
157 s->ignored_samples += frame->nb_samples;
158 frame->pts = s->pts;
159 s->pts += frame->nb_samples;
160 ret = ff_filter_frame(outlink, frame);
161 }
162
163 return ret;
164}
165
166static int arequest_frame(AVFilterLink *outlink)
167{
168 AVFilterContext *ctx = outlink->src;
169 LoopContext *s = ctx->priv;
170 int ret = 0;
171
172 if ((!s->size) ||
173 (s->nb_samples < s->size) ||
174 (s->nb_samples >= s->size && s->loop == 0)) {
175 int nb_samples = av_audio_fifo_size(s->left);
176
177 if (s->loop == 0 && nb_samples > 0) {
178 AVFrame *out;
179
180 out = ff_get_audio_buffer(outlink, nb_samples);
181 if (!out)
182 return AVERROR(ENOMEM);
183 av_audio_fifo_read(s->left, (void **)out->extended_data, nb_samples);
184 out->pts = s->pts;
185 s->pts += nb_samples;
186 ret = ff_filter_frame(outlink, out);
187 if (ret < 0)
188 return ret;
189 }
190 ret = ff_request_frame(ctx->inputs[0]);
191 } else {
192 ret = push_samples(ctx, 1024);
193 }
194
195 if (ret == AVERROR_EOF && s->nb_samples > 0 && s->loop != 0) {
196 ret = push_samples(ctx, outlink->sample_rate);
197 }
198
199 return ret;
200}
201
202static const AVOption aloop_options[] = {
203 { "loop", "number of loops", OFFSET(loop), AV_OPT_TYPE_INT, {.i64 = 0 }, -1, INT_MAX, AFLAGS },
204 { "size", "max number of samples to loop", OFFSET(size), AV_OPT_TYPE_INT64, {.i64 = 0 }, 0, INT32_MAX, AFLAGS },
205 { "start", "set the loop start sample", OFFSET(start), AV_OPT_TYPE_INT64, {.i64 = 0 }, 0, INT64_MAX, AFLAGS },
206 { NULL }
207};
208
209AVFILTER_DEFINE_CLASS(aloop);
210
211static const AVFilterPad ainputs[] = {
212 {
213 .name = "default",
214 .type = AVMEDIA_TYPE_AUDIO,
215 .filter_frame = afilter_frame,
216 .config_props = aconfig_input,
217 },
218 { NULL }
219};
220
221static const AVFilterPad aoutputs[] = {
222 {
223 .name = "default",
224 .type = AVMEDIA_TYPE_AUDIO,
225 .request_frame = arequest_frame,
226 },
227 { NULL }
228};
229
230AVFilter ff_af_aloop = {
231 .name = "aloop",
232 .description = NULL_IF_CONFIG_SMALL("Loop audio samples."),
233 .priv_size = sizeof(LoopContext),
234 .priv_class = &aloop_class,
235 .uninit = auninit,
236 .inputs = ainputs,
237 .outputs = aoutputs,
238};
239#endif /* CONFIG_ALOOP_FILTER */
240
241#if CONFIG_LOOP_FILTER
242
243static av_cold int init(AVFilterContext *ctx)
244{
245 LoopContext *s = ctx->priv;
246
247 s->frames = av_calloc(s->size, sizeof(*s->frames));
248 if (!s->frames)
249 return AVERROR(ENOMEM);
250
251 return 0;
252}
253
254static av_cold void uninit(AVFilterContext *ctx)
255{
256 LoopContext *s = ctx->priv;
257 int i;
258
259 for (i = 0; i < s->nb_frames; i++)
260 av_frame_free(&s->frames[i]);
261
262 av_freep(&s->frames);
263 s->nb_frames = 0;
264}
265
266static int push_frame(AVFilterContext *ctx)
267{
268 AVFilterLink *outlink = ctx->outputs[0];
269 LoopContext *s = ctx->priv;
270 int64_t pts;
271 int ret;
272
273 AVFrame *out = av_frame_clone(s->frames[s->current_frame]);
274
275 if (!out)
276 return AVERROR(ENOMEM);
277 out->pts += s->duration - s->start_pts;
278 pts = out->pts + av_frame_get_pkt_duration(out);
279 ret = ff_filter_frame(outlink, out);
280 s->current_frame++;
281
282 if (s->current_frame >= s->nb_frames) {
283 s->duration = pts;
284 s->current_frame = 0;
285
286 if (s->loop > 0)
287 s->loop--;
288 }
289
290 return ret;
291}
292
293static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
294{
295 AVFilterContext *ctx = inlink->dst;
296 AVFilterLink *outlink = ctx->outputs[0];
297 LoopContext *s = ctx->priv;
298 int ret = 0;
299
300 if (inlink->frame_count_out >= s->start && s->size > 0 && s->loop != 0) {
301 if (s->nb_frames < s->size) {
302 if (!s->nb_frames)
303 s->start_pts = frame->pts;
304 s->frames[s->nb_frames] = av_frame_clone(frame);
305 if (!s->frames[s->nb_frames]) {
306 av_frame_free(&frame);
307 return AVERROR(ENOMEM);
308 }
309 s->nb_frames++;
310 s->duration = frame->pts + av_frame_get_pkt_duration(frame);
311 ret = ff_filter_frame(outlink, frame);
312 } else {
313 av_frame_free(&frame);
314 ret = push_frame(ctx);
315 }
316 } else {
317 frame->pts += s->duration;
318 ret = ff_filter_frame(outlink, frame);
319 }
320
321 return ret;
322}
323
324static int request_frame(AVFilterLink *outlink)
325{
326 AVFilterContext *ctx = outlink->src;
327 LoopContext *s = ctx->priv;
328 int ret = 0;
329
330 if ((!s->size) ||
331 (s->nb_frames < s->size) ||
332 (s->nb_frames >= s->size && s->loop == 0)) {
333 ret = ff_request_frame(ctx->inputs[0]);
334 } else {
335 ret = push_frame(ctx);
336 }
337
338 if (ret == AVERROR_EOF && s->nb_frames > 0 && s->loop != 0) {
339 ret = push_frame(ctx);
340 }
341
342 return ret;
343}
344
345static const AVOption loop_options[] = {
346 { "loop", "number of loops", OFFSET(loop), AV_OPT_TYPE_INT, {.i64 = 0 }, -1, INT_MAX, VFLAGS },
347 { "size", "max number of frames to loop", OFFSET(size), AV_OPT_TYPE_INT64, {.i64 = 0 }, 0, INT16_MAX, VFLAGS },
348 { "start", "set the loop start frame", OFFSET(start), AV_OPT_TYPE_INT64, {.i64 = 0 }, 0, INT64_MAX, VFLAGS },
349 { NULL }
350};
351
352AVFILTER_DEFINE_CLASS(loop);
353
354static const AVFilterPad inputs[] = {
355 {
356 .name = "default",
357 .type = AVMEDIA_TYPE_VIDEO,
358 .filter_frame = filter_frame,
359 },
360 { NULL }
361};
362
363static const AVFilterPad outputs[] = {
364 {
365 .name = "default",
366 .type = AVMEDIA_TYPE_VIDEO,
367 .request_frame = request_frame,
368 },
369 { NULL }
370};
371
372AVFilter ff_vf_loop = {
373 .name = "loop",
374 .description = NULL_IF_CONFIG_SMALL("Loop video frames."),
375 .priv_size = sizeof(LoopContext),
376 .priv_class = &loop_class,
377 .init = init,
378 .uninit = uninit,
379 .inputs = inputs,
380 .outputs = outputs,
381};
382#endif /* CONFIG_LOOP_FILTER */
383