FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
f_loop.c
Go to the documentation of this file.
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 
38  int nb_frames;
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 
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  av_frame_free(&out);
93  return ret;
94  }
95  out->pts = s->pts;
96  out->nb_samples = ret;
97  s->pts += out->nb_samples;
98  i += out->nb_samples;
99  s->current_sample += out->nb_samples;
100 
101  ret = ff_filter_frame(outlink, out);
102  if (ret < 0)
103  return ret;
104 
105  if (s->current_sample >= s->nb_samples) {
106  s->current_sample = 0;
107 
108  if (s->loop > 0)
109  s->loop--;
110  }
111  }
112 
113  return ret;
114 }
115 
116 static int afilter_frame(AVFilterLink *inlink, AVFrame *frame)
117 {
118  AVFilterContext *ctx = inlink->dst;
119  AVFilterLink *outlink = ctx->outputs[0];
120  LoopContext *s = ctx->priv;
121  int ret = 0;
122 
123  if (s->ignored_samples + frame->nb_samples > s->start && s->size > 0 && s->loop != 0) {
124  if (s->nb_samples < s->size) {
125  int written = FFMIN(frame->nb_samples, s->size - s->nb_samples);
126  int drain = 0;
127 
128  ret = av_audio_fifo_write(s->fifo, (void **)frame->extended_data, written);
129  if (ret < 0)
130  return ret;
131  if (!s->nb_samples) {
132  drain = FFMAX(0, s->start - s->ignored_samples);
133  s->pts = frame->pts;
134  av_audio_fifo_drain(s->fifo, drain);
135  s->pts += s->start - s->ignored_samples;
136  }
137  s->nb_samples += ret - drain;
138  drain = frame->nb_samples - written;
139  if (s->nb_samples == s->size && drain > 0) {
140  int ret2;
141 
142  ret2 = av_audio_fifo_write(s->left, (void **)frame->extended_data, frame->nb_samples);
143  if (ret2 < 0)
144  return ret2;
145  av_audio_fifo_drain(s->left, drain);
146  }
147  frame->nb_samples = ret;
148  s->pts += ret;
149  ret = ff_filter_frame(outlink, frame);
150  } else {
151  int nb_samples = frame->nb_samples;
152 
153  av_frame_free(&frame);
154  ret = push_samples(ctx, nb_samples);
155  }
156  } else {
157  s->ignored_samples += frame->nb_samples;
158  frame->pts = s->pts;
159  s->pts += frame->nb_samples;
160  ret = ff_filter_frame(outlink, frame);
161  }
162 
163  return ret;
164 }
165 
166 static int arequest_frame(AVFilterLink *outlink)
167 {
168  AVFilterContext *ctx = outlink->src;
169  LoopContext *s = ctx->priv;
170  int ret = 0;
171 
172  if ((!s->size) ||
173  (s->nb_samples < s->size) ||
174  (s->nb_samples >= s->size && s->loop == 0)) {
175  int nb_samples = av_audio_fifo_size(s->left);
176 
177  if (s->loop == 0 && nb_samples > 0) {
178  AVFrame *out;
179 
180  out = ff_get_audio_buffer(outlink, nb_samples);
181  if (!out)
182  return AVERROR(ENOMEM);
183  av_audio_fifo_read(s->left, (void **)out->extended_data, nb_samples);
184  out->pts = s->pts;
185  s->pts += nb_samples;
186  ret = ff_filter_frame(outlink, out);
187  if (ret < 0)
188  return ret;
189  }
190  ret = ff_request_frame(ctx->inputs[0]);
191  } else {
192  ret = push_samples(ctx, 1024);
193  }
194 
195  if (ret == AVERROR_EOF && s->nb_samples > 0 && s->loop != 0) {
196  ret = push_samples(ctx, outlink->sample_rate);
197  }
198 
199  return ret;
200 }
201 
202 static const AVOption aloop_options[] = {
203  { "loop", "number of loops", OFFSET(loop), AV_OPT_TYPE_INT, {.i64 = 0 }, -1, INT_MAX, AFLAGS },
204  { "size", "max number of samples to loop", OFFSET(size), AV_OPT_TYPE_INT64, {.i64 = 0 }, 0, INT32_MAX, AFLAGS },
205  { "start", "set the loop start sample", OFFSET(start), AV_OPT_TYPE_INT64, {.i64 = 0 }, 0, INT64_MAX, AFLAGS },
206  { NULL }
207 };
208 
209 AVFILTER_DEFINE_CLASS(aloop);
210 
211 static const AVFilterPad ainputs[] = {
212  {
213  .name = "default",
214  .type = AVMEDIA_TYPE_AUDIO,
215  .filter_frame = afilter_frame,
216  .config_props = aconfig_input,
217  },
218  { NULL }
219 };
220 
221 static const AVFilterPad aoutputs[] = {
222  {
223  .name = "default",
224  .type = AVMEDIA_TYPE_AUDIO,
225  .request_frame = arequest_frame,
226  },
227  { NULL }
228 };
229 
230 AVFilter ff_af_aloop = {
231  .name = "aloop",
232  .description = NULL_IF_CONFIG_SMALL("Loop audio samples."),
233  .priv_size = sizeof(LoopContext),
234  .priv_class = &aloop_class,
235  .uninit = auninit,
237  .inputs = ainputs,
238  .outputs = aoutputs,
239 };
240 #endif /* CONFIG_ALOOP_FILTER */
241 
242 #if CONFIG_LOOP_FILTER
243 
244 static av_cold int init(AVFilterContext *ctx)
245 {
246  LoopContext *s = ctx->priv;
247 
248  s->frames = av_calloc(s->size, sizeof(*s->frames));
249  if (!s->frames)
250  return AVERROR(ENOMEM);
251 
252  return 0;
253 }
254 
255 static av_cold void uninit(AVFilterContext *ctx)
256 {
257  LoopContext *s = ctx->priv;
258  int i;
259 
260  for (i = 0; i < s->nb_frames; i++)
261  av_frame_free(&s->frames[i]);
262 
263  av_freep(&s->frames);
264  s->nb_frames = 0;
265 }
266 
267 static int push_frame(AVFilterContext *ctx)
268 {
269  AVFilterLink *outlink = ctx->outputs[0];
270  LoopContext *s = ctx->priv;
271  int64_t pts;
272  int ret;
273 
275 
276  if (!out)
277  return AVERROR(ENOMEM);
278  out->pts += s->duration - s->start_pts;
279  pts = out->pts + av_frame_get_pkt_duration(out);
280  ret = ff_filter_frame(outlink, out);
281  s->current_frame++;
282 
283  if (s->current_frame >= s->nb_frames) {
284  s->duration = pts;
285  s->current_frame = 0;
286 
287  if (s->loop > 0)
288  s->loop--;
289  }
290 
291  return ret;
292 }
293 
294 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
295 {
296  AVFilterContext *ctx = inlink->dst;
297  AVFilterLink *outlink = ctx->outputs[0];
298  LoopContext *s = ctx->priv;
299  int ret = 0;
300 
301  if (inlink->frame_count >= s->start && s->size > 0 && s->loop != 0) {
302  if (s->nb_frames < s->size) {
303  if (!s->nb_frames)
304  s->start_pts = frame->pts;
305  s->frames[s->nb_frames] = av_frame_clone(frame);
306  if (!s->frames[s->nb_frames]) {
307  av_frame_free(&frame);
308  return AVERROR(ENOMEM);
309  }
310  s->nb_frames++;
311  s->duration = frame->pts + av_frame_get_pkt_duration(frame);
312  ret = ff_filter_frame(outlink, frame);
313  } else {
314  av_frame_free(&frame);
315  ret = push_frame(ctx);
316  }
317  } else {
318  frame->pts += s->duration;
319  ret = ff_filter_frame(outlink, frame);
320  }
321 
322  return ret;
323 }
324 
325 static int request_frame(AVFilterLink *outlink)
326 {
327  AVFilterContext *ctx = outlink->src;
328  LoopContext *s = ctx->priv;
329  int ret = 0;
330 
331  if ((!s->size) ||
332  (s->nb_frames < s->size) ||
333  (s->nb_frames >= s->size && s->loop == 0)) {
334  ret = ff_request_frame(ctx->inputs[0]);
335  } else {
336  ret = push_frame(ctx);
337  }
338 
339  if (ret == AVERROR_EOF && s->nb_frames > 0 && s->loop != 0) {
340  ret = push_frame(ctx);
341  }
342 
343  return ret;
344 }
345 
346 static const AVOption loop_options[] = {
347  { "loop", "number of loops", OFFSET(loop), AV_OPT_TYPE_INT, {.i64 = 0 }, -1, INT_MAX, VFLAGS },
348  { "size", "max number of frames to loop", OFFSET(size), AV_OPT_TYPE_INT64, {.i64 = 0 }, 0, INT16_MAX, VFLAGS },
349  { "start", "set the loop start frame", OFFSET(start), AV_OPT_TYPE_INT64, {.i64 = 0 }, 0, INT64_MAX, VFLAGS },
350  { NULL }
351 };
352 
354 
355 static const AVFilterPad inputs[] = {
356  {
357  .name = "default",
358  .type = AVMEDIA_TYPE_VIDEO,
359  .filter_frame = filter_frame,
360  },
361  { NULL }
362 };
363 
364 static const AVFilterPad outputs[] = {
365  {
366  .name = "default",
367  .type = AVMEDIA_TYPE_VIDEO,
368  .request_frame = request_frame,
369  },
370  { NULL }
371 };
372 
373 AVFilter ff_vf_loop = {
374  .name = "loop",
375  .description = NULL_IF_CONFIG_SMALL("Loop video frames."),
376  .priv_size = sizeof(LoopContext),
377  .priv_class = &loop_class,
378  .init = init,
379  .uninit = uninit,
380  .inputs = inputs,
381  .outputs = outputs,
382 };
383 #endif /* CONFIG_LOOP_FILTER */
#define NULL
Definition: coverity.c:32
const char * s
Definition: avisynth_c.h:768
AVAudioFifo * av_audio_fifo_alloc(enum AVSampleFormat sample_fmt, int channels, int nb_samples)
Allocate an AVAudioFifo.
Definition: audio_fifo.c:59
int64_t av_frame_get_pkt_duration(const AVFrame *frame)
int av_audio_fifo_read(AVAudioFifo *af, void **data, int nb_samples)
Read data from an AVAudioFifo.
Definition: audio_fifo.c:181
This structure describes decoded (raw) audio or video data.
Definition: frame.h:184
AVOption.
Definition: opt.h:245
Main libavfilter public API header.
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
void av_audio_fifo_free(AVAudioFifo *af)
Free an AVAudioFifo.
Definition: audio_fifo.c:45
int64_t pts
Definition: f_loop.c:49
int loop
Definition: f_loop.c:46
AVFrame ** frames
Definition: f_loop.c:37
void * av_calloc(size_t nmemb, size_t size)
Non-inlined equivalent of av_mallocz_array().
Definition: mem.c:260
const char * name
Pad name.
Definition: internal.h:59
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:315
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1189
#define av_cold
Definition: attributes.h:82
static av_cold int uninit(AVCodecContext *avctx)
Definition: crystalhd.c:336
AVOptions.
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:268
static AVFrame * frame
int current_frame
Definition: f_loop.c:39
static int push_frame(AVFilterContext *ctx, unsigned in_no, AVFrame *buf)
Definition: avf_concat.c:163
#define AVERROR_EOF
End of file.
Definition: error.h:55
ptrdiff_t size
Definition: opengl_enc.c:101
int64_t current_sample
Definition: f_loop.c:42
A filter pad used for either input or output.
Definition: internal.h:53
AVFrame * ff_get_audio_buffer(AVFilterLink *link, int nb_samples)
Request an audio samples buffer with a specific set of permissions.
Definition: audio.c:64
#define AVERROR(e)
Definition: error.h:43
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:158
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:176
int64_t start
Definition: f_loop.c:48
void * priv
private data for use by the filter
Definition: avfilter.h:322
int nb_frames
Definition: f_loop.c:38
simple assert() macros that are a bit more flexible than ISO C assert().
#define FFMAX(a, b)
Definition: common.h:94
Context for an Audio FIFO Buffer.
Definition: audio_fifo.c:34
common internal API header
int av_audio_fifo_size(AVAudioFifo *af)
Get the current number of samples in the AVAudioFifo available for reading.
Definition: audio_fifo.c:228
static int request_frame(AVFilterLink *outlink)
Definition: aeval.c:274
#define FFMIN(a, b)
Definition: common.h:96
int64_t duration
Definition: f_loop.c:41
int64_t start_pts
Definition: f_loop.c:40
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
Definition: aeval.c:413
AVFormatContext * ctx
Definition: movenc.c:48
#define AFLAGS
Definition: f_loop.c:52
static const AVFilterPad outputs[]
Definition: af_afftfilt.c:386
int64_t size
Definition: f_loop.c:47
AVFrame * av_frame_clone(const AVFrame *src)
Create a new frame that references the same data as src.
Definition: frame.c:480
static const AVFilterPad inputs[]
Definition: af_afftfilt.c:376
static int loop
Definition: ffplay.c:335
a very simple circular buffer FIFO implementation
AVAudioFifo * left
Definition: f_loop.c:36
Describe the class of an AVClass context structure.
Definition: log.h:67
Filter definition.
Definition: avfilter.h:144
int ff_query_formats_all(AVFilterContext *ctx)
Set the formats list to all existing formats.
Definition: formats.c:602
const char * name
Filter name.
Definition: avfilter.h:148
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:319
int64_t nb_samples
Definition: f_loop.c:43
static int64_t pts
Global timestamp for the audio frames.
int av_audio_fifo_write(AVAudioFifo *af, void **data, int nb_samples)
Write data to an AVAudioFifo.
Definition: audio_fifo.c:112
int64_t ignored_samples
Definition: f_loop.c:44
int av_audio_fifo_drain(AVAudioFifo *af, int nb_samples)
Drain data from an AVAudioFifo.
Definition: audio_fifo.c:201
static int query_formats(AVFilterContext *ctx)
Definition: aeval.c:244
static int push_samples(AVFilterLink *outlink)
Audio FIFO Buffer.
#define AVFILTER_DEFINE_CLASS(fname)
Definition: internal.h:339
#define OFFSET(x)
Definition: f_loop.c:54
AVAudioFifo * fifo
Definition: f_loop.c:35
An instance of a filter.
Definition: avfilter.h:307
#define VFLAGS
Definition: f_loop.c:53
FILE * out
Definition: movenc.c:54
#define av_freep(p)
void INT64 start
Definition: avisynth_c.h:690
int ff_request_frame(AVFilterLink *link)
Request an input frame from the filter at the other end of the link.
Definition: avfilter.c:369
internal API functions
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:231
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:241
int av_audio_fifo_peek_at(AVAudioFifo *af, void **data, int nb_samples, int offset)
Peek data from an AVAudioFifo.
Definition: audio_fifo.c:157