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