Browse code

avfilter: add loop filters

Signed-off-by: Paul B Mahol <onemda@gmail.com>

Paul B Mahol authored on 2016/02/12 06:05:54
Showing 9 changed files
... ...
@@ -4,6 +4,7 @@ releases are sorted from youngest to oldest.
4 4
 version <next>:
5 5
 - DXVA2-accelerated HEVC Main10 decoding
6 6
 - fieldhint filter
7
+- loop video filter and aloop audio filter
7 8
 
8 9
 
9 10
 version 3.0:
... ...
@@ -15,6 +15,9 @@ libavutil:     2015-08-28
15 15
 
16 16
 API changes, most recent first:
17 17
 
18
+2016-xx-xx - lavu 55.18.100
19
+  xxxxxxx audio_fifo.h - Add av_audio_fifo_peek_at().
20
+
18 21
 2016-xx-xx - lavu 55.18.0
19 22
   xxxxxxx buffer.h - Add av_buffer_pool_init2().
20 23
   xxxxxxx hwcontext.h - Add a new installed header hwcontext.h with a new API
... ...
@@ -8185,6 +8185,25 @@ The formula that generates the correction is:
8185 8185
 where @var{r_0} is halve of the image diagonal and @var{r_src} and @var{r_tgt} are the
8186 8186
 distances from the focal point in the source and target images, respectively.
8187 8187
 
8188
+@section loop, aloop
8189
+
8190
+Loop video frames or audio samples.
8191
+
8192
+Those filters accepts the following options:
8193
+
8194
+@table @option
8195
+@item loop
8196
+Set the number of loops.
8197
+
8198
+@item size
8199
+Set maximal size in number of frames for @code{loop} filter or maximal number
8200
+of samples in case of @code{aloop} filter.
8201
+
8202
+@item start
8203
+Set first frame of loop for @code{loop} filter or first sample of loop in case
8204
+of @code{aloop} filter.
8205
+@end table
8206
+
8188 8207
 @anchor{lut3d}
8189 8208
 @section lut3d
8190 8209
 
... ...
@@ -38,6 +38,7 @@ OBJS-$(CONFIG_AGATE_FILTER)                  += af_agate.o
38 38
 OBJS-$(CONFIG_AINTERLEAVE_FILTER)            += f_interleave.o
39 39
 OBJS-$(CONFIG_ALIMITER_FILTER)               += af_alimiter.o
40 40
 OBJS-$(CONFIG_ALLPASS_FILTER)                += af_biquads.o
41
+OBJS-$(CONFIG_ALOOP_FILTER)                  += f_loop.o
41 42
 OBJS-$(CONFIG_AMERGE_FILTER)                 += af_amerge.o
42 43
 OBJS-$(CONFIG_AMETADATA_FILTER)              += f_metadata.o
43 44
 OBJS-$(CONFIG_AMIX_FILTER)                   += af_amix.o
... ...
@@ -181,6 +182,7 @@ OBJS-$(CONFIG_INTERLACE_FILTER)              += vf_interlace.o
181 181
 OBJS-$(CONFIG_INTERLEAVE_FILTER)             += f_interleave.o
182 182
 OBJS-$(CONFIG_KERNDEINT_FILTER)              += vf_kerndeint.o
183 183
 OBJS-$(CONFIG_LENSCORRECTION_FILTER)         += vf_lenscorrection.o
184
+OBJS-$(CONFIG_LOOP_FILTER)                   += f_loop.o
184 185
 OBJS-$(CONFIG_LUT3D_FILTER)                  += vf_lut3d.o
185 186
 OBJS-$(CONFIG_LUT_FILTER)                    += vf_lut.o
186 187
 OBJS-$(CONFIG_LUTRGB_FILTER)                 += vf_lut.o
... ...
@@ -58,6 +58,7 @@ void avfilter_register_all(void)
58 58
     REGISTER_FILTER(AINTERLEAVE,    ainterleave,    af);
59 59
     REGISTER_FILTER(ALIMITER,       alimiter,       af);
60 60
     REGISTER_FILTER(ALLPASS,        allpass,        af);
61
+    REGISTER_FILTER(ALOOP,          aloop,          af);
61 62
     REGISTER_FILTER(AMERGE,         amerge,         af);
62 63
     REGISTER_FILTER(AMETADATA,      ametadata,      af);
63 64
     REGISTER_FILTER(AMIX,           amix,           af);
... ...
@@ -202,6 +203,7 @@ void avfilter_register_all(void)
202 202
     REGISTER_FILTER(INTERLEAVE,     interleave,     vf);
203 203
     REGISTER_FILTER(KERNDEINT,      kerndeint,      vf);
