]> git.sesse.net Git - ffmpeg/blob - libavfilter/f_loop.c
Merge commit '896fe15dbb7b78de495c4a7dd75e7faec66778da'
[ffmpeg] / libavfilter / f_loop.c
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 "filters.h"
29 #include "formats.h"
30 #include "internal.h"
31 #include "video.h"
32
33 typedef struct LoopContext {
34     const AVClass *class;
35
36     AVAudioFifo *fifo;
37     AVAudioFifo *left;
38     AVFrame **frames;
39     int nb_frames;
40     int current_frame;
41     int64_t start_pts;
42     int64_t duration;
43     int64_t current_sample;
44     int64_t nb_samples;
45     int64_t ignored_samples;
46
47     int loop;
48     int eof;
49     int64_t size;
50     int64_t start;
51     int64_t pts;
52 } LoopContext;
53
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)
57
58 #if CONFIG_ALOOP_FILTER
59
60 static int aconfig_input(AVFilterLink *inlink)
61 {
62     AVFilterContext *ctx = inlink->dst;
63     LoopContext *s  = ctx->priv;
64
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);
69
70     return 0;
71 }
72
73 static av_cold void auninit(AVFilterContext *ctx)
74 {
75     LoopContext *s = ctx->priv;
76
77     av_audio_fifo_free(s->fifo);
78     av_audio_fifo_free(s->left);
79 }
80
81 static int push_samples(AVFilterContext *ctx, int nb_samples)
82 {
83     AVFilterLink *outlink = ctx->outputs[0];
84     LoopContext *s = ctx->priv;
85     AVFrame *out;
86     int ret, i = 0;
87
88     while (s->loop != 0 && i < nb_samples) {
89         out = ff_get_audio_buffer(outlink, FFMIN(nb_samples, s->nb_samples - s->current_sample));
90         if (!out)
91             return AVERROR(ENOMEM);
92         ret = av_audio_fifo_peek_at(s->fifo, (void **)out->extended_data, out->nb_samples, s->current_sample);
93         if (ret < 0) {
94             av_frame_free(&out);
95             return ret;
96         }
97         out->pts = s->pts;
98         out->nb_samples = ret;
99         s->pts += out->nb_samples;
100         i += out->nb_samples;
101         s->current_sample += out->nb_samples;
102
103         ret = ff_filter_frame(outlink, out);
104         if (ret < 0)
105             return ret;
106
107         if (s->current_sample >= s->nb_samples) {
108             s->current_sample = 0;
109
110             if (s->loop > 0)
111                 s->loop--;
112         }
113     }
114
115     return ret;
116 }
117
118 static int afilter_frame(AVFilterLink *inlink, AVFrame *frame)
119 {
120     AVFilterContext *ctx = inlink->dst;
121     AVFilterLink *outlink = ctx->outputs[0];
122     LoopContext *s = ctx->priv;
123     int ret = 0;
124
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);
128             int drain = 0;
129
130             ret = av_audio_fifo_write(s->fifo, (void **)frame->extended_data, written);
131             if (ret < 0)
132                 return ret;
133             if (!s->nb_samples) {
134                 drain = FFMAX(0, s->start - s->ignored_samples);
135                 s->pts = frame->pts;
136                 av_audio_fifo_drain(s->fifo, drain);
137                 s->pts += s->start - s->ignored_samples;
138             }
139             s->nb_samples += ret - drain;
140             drain = frame->nb_samples - written;
141             if (s->nb_samples == s->size && drain > 0) {
142                 int ret2;
143
144                 ret2 = av_audio_fifo_write(s->left, (void **)frame->extended_data, frame->nb_samples);
145                 if (ret2 < 0)
146                    return ret2;
147                 av_audio_fifo_drain(s->left, drain);
148             }
149             frame->nb_samples = ret;
150             s->pts += ret;
151             ret = ff_filter_frame(outlink, frame);
152         } else {
153             int nb_samples = frame->nb_samples;
154
155             av_frame_free(&frame);
156             ret = push_samples(ctx, nb_samples);
157         }
158     } else {
159         s->ignored_samples += frame->nb_samples;
160         frame->pts = s->pts;
161         s->pts += frame->nb_samples;
162         ret = ff_filter_frame(outlink, frame);
163     }
164
165     return ret;
166 }
167
168 static int arequest_frame(AVFilterLink *outlink)
169 {
170     AVFilterContext *ctx = outlink->src;
171     LoopContext *s = ctx->priv;
172     int ret = 0;
173
174     if ((!s->size) ||
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);
178
179         if (s->loop == 0 && nb_samples > 0) {
180             AVFrame *out;
181
182             out = ff_get_audio_buffer(outlink, nb_samples);
183             if (!out)
184                 return AVERROR(ENOMEM);
185             av_audio_fifo_read(s->left, (void **)out->extended_data, nb_samples);
186             out->pts = s->pts;
187             s->pts += nb_samples;
188             ret = ff_filter_frame(outlink, out);
189             if (ret < 0)
190                 return ret;
191         }
192         ret = ff_request_frame(ctx->inputs[0]);
193     } else {
194         ret = push_samples(ctx, 1024);
195     }
196
197     if (ret == AVERROR_EOF && s->nb_samples > 0 && s->loop != 0) {
198         ret = push_samples(ctx, outlink->sample_rate);
199     }
200
201     return ret;
202 }
203
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 },
208     { NULL }
209 };
210
211 AVFILTER_DEFINE_CLASS(aloop);
212
213 static const AVFilterPad ainputs[] = {
214     {
215         .name         = "default",
216         .type         = AVMEDIA_TYPE_AUDIO,
217         .filter_frame = afilter_frame,
218         .config_props = aconfig_input,
219     },
220     { NULL }
221 };
222
223 static const AVFilterPad aoutputs[] = {
224     {
225         .name          = "default",
226         .type          = AVMEDIA_TYPE_AUDIO,
227         .request_frame = arequest_frame,
228     },
229     { NULL }
230 };
231
232 AVFilter ff_af_aloop = {
233     .name          = "aloop",
234     .description   = NULL_IF_CONFIG_SMALL("Loop audio samples."),
235     .priv_size     = sizeof(LoopContext),
236     .priv_class    = &aloop_class,
237     .uninit        = auninit,
238     .inputs        = ainputs,
239     .outputs       = aoutputs,
240 };
241 #endif /* CONFIG_ALOOP_FILTER */
242
243 #if CONFIG_LOOP_FILTER
244
245 static av_cold int init(AVFilterContext *ctx)
246 {
247     LoopContext *s = ctx->priv;
248
249     s->frames = av_calloc(s->size, sizeof(*s->frames));
250     if (!s->frames)
251         return AVERROR(ENOMEM);
252
253     return 0;
254 }
255
256 static av_cold void uninit(AVFilterContext *ctx)
257 {
258     LoopContext *s = ctx->priv;
259     int i;
260
261     for (i = 0; i < s->nb_frames; i++)
262         av_frame_free(&s->frames[i]);
263
264     av_freep(&s->frames);
265     s->nb_frames = 0;
266 }
267
268 static int push_frame(AVFilterContext *ctx)
269 {
270     AVFilterLink *outlink = ctx->outputs[0];
271     LoopContext *s = ctx->priv;
272     int64_t pts, duration;
273     int ret;
274
275     AVFrame *out = av_frame_clone(s->frames[s->current_frame]);
276
277     if (!out)
278         return AVERROR(ENOMEM);
279     out->pts += s->duration - s->start_pts;
280     if (out->pkt_duration)
281         duration = out->pkt_duration;
282     else
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);
286     s->current_frame++;
287
288     if (s->current_frame >= s->nb_frames) {
289         s->duration = pts;
290         s->current_frame = 0;
291
292         if (s->loop > 0)
293             s->loop--;
294     }
295
296     return ret;
297 }
298
299 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
300 {
301     AVFilterContext *ctx = inlink->dst;
302     AVFilterLink *outlink = ctx->outputs[0];
303     LoopContext *s = ctx->priv;
304     int64_t duration;
305     int ret = 0;
306
307     if (inlink->frame_count_out >= s->start && s->size > 0 && s->loop != 0) {
308         if (s->nb_frames < s->size) {
309             if (!s->nb_frames)
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);
315             }
316             s->nb_frames++;
317             if (frame->pkt_duration)
318                 duration = frame->pkt_duration;
319             else
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);
323         } else {
324             av_frame_free(&frame);
325             ret = push_frame(ctx);
326         }
327     } else {
328         frame->pts += s->duration;
329         ret = ff_filter_frame(outlink, frame);
330     }
331
332     return ret;
333 }
334
335 static int activate(AVFilterContext *ctx)
336 {
337     AVFilterLink *inlink = ctx->inputs[0];
338     AVFilterLink *outlink = ctx->outputs[0];
339     LoopContext *s = ctx->priv;
340     AVFrame *frame = NULL;
341     int ret, status;
342     int64_t pts;
343
344     FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
345
346     if (!s->eof && (s->nb_frames < s->size || !s->loop)) {
347         ret = ff_inlink_consume_frame(inlink, &frame);
348         if (ret < 0)
349             return ret;
350         if (ret > 0)
351             return filter_frame(inlink, frame);
352     }
353
354     if (!s->eof && ff_inlink_acknowledge_status(inlink, &status, &pts)) {
355         if (status == AVERROR_EOF)
356             s->eof = 1;
357     }
358
359     if (s->eof && (s->loop == 0 || s->nb_frames < s->size)) {
360         ff_outlink_set_status(outlink, AVERROR_EOF, s->duration);
361         return 0;
362     }
363
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);
370     }
371
372     return FFERROR_NOT_READY;
373 }
374
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 },
379     { NULL }
380 };
381
382 AVFILTER_DEFINE_CLASS(loop);
383
384 static const AVFilterPad inputs[] = {
385     {
386         .name = "default",
387         .type = AVMEDIA_TYPE_VIDEO,
388     },
389     { NULL }
390 };
391
392 static const AVFilterPad outputs[] = {
393     {
394         .name = "default",
395         .type = AVMEDIA_TYPE_VIDEO,
396     },
397     { NULL }
398 };
399
400 AVFilter ff_vf_loop = {
401     .name        = "loop",
402     .description = NULL_IF_CONFIG_SMALL("Loop video frames."),
403     .priv_size   = sizeof(LoopContext),
404     .priv_class  = &loop_class,
405     .init        = init,
406     .uninit      = uninit,
407     .activate    = activate,
408     .inputs      = inputs,
409     .outputs     = outputs,
410 };
411 #endif /* CONFIG_LOOP_FILTER */