Imported Upstream version 6.1
[platform/upstream/ffmpeg.git] / 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 "config_components.h"
22
23 #include "libavutil/audio_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 "internal.h"
30 #include "video.h"
31
32 typedef 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 time_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     int eof;
48     int64_t size;
49     int64_t start;
50     int64_t time;
51     int64_t pts;
52     int64_t pts_offset;
53     int64_t eof_pts;
54 } LoopContext;
55
56 #define AFLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
57 #define VFLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
58 #define OFFSET(x) offsetof(LoopContext, x)
59
60 static void check_size(AVFilterContext *ctx)
61 {
62     LoopContext *s = ctx->priv;
63
64     if (!s->size)
65         av_log(ctx, AV_LOG_WARNING, "Number of %s to loop is not set!\n",
66                ctx->input_pads[0].type == AVMEDIA_TYPE_VIDEO ? "frames" : "samples");
67 }
68
69 static void update_time(AVFilterContext *ctx, AVRational tb)
70 {
71     LoopContext *s = ctx->priv;
72
73     if (s->time != INT64_MAX) {
74         int64_t time_pts = av_rescale_q(s->time, AV_TIME_BASE_Q, tb);
75         if (s->time_pts == AV_NOPTS_VALUE || time_pts < s->time_pts)
76             s->time_pts = time_pts;
77     }
78 }
79
80 #if CONFIG_ALOOP_FILTER
81
82 static int aconfig_input(AVFilterLink *inlink)
83 {
84     AVFilterContext *ctx = inlink->dst;
85     LoopContext *s  = ctx->priv;
86
87     s->time_pts = AV_NOPTS_VALUE;
88
89     s->fifo = av_audio_fifo_alloc(inlink->format, inlink->ch_layout.nb_channels, 8192);
90     s->left = av_audio_fifo_alloc(inlink->format, inlink->ch_layout.nb_channels, 8192);
91     if (!s->fifo || !s->left)
92         return AVERROR(ENOMEM);
93
94     check_size(ctx);
95
96     return 0;
97 }
98
99 static av_cold void auninit(AVFilterContext *ctx)
100 {
101     LoopContext *s = ctx->priv;
102
103     av_audio_fifo_free(s->fifo);
104     av_audio_fifo_free(s->left);
105 }
106
107 static int push_samples(AVFilterContext *ctx, int nb_samples)
108 {
109     AVFilterLink *outlink = ctx->outputs[0];
110     LoopContext *s = ctx->priv;
111     AVFrame *out;
112     int ret = 0, i = 0;
113
114     while (s->loop != 0 && i < nb_samples) {
115         out = ff_get_audio_buffer(outlink, FFMIN(nb_samples, s->nb_samples - s->current_sample));
116         if (!out)
117             return AVERROR(ENOMEM);
118         ret = av_audio_fifo_peek_at(s->fifo, (void **)out->extended_data, out->nb_samples, s->current_sample);
119         if (ret < 0) {
120             av_frame_free(&out);
121             return ret;
122         }
123         out->pts = s->pts;
124         out->nb_samples = ret;
125         s->pts += av_rescale_q(out->nb_samples, (AVRational){1, outlink->sample_rate}, outlink->time_base);
126         i += out->nb_samples;
127         s->current_sample += out->nb_samples;
128
129         ret = ff_filter_frame(outlink, out);
130         if (ret < 0)
131             return ret;
132
133         if (s->current_sample >= s->nb_samples) {
134             s->current_sample = 0;
135
136             if (s->loop > 0)
137                 s->loop--;
138         }
139     }
140
141     return ret;
142 }
143
144 static int afilter_frame(AVFilterLink *inlink, AVFrame *frame)
145 {
146     AVFilterContext *ctx = inlink->dst;
147     AVFilterLink *outlink = ctx->outputs[0];
148     LoopContext *s = ctx->priv;
149     int ret = 0;
150
151     if (((s->start >= 0 && s->ignored_samples + frame->nb_samples > s->start) ||
152          (s->time_pts != AV_NOPTS_VALUE &&
153           frame->pts >= s->time_pts)) &&
154         s->size > 0 && s->loop != 0) {
155         if (s->nb_samples < s->size) {
156             int written = FFMIN(frame->nb_samples, s->size - s->nb_samples);
157             int drain = 0;
158
159             if (s->start < 0)
160                 s->start = inlink->sample_count_out - written;
161
162             ret = av_audio_fifo_write(s->fifo, (void **)frame->extended_data, written);
163             if (ret < 0)
164                 return ret;
165             if (!s->nb_samples) {
166                 drain = FFMAX(0, s->start - s->ignored_samples);
167                 s->pts = frame->pts;
168                 av_audio_fifo_drain(s->fifo, drain);
169                 s->pts += av_rescale_q(s->start - s->ignored_samples, (AVRational){1, outlink->sample_rate}, outlink->time_base);
170             }
171             s->nb_samples += ret - drain;
172             drain = frame->nb_samples - written;
173             if (s->nb_samples == s->size && drain > 0) {
174                 int ret2;
175
176                 ret2 = av_audio_fifo_write(s->left, (void **)frame->extended_data, frame->nb_samples);
177                 if (ret2 < 0)
178                    return ret2;
179                 av_audio_fifo_drain(s->left, drain);
180             }
181             frame->nb_samples = ret;
182             s->pts += av_rescale_q(ret, (AVRational){1, outlink->sample_rate}, outlink->time_base);
183             ret = ff_filter_frame(outlink, frame);
184         } else {
185             int nb_samples = frame->nb_samples;
186
187             av_frame_free(&frame);
188             ret = push_samples(ctx, nb_samples);
189         }
190     } else {
191         s->ignored_samples += frame->nb_samples;
192         frame->pts = s->pts;
193         s->pts += av_rescale_q(frame->nb_samples, (AVRational){1, outlink->sample_rate}, outlink->time_base);
194         ret = ff_filter_frame(outlink, frame);
195     }
196
197     return ret;
198 }
199
200 static int arequest_frame(AVFilterLink *outlink)
201 {
202     AVFilterContext *ctx = outlink->src;
203     LoopContext *s = ctx->priv;
204     int ret = 0;
205
206     if ((!s->size) ||
207         (s->nb_samples < s->size) ||
208         (s->nb_samples >= s->size && s->loop == 0)) {
209         int nb_samples = av_audio_fifo_size(s->left);
210
211         if (s->loop == 0 && nb_samples > 0) {
212             AVFrame *out;
213
214             out = ff_get_audio_buffer(outlink, nb_samples);
215             if (!out)
216                 return AVERROR(ENOMEM);
217             av_audio_fifo_read(s->left, (void **)out->extended_data, nb_samples);
218             out->pts = s->pts;
219             s->pts += av_rescale_q(nb_samples, (AVRational){1, outlink->sample_rate}, outlink->time_base);
220             ret = ff_filter_frame(outlink, out);
221             if (ret < 0)
222                 return ret;
223         }
224         ret = ff_request_frame(ctx->inputs[0]);
225     } else {
226         ret = push_samples(ctx, 1024);
227     }
228
229     if (s->eof && s->nb_samples > 0 && s->loop != 0) {
230         ret = push_samples(ctx, 1024);
231     }
232
233     return ret;
234 }
235
236 static int aactivate(AVFilterContext *ctx)
237 {
238     AVFilterLink *inlink = ctx->inputs[0];
239     AVFilterLink *outlink = ctx->outputs[0];
240     LoopContext *s = ctx->priv;
241     AVFrame *frame = NULL;
242     int ret, status;
243
244     FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
245
246     update_time(ctx, inlink->time_base);
247
248     if (!s->eof && (s->nb_samples < s->size || !s->loop || !s->size)) {
249         const int in_nb_samples = FFMIN(1024, s->size - s->nb_samples);
250         if (in_nb_samples == 0)
251             ret = ff_inlink_consume_frame(inlink, &frame);
252         else
253             ret = ff_inlink_consume_samples(inlink, in_nb_samples, in_nb_samples, &frame);
254         if (ret < 0)
255             return ret;
256         if (ret > 0)
257             return afilter_frame(inlink, frame);
258     }
259
260     if (!s->eof && ff_inlink_acknowledge_status(inlink, &status, &s->eof_pts)) {
261         if (status == AVERROR_EOF) {
262             s->size = s->nb_samples;
263             s->eof = 1;
264         }
265     }
266
267     if (s->eof && (!s->loop || !s->size)) {
268         ff_outlink_set_status(outlink, AVERROR_EOF, s->eof_pts + s->pts_offset);
269         return 0;
270     }
271
272     if (!s->eof && (!s->size ||
273         (s->nb_samples < s->size) ||
274         (s->nb_samples >= s->size && s->loop == 0))) {
275         FF_FILTER_FORWARD_WANTED(outlink, inlink);
276     } else if (s->loop && s->nb_samples == s->size) {
277         return arequest_frame(outlink);
278     }
279
280     return FFERROR_NOT_READY;
281 }
282
283 static const AVOption aloop_options[] = {
284     { "loop",  "number of loops",               OFFSET(loop),  AV_OPT_TYPE_INT,   {.i64 = 0 }, -1, INT_MAX,   AFLAGS },
285     { "size",  "max number of samples to loop", OFFSET(size),  AV_OPT_TYPE_INT64, {.i64 = 0 },  0, INT32_MAX, AFLAGS },
286     { "start", "set the loop start sample",     OFFSET(start), AV_OPT_TYPE_INT64, {.i64 = 0 }, -1, INT64_MAX, AFLAGS },
287     { "time",  "set the loop start time",       OFFSET(time),  AV_OPT_TYPE_DURATION, {.i64=INT64_MAX}, INT64_MIN, INT64_MAX, AFLAGS },
288     { NULL }
289 };
290
291 AVFILTER_DEFINE_CLASS(aloop);
292
293 static const AVFilterPad ainputs[] = {
294     {
295         .name         = "default",
296         .type         = AVMEDIA_TYPE_AUDIO,
297         .config_props = aconfig_input,
298     },
299 };
300
301 const AVFilter ff_af_aloop = {
302     .name          = "aloop",
303     .description   = NULL_IF_CONFIG_SMALL("Loop audio samples."),
304     .priv_size     = sizeof(LoopContext),
305     .priv_class    = &aloop_class,
306     .activate      = aactivate,
307     .uninit        = auninit,
308     FILTER_INPUTS(ainputs),
309     FILTER_OUTPUTS(ff_audio_default_filterpad),
310 };
311 #endif /* CONFIG_ALOOP_FILTER */
312
313 #if CONFIG_LOOP_FILTER
314
315 static av_cold int init(AVFilterContext *ctx)
316 {
317     LoopContext *s = ctx->priv;
318
319     s->time_pts = AV_NOPTS_VALUE;
320
321     s->frames = av_calloc(s->size, sizeof(*s->frames));
322     if (!s->frames)
323         return AVERROR(ENOMEM);
324
325     check_size(ctx);
326
327     return 0;
328 }
329
330 static void free_frames(AVFilterContext *ctx)
331 {
332     LoopContext *s = ctx->priv;
333
334     for (int i = 0; i < s->nb_frames; i++)
335         av_frame_free(&s->frames[i]);
336 }
337
338 static av_cold void uninit(AVFilterContext *ctx)
339 {
340     LoopContext *s = ctx->priv;
341
342     free_frames(ctx);
343     av_freep(&s->frames);
344     s->nb_frames = 0;
345 }
346
347 static int push_frame(AVFilterContext *ctx)
348 {
349     AVFilterLink *outlink = ctx->outputs[0];
350     LoopContext *s = ctx->priv;
351     AVFrame *out;
352     int ret;
353
354     out = av_frame_clone(s->frames[s->current_frame]);
355     if (!out)
356         return AVERROR(ENOMEM);
357     out->pts += s->pts_offset;
358     ret = ff_filter_frame(outlink, out);
359     s->current_frame++;
360
361     if (s->current_frame >= s->nb_frames) {
362         s->current_frame = 0;
363
364         s->pts_offset += s->duration;
365         if (s->loop > 0)
366             s->loop--;
367         if (s->loop == 0)
368             free_frames(ctx);
369     }
370
371     return ret;
372 }
373
374 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
375 {
376     AVFilterContext *ctx = inlink->dst;
377     AVFilterLink *outlink = ctx->outputs[0];
378     LoopContext *s = ctx->priv;
379     int64_t duration;
380     int ret = 0;
381
382     if (((s->start >= 0 && inlink->frame_count_out >= s->start) ||
383          (s->time_pts != AV_NOPTS_VALUE &&
384           frame->pts >= s->time_pts)) &&
385         s->size > 0 && s->loop != 0) {
386         if (s->nb_frames < s->size) {
387             s->frames[s->nb_frames] = av_frame_clone(frame);
388             if (!s->frames[s->nb_frames]) {
389                 av_frame_free(&frame);
390                 return AVERROR(ENOMEM);
391             }
392             s->nb_frames++;
393             if (frame->duration)
394                 duration = frame->duration;
395             else
396                 duration = av_rescale_q(1, av_inv_q(outlink->frame_rate), outlink->time_base);
397             s->duration += duration;
398             s->pts_offset = s->duration;
399             ret = ff_filter_frame(outlink, frame);
400         } else {
401             av_frame_free(&frame);
402             ret = push_frame(ctx);
403         }
404     } else {
405         frame->pts += s->pts_offset - s->duration;
406         ret = ff_filter_frame(outlink, frame);
407     }
408
409     return ret;
410 }
411
412 static int activate(AVFilterContext *ctx)
413 {
414     AVFilterLink *inlink = ctx->inputs[0];
415     AVFilterLink *outlink = ctx->outputs[0];
416     LoopContext *s = ctx->priv;
417     AVFrame *frame = NULL;
418     int ret, status;
419
420     ret = ff_outlink_get_status(outlink);
421     if (ret) {
422         ff_inlink_set_status(inlink, ret);
423         free_frames(ctx);
424         return 0;
425     }
426
427     update_time(ctx, inlink->time_base);
428
429     if (!s->eof && (s->nb_frames < s->size || !s->loop || !s->size)) {
430         ret = ff_inlink_consume_frame(inlink, &frame);
431         if (ret < 0)
432             return ret;
433         if (ret > 0)
434             return filter_frame(inlink, frame);
435     }
436
437     if (!s->eof && ff_inlink_acknowledge_status(inlink, &status, &s->eof_pts)) {
438         if (status == AVERROR_EOF) {
439             s->size = s->nb_frames;
440             s->eof = 1;
441         }
442     }
443
444     if (s->eof && (!s->loop || !s->size)) {
445         ff_outlink_set_status(outlink, AVERROR_EOF, s->eof_pts + s->pts_offset);
446         free_frames(ctx);
447         return 0;
448     }
449
450     if (!s->eof && (!s->size ||
451         (s->nb_frames < s->size) ||
452         (s->nb_frames >= s->size && s->loop == 0))) {
453         FF_FILTER_FORWARD_WANTED(outlink, inlink);
454     } else if (s->loop && s->nb_frames == s->size) {
455         return push_frame(ctx);
456     }
457
458     return FFERROR_NOT_READY;
459 }
460
461 static const AVOption loop_options[] = {
462     { "loop",  "number of loops",              OFFSET(loop),  AV_OPT_TYPE_INT,   {.i64 = 0 }, -1, INT_MAX,   VFLAGS },
463     { "size",  "max number of frames to loop", OFFSET(size),  AV_OPT_TYPE_INT64, {.i64 = 0 },  0, INT16_MAX, VFLAGS },
464     { "start", "set the loop start frame",     OFFSET(start), AV_OPT_TYPE_INT64, {.i64 = 0 }, -1, INT64_MAX, VFLAGS },
465     { "time",  "set the loop start time",      OFFSET(time),  AV_OPT_TYPE_DURATION, {.i64=INT64_MAX}, INT64_MIN, INT64_MAX, VFLAGS },
466     { NULL }
467 };
468
469 AVFILTER_DEFINE_CLASS(loop);
470
471 const AVFilter ff_vf_loop = {
472     .name        = "loop",
473     .description = NULL_IF_CONFIG_SMALL("Loop video frames."),
474     .priv_size   = sizeof(LoopContext),
475     .priv_class  = &loop_class,
476     .init        = init,
477     .uninit      = uninit,
478     .activate    = activate,
479     FILTER_INPUTS(ff_video_default_filterpad),
480     FILTER_OUTPUTS(ff_video_default_filterpad),
481 };
482 #endif /* CONFIG_LOOP_FILTER */