204 204
     REGISTER_FILTER(LENSCORRECTION, lenscorrection, vf);
205
+    REGISTER_FILTER(LOOP,           loop,           vf);
205 206
     REGISTER_FILTER(LUT3D,          lut3d,          vf);
206 207
     REGISTER_FILTER(LUT,            lut,            vf);
207 208
     REGISTER_FILTER(LUTRGB,         lutrgb,         vf);
208 209
new file mode 100644
... ...
@@ -0,0 +1,381 @@
0
+/*
1
+ * Copyright (c) 2016 Paul B Mahol
2
+ *
3
+ * This file is part of FFmpeg.
4
+ *
5
+ * FFmpeg is free software; you can redistribute it and/or
6
+ * modify it under the terms of the GNU Lesser General Public
7
+ * License as published by the Free Software Foundation; either
8
+ * version 2.1 of the License, or (at your option) any later version.
9
+ *
10
+ * FFmpeg is distributed in the hope that it will be useful,
11
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
+ * Lesser General Public License for more details.
14
+ *
15
+ * You should have received a copy of the GNU Lesser General Public
16
+ * License along with FFmpeg; if not, write to the Free Software
17
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
+ */
19
+
20
+#include "libavutil/audio_fifo.h"
21
+#include "libavutil/avassert.h"
22
+#include "libavutil/fifo.h"
23
+#include "libavutil/internal.h"
24
+#include "libavutil/opt.h"
25
+#include "avfilter.h"
26
+#include "audio.h"
27
+#include "formats.h"
28
+#include "internal.h"
29
+#include "video.h"
30
+
31
+typedef struct LoopContext {
32
+    const AVClass *class;
33
+
34
+    AVAudioFifo *fifo;
35
+    AVAudioFifo *left;
36
+    AVFrame **frames;
37
+    int nb_frames;
38
+    int current_frame;
39
+    int64_t start_pts;
40
+    int64_t duration;
41
+    int64_t current_sample;
42
+    int64_t nb_samples;
43
+    int64_t ignored_samples;
44
+
45
+    int loop;
46
+    int64_t size;
47
+    int64_t start;
48
+    int64_t pts;
49
+} LoopContext;
50
+
51
+#define AFLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
52
+#define VFLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
53
+#define OFFSET(x) offsetof(LoopContext, x)
54
+
55
+#if CONFIG_ALOOP_FILTER
56
+
57
+static int aconfig_input(AVFilterLink *inlink)
58
+{
59
+    AVFilterContext *ctx = inlink->dst;
60
+    LoopContext *s  = ctx->priv;
61
+
62
+    s->fifo = av_audio_fifo_alloc(inlink->format, inlink->channels, 8192);
63
+    s->left = av_audio_fifo_alloc(inlink->format, inlink->channels, 8192);
64
+    if (!s->fifo || !s->left)
65
+        return AVERROR(ENOMEM);
66
+
67
+    return 0;
68
+}
69
+
70
+static av_cold void auninit(AVFilterContext *ctx)
71
+{
72
+    LoopContext *s = ctx->priv;
73
+
74
+    av_audio_fifo_free(s->fifo);
75
+    av_audio_fifo_free(s->left);
76
+}
77
+
78
+static int push_samples(AVFilterContext *ctx, int nb_samples)
79
+{
80
+    AVFilterLink *outlink = ctx->outputs[0];
81
+    LoopContext *s = ctx->priv;
82
+    AVFrame *out;
83
+    int ret, i = 0;
84
+
85
+    while (s->loop != 0 && i < nb_samples) {
86
+        out = ff_get_audio_buffer(outlink, FFMIN(nb_samples, s->nb_samples - s->current_sample));
87
+        if (!out)
88
+            return AVERROR(ENOMEM);
89
+        ret = av_audio_fifo_peek_at(s->fifo, (void **)out->extended_data, out->nb_samples, s->current_sample);
90
+        if (ret < 0)
91
+            return ret;
92
+        out->pts = s->pts;
93
+        out->nb_samples = ret;
94
+        s->pts += out->nb_samples;
95
+        i += out->nb_samples;
96
+        s->current_sample += out->nb_samples;
97
+
98
+        ret = ff_filter_frame(outlink, out);
99
+        if (ret < 0)
100
+            return ret;
101
+
102
+        if (s->current_sample >= s->nb_samples) {
103
+            s->current_sample = 0;
104
+
105
+            if (s->loop > 0)
106
+                s->loop--;
107
+        }
108
+    }
109
+
110
+    return ret;
111
+}
112
+
113
+static int afilter_frame(AVFilterLink *inlink, AVFrame *frame)
114
+{
115
+    AVFilterContext *ctx = inlink->dst;
116
+    AVFilterLink *outlink = ctx->outputs[0];
117
+    LoopContext *s = ctx->priv;
118
+    int ret = 0;
119
+
120
+    if (s->ignored_samples + frame->nb_samples > s->start && s->size > 0 && s->loop != 0) {
121
+        if (s->nb_samples < s->size) {
122
+            int written = FFMIN(frame->nb_samples, s->size - s->nb_samples);
123
+            int drain = 0;
124
+
125
+            ret = av_audio_fifo_write(s->fifo, (void **)frame->extended_data, written);
126
+            if (ret < 0)
127
+                return ret;
128
+            if (!s->nb_samples) {
129
+                drain = FFMAX(0, s->start - s->ignored_samples);
130
+                s->pts = frame->pts;
131
+                av_audio_fifo_drain(s->fifo, drain);
132
+                s->pts += s->start - s->ignored_samples;
133
+            }
134
+            s->nb_samples += ret - drain;
135
+            drain = frame->nb_samples - written;
136
+            if (s->nb_samples == s->size && drain > 0) {
137
+                int ret2;
138
+
139
+                ret2 = av_audio_fifo_write(s->left, (void **)frame->extended_data, frame->nb_samples);
140
+                if (ret2 < 0)
141
+                   return ret2;
142
+                av_audio_fifo_drain(s->left, drain);
143
+            }
144
+            frame->nb_samples = ret;
145
+            s->pts += ret;
146
+            ret = ff_filter_frame(outlink, frame);
147
+        } else {
148
+            int nb_samples = frame->nb_samples;
149
+
150
+            av_frame_free(&frame);
151
+            ret = push_samples(ctx, nb_samples);
152
+        }
153
+    } else {
154
+        s->ignored_samples += frame->nb_samples;
155
+        frame->pts = s->pts;
156
+        s->pts += frame->nb_samples;
157
+        ret = ff_filter_frame(outlink, frame);
158
+    }
159
+
160
+    return ret;
161
+}
162
+
163
+static int arequest_frame(AVFilterLink *outlink)
164
+{
165
+    AVFilterContext *ctx = outlink->src;
166
+    LoopContext *s = ctx->priv;
167
+    int ret = 0;
168
+
169
+    if ((!s->size) ||
170
+        (s->nb_samples < s->size) ||
171
+        (s->nb_samples >= s->size && s->loop == 0)) {
172
+        int nb_samples = av_audio_fifo_size(s->left);
173
+
174
+        if (s->loop == 0 && nb_samples > 0) {
175
+            AVFrame *out;
176
+
177
+            out = ff_get_audio_buffer(outlink, nb_samples);
178
+            if (!out)
179
+                return AVERROR(ENOMEM);
180
+            av_audio_fifo_read(s->left, (void **)out->extended_data, nb_samples);
181
+            out->pts = s->pts;
182
+            s->pts += nb_samples;
183
+            ret = ff_filter_frame(outlink, out);
184
+            if (ret < 0)
185
+                return ret;
186
+        }
187
+        ret = ff_request_frame(ctx->inputs[0]);
188
+    } else {
189
+        ret = push_samples(ctx, 1024);
190
+    }
191
+
192
+    if (ret == AVERROR_EOF && s->nb_samples > 0 && s->loop != 0) {
193
+        ret = push_samples(ctx, outlink->sample_rate);
194
+    }
195
+
196
+    return ret;
197
+}
198
+
199
+static const AVOption aloop_options[] = {
200
+    { "loop",  "number of loops",               OFFSET(loop),  AV_OPT_TYPE_INT,   {.i64 = 0 }, -1, INT_MAX,   AFLAGS },
201
+    { "size",  "max number of samples to loop", OFFSET(size),  AV_OPT_TYPE_INT64, {.i64 = 0 },  0, INT32_MAX, AFLAGS },
202
+    { "start", "set the loop start sample",     OFFSET(start), AV_OPT_TYPE_INT64, {.i64 = 0 },  0, INT64_MAX, AFLAGS },
203
+    { NULL }
204
+};
205
+
206
+AVFILTER_DEFINE_CLASS(aloop);
207
+
208
+static const AVFilterPad ainputs[] = {
209
+    {
210
+        .name         = "default",
211
+        .type         = AVMEDIA_TYPE_AUDIO,
212
+        .filter_frame = afilter_frame,
213
+        .config_props = aconfig_input,
214
+    },
215
+    { NULL }
216
+};
217
+
218
+static const AVFilterPad aoutputs[] = {
219
+    {
220
+        .name          = "default",
221
+        .type          = AVMEDIA_TYPE_AUDIO,
222
+        .request_frame = arequest_frame,
223
+    },
224
+    { NULL }
225
+};
226
+
227
+AVFilter ff_af_aloop = {
228
+    .name          = "aloop",
229
+    .description   = NULL_IF_CONFIG_SMALL("Loop audio samples."),
230
+    .priv_size     = sizeof(LoopContext),
231
+    .priv_class    = &aloop_class,
232
+    .uninit        = auninit,
233
+    .query_formats = ff_query_formats_all,
234
+    .inputs        = ainputs,
235
+    .outputs       = aoutputs,
236
+};
237
+#endif /* CONFIG_ALOOP_FILTER */
238
+
239
+#if CONFIG_LOOP_FILTER
240
+
241
+static av_cold int init(AVFilterContext *ctx)
242
+{
243
+    LoopContext *s = ctx->priv;
244
+
245
+    s->frames = av_calloc(s->size, sizeof(*s->frames));
246
+    if (!s->frames)
247
+        return AVERROR(ENOMEM);
248
+
249
+    return 0;
250
+}
251
+
252
+static av_cold void uninit(AVFilterContext *ctx)
253
+{
254
+    LoopContext *s = ctx->priv;
255
+    int i;
256
+
257
+    for (i = 0; i < s->nb_frames; i++)
258
+        av_frame_free(&s->frames[i]);
259
+
260
+    av_freep(&s->frames);
261
+    s->nb_frames = 0;
262
+}
263
+
264
+static int push_frame(AVFilterContext *ctx)
265
+{
266
+    AVFilterLink *outlink = ctx->outputs[0];
267
+    LoopContext *s = ctx->priv;
268
+    int64_t pts;
269
+    int ret;
270
+
271
+    AVFrame *out = av_frame_clone(s->frames[s->current_frame]);
272
+
273
+    if (!out)
274
+        return AVERROR(ENOMEM);
275
+    out->pts += s->duration - s->start_pts;
276
+    pts = out->pts + av_frame_get_pkt_duration(out);
277
+    ret = ff_filter_frame(outlink, out);
278
+    s->current_frame++;
279
+
280
+    if (s->current_frame >= s->nb_frames) {
281
+        s->duration = pts;
282
+        s->current_frame = 0;
283
+
284
+        if (s->loop > 0)
285
+            s->loop--;
286
+    }
287
+
288
+    return ret;
289
+}
290
+
291
+static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
292
+{
293
+    AVFilterContext *ctx = inlink->dst;
294
+    AVFilterLink *outlink = ctx->outputs[0];
295
+    LoopContext *s = ctx->priv;
296
+    int ret = 0;
297
+
298
+    if (inlink->frame_count >= s->start && s->size > 0 && s->loop != 0) {
299
+        if (s->nb_frames < s->size) {
300
+            if (!s->nb_frames)
301
+                s->start_pts = frame->pts;
302
+            s->frames[s->nb_frames] = av_frame_clone(frame);
303
+            if (!s->frames[s->nb_frames]) {
304
+                av_frame_free(&frame);
305
+                return AVERROR(ENOMEM);
306
+            }
307
+            s->nb_frames++;
308
+            s->duration = frame->pts + av_frame_get_pkt_duration(frame);
309
+            ret = ff_filter_frame(outlink, frame);
310
+        } else {
311
+            av_frame_free(&frame);
312
+            ret = push_frame(ctx);
313
+        }
314
+    } else {
315
+        frame->pts += s->duration;
316
+        ret = ff_filter_frame(outlink, frame);
317
+    }
318
+
319
+    return ret;
320
+}
321
+
322
+static int request_frame(AVFilterLink *outlink)
323
+{
324
+    AVFilterContext *ctx = outlink->src;
325
+    LoopContext *s = ctx->priv;
326
+    int ret = 0;
327
+
328
+    if ((!s->size) ||
329
+        (s->nb_frames < s->size) ||
330
+        (s->nb_frames >= s->size && s->loop == 0)) {
331
+        ret = ff_request_frame(ctx->inputs[0]);
332
+    } else {
333
+        ret = push_frame(ctx);
334
+    }
335
+
336
+    if (ret == AVERROR_EOF && s->nb_frames > 0 && s->loop != 0) {
337
+        ret = push_frame(ctx);
338
+    }
339
+
340
+    return ret;
341
+}
342
+
343
+static const AVOption loop_options[] = {
344
+    { "loop",  "number of loops",              OFFSET(loop),  AV_OPT_TYPE_INT,   {.i64 = 0 }, -1, INT_MAX,   VFLAGS },
345
+    { "size",  "max number of frames to loop", OFFSET(size),  AV_OPT_TYPE_INT64, {.i64 = 0 },  0, INT16_MAX, VFLAGS },
346
+    { "start", "set the loop start frame",     OFFSET(start), AV_OPT_TYPE_INT64, {.i64 = 0 },  0, INT64_MAX, VFLAGS },
347
+    { NULL }
348
+};
349
+
350
+AVFILTER_DEFINE_CLASS(loop);
351
+
352
+static const AVFilterPad inputs[] = {
353
+    {
354
+        .name         = "default",
355
+        .type         = AVMEDIA_TYPE_VIDEO,
356
+        .filter_frame = filter_frame,
357
+    },
358
+    { NULL }
359
+};
360
+
361
+static const AVFilterPad outputs[] = {
362
+    {
363
+        .name          = "default",
364
+        .type          = AVMEDIA_TYPE_VIDEO,
365
+        .request_frame = request_frame,
366
+    },
367
+    { NULL }
368
+};
369
+
370
+AVFilter ff_vf_loop = {
371
+    .name        = "loop",
372
+    .description = NULL_IF_CONFIG_SMALL("Loop video frames."),
373
+    .priv_size   = sizeof(LoopContext),
374
+    .priv_class  = &loop_class,
375
+    .init        = init,
376
+    .uninit      = uninit,
377
+    .inputs      = inputs,
378
+    .outputs     = outputs,
379
+};
380
+#endif /* CONFIG_LOOP_FILTER */
... ...
@@ -30,7 +30,7 @@
30 30
 #include "libavutil/version.h"
