History

avfilter: add loop filters

Paul B Mahol committed 10 years ago

Commit 08acab81 parent 5590ab49 files · +450 −1Browse files

Unified · Split

9 files
  1. Changelog +1 −0
  2. doc/APIchanges +3 −0
  3. doc/filters.texi +19 −0
  4. libavfilter/Makefile +2 −0
  5. libavfilter/allfilters.c +2 −0
  6. libavfilter/f_loop.c +381 −0
  7. libavfilter/version.h +1 −1
  8. libavutil/audio_fifo.c +24 −0
  9. libavutil/audio_fifo.h +17 −0
Changelog+1−0
@@ -4,6 +4,7 @@ releases are sorted from youngest to oldest. · 3 unmodified lines
4version <next>:4version <next>:
5- DXVA2-accelerated HEVC Main10 decoding5- DXVA2-accelerated HEVC Main10 decoding
6- fieldhint filter6- fieldhint filter
7- loop video filter and aloop audio filter
7 8
8 9
9version 3.0:10version 3.0:
doc/APIchanges+3−0
@@ -15,6 +15,9 @@ libavutil: 2015-08-28 · 14 unmodified lines
15 15
16API changes, most recent first:16API changes, most recent first:
17 17
182016-xx-xx - lavu 55.18.100
19 xxxxxxx audio_fifo.h - Add av_audio_fifo_peek_at().
20
182016-xx-xx - lavu 55.18.0212016-xx-xx - lavu 55.18.0
19 xxxxxxx buffer.h - Add av_buffer_pool_init2().22 xxxxxxx buffer.h - Add av_buffer_pool_init2().
20 xxxxxxx hwcontext.h - Add a new installed header hwcontext.h with a new API23 xxxxxxx hwcontext.h - Add a new installed header hwcontext.h with a new API
doc/filters.texi+19−0
@@ -8185,6 +8185,25 @@ The formula that generates the correction is: · 8184 unmodified lines
8185where @var{r_0} is halve of the image diagonal and @var{r_src} and @var{r_tgt} are the8185where @var{r_0} is halve of the image diagonal and @var{r_src} and @var{r_tgt} are the
8186distances from the focal point in the source and target images, respectively.8186distances from the focal point in the source and target images, respectively.
8187 8187
8188@section loop, aloop
8189
8190Loop video frames or audio samples.
8191
8192Those filters accepts the following options:
8193
8194@table @option
8195@item loop
8196Set the number of loops.
8197
8198@item size
8199Set maximal size in number of frames for @code{loop} filter or maximal number
8200of samples in case of @code{aloop} filter.
8201
8202@item start
8203Set first frame of loop for @code{loop} filter or first sample of loop in case
8204of @code{aloop} filter.
8205@end table
8206
8188@anchor{lut3d}8207@anchor{lut3d}
8189@section lut3d8208@section lut3d
8190 8209
libavfilter/Makefile+2−0
@@ -38,6 +38,7 @@ OBJS-$(CONFIG_AGATE_FILTER) += af_agate.o · 37 unmodified lines
38OBJS-$(CONFIG_AINTERLEAVE_FILTER) += f_interleave.o38OBJS-$(CONFIG_AINTERLEAVE_FILTER) += f_interleave.o
39OBJS-$(CONFIG_ALIMITER_FILTER) += af_alimiter.o39OBJS-$(CONFIG_ALIMITER_FILTER) += af_alimiter.o
40OBJS-$(CONFIG_ALLPASS_FILTER) += af_biquads.o40OBJS-$(CONFIG_ALLPASS_FILTER) += af_biquads.o
41OBJS-$(CONFIG_ALOOP_FILTER) += f_loop.o
41OBJS-$(CONFIG_AMERGE_FILTER) += af_amerge.o42OBJS-$(CONFIG_AMERGE_FILTER) += af_amerge.o
42OBJS-$(CONFIG_AMETADATA_FILTER) += f_metadata.o43OBJS-$(CONFIG_AMETADATA_FILTER) += f_metadata.o
43OBJS-$(CONFIG_AMIX_FILTER) += af_amix.o44OBJS-$(CONFIG_AMIX_FILTER) += af_amix.o
@@ -181,6 +182,7 @@ OBJS-$(CONFIG_INTERLACE_FILTER) += vf_interlace.o · 137 unmodified lines
181OBJS-$(CONFIG_INTERLEAVE_FILTER) += f_interleave.o182OBJS-$(CONFIG_INTERLEAVE_FILTER) += f_interleave.o
182OBJS-$(CONFIG_KERNDEINT_FILTER) += vf_kerndeint.o183OBJS-$(CONFIG_KERNDEINT_FILTER) += vf_kerndeint.o
183OBJS-$(CONFIG_LENSCORRECTION_FILTER) += vf_lenscorrection.o184OBJS-$(CONFIG_LENSCORRECTION_FILTER) += vf_lenscorrection.o
185OBJS-$(CONFIG_LOOP_FILTER) += f_loop.o
184OBJS-$(CONFIG_LUT3D_FILTER) += vf_lut3d.o186OBJS-$(CONFIG_LUT3D_FILTER) += vf_lut3d.o
185OBJS-$(CONFIG_LUT_FILTER) += vf_lut.o187OBJS-$(CONFIG_LUT_FILTER) += vf_lut.o
186OBJS-$(CONFIG_LUTRGB_FILTER) += vf_lut.o188OBJS-$(CONFIG_LUTRGB_FILTER) += vf_lut.o
libavfilter/allfilters.c+2−0
@@ -58,6 +58,7 @@ void avfilter_register_all(void) · 57 unmodified lines
58 REGISTER_FILTER(AINTERLEAVE, ainterleave, af);58 REGISTER_FILTER(AINTERLEAVE, ainterleave, af);
59 REGISTER_FILTER(ALIMITER, alimiter, af);59 REGISTER_FILTER(ALIMITER, alimiter, af);
60 REGISTER_FILTER(ALLPASS, allpass, af);60 REGISTER_FILTER(ALLPASS, allpass, af);
61 REGISTER_FILTER(ALOOP, aloop, af);
61 REGISTER_FILTER(AMERGE, amerge, af);62 REGISTER_FILTER(AMERGE, amerge, af);
62 REGISTER_FILTER(AMETADATA, ametadata, af);63 REGISTER_FILTER(AMETADATA, ametadata, af);
63 REGISTER_FILTER(AMIX, amix, af);64 REGISTER_FILTER(AMIX, amix, af);
@@ -202,6 +203,7 @@ void avfilter_register_all(void) · 138 unmodified lines
202 REGISTER_FILTER(INTERLEAVE, interleave, vf);203 REGISTER_FILTER(INTERLEAVE, interleave, vf);
203 REGISTER_FILTER(KERNDEINT, kerndeint, vf);204 REGISTER_FILTER(KERNDEINT, kerndeint, vf);
204 REGISTER_FILTER(LENSCORRECTION, lenscorrection, vf);205 REGISTER_FILTER(LENSCORRECTION, lenscorrection, vf);
206 REGISTER_FILTER(LOOP, loop, vf);
205 REGISTER_FILTER(LUT3D, lut3d, vf);207 REGISTER_FILTER(LUT3D, lut3d, vf);
206 REGISTER_FILTER(LUT, lut, vf);208 REGISTER_FILTER(LUT, lut, vf);
207 REGISTER_FILTER(LUTRGB, lutrgb, vf);209 REGISTER_FILTER(LUTRGB, lutrgb, vf);
libavfilter/f_loop.c+381−0
@@ -0,0 +1,381 @@
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 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
114static 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
164static 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
200static 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
207AVFILTER_DEFINE_CLASS(aloop);
208
209static 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
219static const AVFilterPad aoutputs[] = {
220 {
221 .name = "default",
222 .type = AVMEDIA_TYPE_AUDIO,
223 .request_frame = arequest_frame,
224 },
225 { NULL }
226};
227
228AVFilter 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
242static 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
253static 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
265static 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
292static 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
323static 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
344static 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
351AVFILTER_DEFINE_CLASS(loop);
352
353static const AVFilterPad inputs[] = {
354 {
355 .name = "default",
356 .type = AVMEDIA_TYPE_VIDEO,
357 .filter_frame = filter_frame,
358 },
359 { NULL }
360};
361
362static const AVFilterPad outputs[] = {
363 {
364 .name = "default",
365 .type = AVMEDIA_TYPE_VIDEO,
366 .request_frame = request_frame,
367 },
368 { NULL }
369};
370
371AVFilter 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 */
libavfilter/version.h+1−1
@@ -30,7 +30,7 @@ · 29 unmodified lines
30#include "libavutil/version.h"30#include "libavutil/version.h"
31 31
32#define LIBAVFILTER_VERSION_MAJOR 632#define LIBAVFILTER_VERSION_MAJOR 6
33#define LIBAVFILTER_VERSION_MINOR 3233#define LIBAVFILTER_VERSION_MINOR 33
34#define LIBAVFILTER_VERSION_MICRO 10034#define LIBAVFILTER_VERSION_MICRO 100
35 35
36#define LIBAVFILTER_VERSION_INT AV_VERSION_INT(LIBAVFILTER_VERSION_MAJOR, \36#define LIBAVFILTER_VERSION_INT AV_VERSION_INT(LIBAVFILTER_VERSION_MAJOR, \
libavutil/audio_fifo.c+24−0
@@ -155,6 +155,30 @@ int av_audio_fifo_peek(AVAudioFifo *af, void **data, int nb_samples) · 154 unmodified lines
155 return nb_samples;155 return nb_samples;
156}156}
157 157
158int 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
158int av_audio_fifo_read(AVAudioFifo *af, void **data, int nb_samples)182int av_audio_fifo_read(AVAudioFifo *af, void **data, int nb_samples)
159{183{
160 int i, ret, size;184 int i, ret, size;
libavutil/audio_fifo.h+17−0
@@ -110,6 +110,23 @@ int av_audio_fifo_write(AVAudioFifo *af, void **data, int nb_samples); · 109 unmodified lines
110 */110 */
111int av_audio_fifo_peek(AVAudioFifo *af, void **data, int nb_samples);111int av_audio_fifo_peek(AVAudioFifo *af, void **data, int nb_samples);
112 112
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 */
128int av_audio_fifo_peek_at(AVAudioFifo *af, void **data, int nb_samples, int offset);
129
113/**130/**
114 * Read data from an AVAudioFifo.131 * Read data from an AVAudioFifo.
115 *132 *

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t