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 
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,
236  .inputs = ainputs,
237  .outputs = aoutputs,
238 };
239 #endif /* CONFIG_ALOOP_FILTER */
240 
241 #if CONFIG_LOOP_FILTER
242 
243 static av_cold int init(AVFilterContext *ctx)
244 {
245  LoopContext *s = ctx->priv;
246 
247  s->frames = av_calloc(s->size, sizeof(*s->frames));
248  if (!s->frames)
249  return AVERROR(ENOMEM);
250 
251  return 0;
252 }
253 
254 static av_cold void uninit(AVFilterContext *ctx)
255 {
256  LoopContext *s = ctx->priv;
257  int i;
258 
259  for (i = 0; i < s->nb_frames; i++)
260  av_frame_free(&s->frames[i]);
261 
262  av_freep(&s->frames);
263  s->nb_frames = 0;
264 }
265 
266 static int push_frame(AVFilterContext *ctx)
267 {
268  AVFilterLink *outlink = ctx->outputs[0];
269  LoopContext *s = ctx->priv;
270  int64_t pts;
271  int ret;
272 
274 
275  if (!out)
276  return AVERROR(ENOMEM);
277  out->pts += s->duration - s->start_pts;
278  pts = out->pts + out->pkt_duration;
279  ret = ff_filter_frame(outlink, out);
280  s->current_frame++;
281 
282  if (s->current_frame >= s->nb_frames) {
283  s->duration = pts;
284  s->current_frame = 0;
285 
286  if (s->loop > 0)
287  s->loop--;
288  }
289 
290  return ret;
291 }
292 
293 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
294 {
295  AVFilterContext *ctx = inlink->dst;
296  AVFilterLink *outlink = ctx->outputs[0];
297  LoopContext *s = ctx->priv;
298  int ret = 0;
299 
300  if (inlink->frame_count_out >= s->start && s->size > 0 && s->loop != 0) {
301  if (s->nb_frames < s->size) {
302  if (!s->nb_frames)
303  s->start_pts = frame->pts;
304  s->frames[s->nb_frames] = av_frame_clone(frame);
305  if (!s->frames[s->nb_frames]) {
306  av_frame_free(&frame);
307  return AVERROR(ENOMEM);
308  }
309  s->nb_frames++;
310  s->duration = frame->pts + frame->pkt_duration;
311  ret = ff_filter_frame(outlink, frame);
312  } else {
313  av_frame_free(&frame);
314  ret = push_frame(ctx);
315  }
316  } else {
317  frame->pts += s->duration;
318  ret = ff_filter_frame(outlink, frame);
319  }
320 
321  return ret;
322 }
323 
324 static int request_frame(AVFilterLink *outlink)
325 {
326  AVFilterContext *ctx = outlink->src;
327  LoopContext *s = ctx->priv;
328  int ret = 0;
329 
330  if ((!s->size) ||
331  (s->nb_frames < s->size) ||
332  (s->nb_frames >= s->size && s->loop == 0)) {
333  ret = ff_request_frame(ctx->inputs[0]);
334  } else {
335  ret = push_frame(ctx);
336  }
337 
338  if (ret == AVERROR_EOF && s->nb_frames > 0 && s->loop != 0) {
339  ret = push_frame(ctx);
340  }
341 
342  return ret;
343 }
344 
345 static const AVOption loop_options[] = {
346  { "loop", "number of loops", OFFSET(loop), AV_OPT_TYPE_INT, {.i64 = 0 }, -1, INT_MAX, VFLAGS },
347  { "size", "max number of frames to loop", OFFSET(size), AV_OPT_TYPE_INT64, {.i64 = 0 }, 0, INT16_MAX, VFLAGS },
348  { "start", "set the loop start frame", OFFSET(start), AV_OPT_TYPE_INT64, {.i64 = 0 }, 0, INT64_MAX, VFLAGS },
349  { NULL }
350 };
351 
353 
354 static const AVFilterPad inputs[] = {
355  {
356  .name = "default",
357  .type = AVMEDIA_TYPE_VIDEO,
358  .filter_frame = filter_frame,
359  },
360  { NULL }
361 };
362 
363 static const AVFilterPad outputs[] = {
364  {
365  .name = "default",
366  .type = AVMEDIA_TYPE_VIDEO,
367  .request_frame = request_frame,
368  },
369  { NULL }
370 };
371 
373  .name = "loop",
374  .description = NULL_IF_CONFIG_SMALL("Loop video frames."),
375  .priv_size = sizeof(LoopContext),
376  .priv_class = &loop_class,
377  .init = init,
378  .uninit = uninit,
379  .inputs = inputs,
380  .outputs = outputs,
381 };
382 #endif /* CONFIG_LOOP_FILTER */
#define NULL
Definition: coverity.c:32
static int push_samples(ATempoContext *atempo, AVFilterLink *outlink, int n_out)
Definition: af_atempo.c:1061
AVAudioFifo * av_audio_fifo_alloc(enum AVSampleFormat sample_fmt, int channels, int nb_samples)
Allocate an AVAudioFifo.
Definition: audio_fifo.c:59
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:226
AVOption.
Definition: opt.h:246
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:244
const char * name
Pad name.
Definition: internal.h:60
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:346
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1080
#define av_cold
Definition: attributes.h:82
static av_cold int uninit(AVCodecContext *avctx)
Definition: crystalhd.c:279
AVOptions.
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:319
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:161
#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:54
AVFrame * ff_get_audio_buffer(AVFilterLink *link, int nb_samples)
Request an audio samples buffer with a specific set of permissions.
Definition: audio.c:86
#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:202
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:186
int64_t start
Definition: f_loop.c:48
void * priv
private data for use by the filter
Definition: avfilter.h:353
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
AVFormatContext * ctx
Definition: movenc.c:48
#define s(width, name)
Definition: cbs_vp9.c:257
#define AFLAGS
Definition: f_loop.c:52
int64_t size
Definition: f_loop.c:47
static const AVFilterPad inputs[]
Definition: af_acontrast.c:193
AVFrame * av_frame_clone(const AVFrame *src)
Create a new frame that references the same data as src.
Definition: frame.c:540
static const AVFilterPad outputs[]
Definition: af_acontrast.c:203
int64_t pkt_duration
duration of the corresponding packet, expressed in AVStream->time_base units, 0 if unknown...
Definition: frame.h:506
static int loop
Definition: ffplay.c:339
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
const char * name
Filter name.
Definition: avfilter.h:148
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:350
int64_t nb_samples
Definition: f_loop.c:43
static int64_t pts
static int filter_frame(DBEContext *s, AVFrame *frame)
Definition: dolby_e.c:565
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
AVFilter ff_vf_loop
Audio FIFO Buffer.
#define AVFILTER_DEFINE_CLASS(fname)
Definition: internal.h:334
#define OFFSET(x)
Definition: f_loop.c:54
AVAudioFifo * fifo
Definition: f_loop.c:35
An instance of a filter.
Definition: avfilter.h:338
#define VFLAGS
Definition: f_loop.c:53
FILE * out
Definition: movenc.c:54
AVFilter ff_af_aloop
#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:407
internal API functions
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:273
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:292
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