31 31
 
32 32
 #define LIBAVFILTER_VERSION_MAJOR   6
33
-#define LIBAVFILTER_VERSION_MINOR  32
33
+#define LIBAVFILTER_VERSION_MINOR  33
34 34
 #define LIBAVFILTER_VERSION_MICRO 100
35 35
 
36 36
 #define LIBAVFILTER_VERSION_INT AV_VERSION_INT(LIBAVFILTER_VERSION_MAJOR, \
... ...
@@ -155,6 +155,30 @@ int av_audio_fifo_peek(AVAudioFifo *af, void **data, int nb_samples)
155 155
     return nb_samples;
156 156
 }
157 157
 
158
+int av_audio_fifo_peek_at(AVAudioFifo *af, void **data, int nb_samples, int offset)
159
+{
160
+    int i, ret, size;
161
+
162
+    if (offset < 0 || offset >= af->nb_samples)
163
+        return AVERROR(EINVAL);
164
+    if (nb_samples < 0)
165
+        return AVERROR(EINVAL);
166
+    nb_samples = FFMIN(nb_samples, af->nb_samples);
167
+    if (!nb_samples)
168
+        return 0;
169
+    if (offset > af->nb_samples - nb_samples)
170
+        return AVERROR(EINVAL);
171
+
172
+    offset *= af->sample_size;
173
+    size = nb_samples * af->sample_size;
174
+    for (i = 0; i < af->nb_buffers; i++) {
175
+        if ((ret = av_fifo_generic_peek_at(af->buf[i], data[i], offset, size, NULL)) < 0)
176
+            return AVERROR_BUG;
177
+    }
178
+
179
+    return nb_samples;
180
+}
181
+
158 182
 int av_audio_fifo_read(AVAudioFifo *af, void **data, int nb_samples)
159 183
 {
160 184
     int i, ret, size;
... ...
@@ -111,6 +111,23 @@ int av_audio_fifo_write(AVAudioFifo *af, void **data, int nb_samples);
111 111
 int av_audio_fifo_peek(AVAudioFifo *af, void **data, int nb_samples);
112 112
 
113 113
 /**
114
+ * Peek data from an AVAudioFifo.
115
+ *
116
+ * @see enum AVSampleFormat
117
+ * The documentation for AVSampleFormat describes the data layout.
118
+ *
119
+ * @param af          AVAudioFifo to read from
120
+ * @param data        audio data plane pointers
121
+ * @param nb_samples  number of samples to peek
122
+ * @param offset      offset from current read position
123
+ * @return            number of samples actually peek, or negative AVERROR code
124
+ *                    on failure. The number of samples actually peek will not
125
+ *                    be greater than nb_samples, and will only be less than
126
+ *                    nb_samples if av_audio_fifo_size is less than nb_samples.
127
+ */
128
+int av_audio_fifo_peek_at(AVAudioFifo *af, void **data, int nb_samples, int offset);
129
+
130
+/**
114 131
  * Read data from an AVAudioFifo.
115 132
  *
116 133
  * @see enum AVSampleFormat