libavfilter/fifo.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2007 Bobby Bingham
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 /**
22  * @file
23  * FIFO buffering filter
24  */
25 
26 #include "libavutil/avassert.h"
28 #include "libavutil/common.h"
29 #include "libavutil/mathematics.h"
30 #include "libavutil/samplefmt.h"
31 
32 #include "audio.h"
33 #include "avfilter.h"
34 #include "internal.h"
35 #include "video.h"
36 
37 typedef struct Buf {
39  struct Buf *next;
40 } Buf;
41 
42 typedef struct {
44  Buf *last; ///< last buffered frame
45 
46  /**
47  * When a specific number of output samples is requested, the partial
48  * buffer is stored here
49  */
51  int allocated_samples; ///< number of samples out was allocated for
52 } FifoContext;
53 
54 static av_cold int init(AVFilterContext *ctx)
55 {
56  FifoContext *fifo = ctx->priv;
57  fifo->last = &fifo->root;
58 
59  return 0;
60 }
61 
62 static av_cold void uninit(AVFilterContext *ctx)
63 {
64  FifoContext *fifo = ctx->priv;
65  Buf *buf, *tmp;
66 
67  for (buf = fifo->root.next; buf; buf = tmp) {
68  tmp = buf->next;
69  av_frame_free(&buf->frame);
70  av_free(buf);
71  }
72 
73  av_frame_free(&fifo->out);
74 }
75 
76 static int add_to_queue(AVFilterLink *inlink, AVFrame *frame)
77 {
78  FifoContext *fifo = inlink->dst->priv;
79 
80  fifo->last->next = av_mallocz(sizeof(Buf));
81  if (!fifo->last->next) {
82  av_frame_free(&frame);
83  return AVERROR(ENOMEM);
84  }
85 
86  fifo->last = fifo->last->next;
87  fifo->last->frame = frame;
88 
89  return 0;
90 }
91 
92 static void queue_pop(FifoContext *s)
93 {
94  Buf *tmp = s->root.next->next;
95  if (s->last == s->root.next)
96  s->last = &s->root;
97  av_freep(&s->root.next);
98  s->root.next = tmp;
99 }
100 
101 /**
102  * Move data pointers and pts offset samples forward.
103  */
105  int offset)
106 {
108  int planar = av_sample_fmt_is_planar(link->format);
109  int planes = planar ? nb_channels : 1;
110  int block_align = av_get_bytes_per_sample(link->format) * (planar ? 1 : nb_channels);
111  int i;
112 
113  av_assert0(frame->nb_samples > offset);
114 
115  for (i = 0; i < planes; i++)
116  frame->extended_data[i] += block_align * offset;
117  if (frame->data != frame->extended_data)
118  memcpy(frame->data, frame->extended_data,
119  FFMIN(planes, FF_ARRAY_ELEMS(frame->data)) * sizeof(*frame->data));
120  frame->linesize[0] -= block_align*offset;
121  frame->nb_samples -= offset;
122 
123  if (frame->pts != AV_NOPTS_VALUE) {
124  frame->pts += av_rescale_q(offset, (AVRational){1, link->sample_rate},
125  link->time_base);
126  }
127 }
128 
130 {
131  int planes = av_sample_fmt_is_planar(frame->format) ?
133  int min_align = 128;
134  int p;
135 
136  for (p = 0; p < planes; p++) {
137  int cur_align = 128;
138  while ((intptr_t)frame->extended_data[p] % cur_align)
139  cur_align >>= 1;
140  if (cur_align < min_align)
141  min_align = cur_align;
142  }
143  return min_align;
144 }
145 
147 {
148  AVFilterLink *link = ctx->outputs[0];
149  FifoContext *s = ctx->priv;
150  AVFrame *head = s->root.next->frame;
151  AVFrame *out;
152  int ret;
153 
154  if (!s->out &&
155  head->nb_samples >= link->request_samples &&
156  calc_ptr_alignment(head) >= 32) {
157  if (head->nb_samples == link->request_samples) {
158  out = head;
159  queue_pop(s);
160  } else {
161  out = av_frame_clone(head);
162  if (!out)
163  return AVERROR(ENOMEM);
164 
165  out->nb_samples = link->request_samples;
166  buffer_offset(link, head, link->request_samples);
167  }
168  } else {
170 
171  if (!s->out) {
172  s->out = ff_get_audio_buffer(link, link->request_samples);
173  if (!s->out)
174  return AVERROR(ENOMEM);
175 
176  s->out->nb_samples = 0;
177  s->out->pts = head->pts;
179  } else if (link->request_samples != s->allocated_samples) {
180  av_log(ctx, AV_LOG_ERROR, "request_samples changed before the "
181  "buffer was returned.\n");
182  return AVERROR(EINVAL);
183  }
184 
185  while (s->out->nb_samples < s->allocated_samples) {
186  int len = FFMIN(s->allocated_samples - s->out->nb_samples,
187  head->nb_samples);
188 
190  s->out->nb_samples, 0, len, nb_channels,
191  link->format);
192  s->out->nb_samples += len;
193 
194  if (len == head->nb_samples) {
195  av_frame_free(&head);
196  queue_pop(s);
197 
198  if (!s->root.next &&
199  (ret = ff_request_frame(ctx->inputs[0])) < 0) {
200  if (ret == AVERROR_EOF) {
202  s->out->nb_samples,
203  s->allocated_samples -
204  s->out->nb_samples,
205  nb_channels, link->format);
207  break;
208  }
209  return ret;
210  }
211  head = s->root.next->frame;
212  } else {
213  buffer_offset(link, head, len);
214  }
215  }
216  out = s->out;
217  s->out = NULL;
218  }
219  return ff_filter_frame(link, out);
220 }
221 
222 static int request_frame(AVFilterLink *outlink)
223 {
224  FifoContext *fifo = outlink->src->priv;
225  int ret = 0;
226 
227  if (!fifo->root.next) {
228  if ((ret = ff_request_frame(outlink->src->inputs[0])) < 0)
229  return ret;
230  av_assert0(fifo->root.next);
231  }
232 
233  if (outlink->request_samples) {
234  return return_audio_frame(outlink->src);
235  } else {
236  ret = ff_filter_frame(outlink, fifo->root.next->frame);
237  queue_pop(fifo);
238  }
239 
240  return ret;
241 }
242 
244  {
245  .name = "default",
246  .type = AVMEDIA_TYPE_VIDEO,
247  .get_video_buffer = ff_null_get_video_buffer,
248  .filter_frame = add_to_queue,
249  },
250  { NULL }
251 };
252 
254  {
255  .name = "default",
256  .type = AVMEDIA_TYPE_VIDEO,
257  .request_frame = request_frame,
258  },
259  { NULL }
260 };
261 
263  .name = "fifo",
264  .description = NULL_IF_CONFIG_SMALL("Buffer input images and send them when they are requested."),
265 
266  .init = init,
267  .uninit = uninit,
268 
269  .priv_size = sizeof(FifoContext),
270 
271  .inputs = avfilter_vf_fifo_inputs,
272  .outputs = avfilter_vf_fifo_outputs,
273 };
274 
276  {
277  .name = "default",
278  .type = AVMEDIA_TYPE_AUDIO,
279  .get_audio_buffer = ff_null_get_audio_buffer,
280  .filter_frame = add_to_queue,
281  },
282  { NULL }
283 };
284 
286  {
287  .name = "default",
288  .type = AVMEDIA_TYPE_AUDIO,
289  .request_frame = request_frame,
290  },
291  { NULL }
292 };
293 
295  .name = "afifo",
296  .description = NULL_IF_CONFIG_SMALL("Buffer input frames and send them when they are requested."),
297 
298  .init = init,
299  .uninit = uninit,
300 
301  .priv_size = sizeof(FifoContext),
302 
303  .inputs = avfilter_af_afifo_inputs,
304  .outputs = avfilter_af_afifo_outputs,
305 };
static int calc_ptr_alignment(AVFrame *frame)
AVFrame * out
When a specific number of output samples is requested, the partial buffer is stored here...
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:205
AVFilter avfilter_vf_fifo
const char * s
Definition: avisynth_c.h:668
This structure describes decoded (raw) audio or video data.
Definition: frame.h:76
Buf * last
last buffered frame
static av_cold int init(AVFilterContext *ctx)
static const AVFilterPad outputs[]
Definition: af_ashowinfo.c:117
static const AVFilterPad avfilter_vf_fifo_inputs[]
external API header
AVFilter avfilter_af_afifo
static void queue_pop(FifoContext *s)
AVFrame * ff_null_get_video_buffer(AVFilterLink *link, int w, int h)
Definition: video.c:35
static av_cold void uninit(AVFilterContext *ctx)
#define FF_ARRAY_ELEMS(a)
static int add_to_queue(AVFilterLink *inlink, AVFrame *frame)
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:198
initialize output if(nPeaks >3)%at least 3 peaks in spectrum for trying to find f0 nf0peaks
const char * name
Pad name.
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:532
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
AVFrame * frame
it can be given away to ff_start_frame *A reference passed to ff_filter_frame(or the deprecated ff_start_frame) is given away and must no longer be used.*A reference created with avfilter_ref_buffer belongs to the code that created it.*A reference obtained with ff_get_video_buffer or ff_get_audio_buffer belongs to the code that requested it.*A reference given as return value by the get_video_buffer or get_audio_buffer method is given away and must no longer be used.Link reference fields---------------------The AVFilterLink structure has a few AVFilterBufferRef fields.The cur_buf and out_buf were used with the deprecated start_frame/draw_slice/end_frame API and should no longer be used.src_buf
#define av_cold
Definition: attributes.h:78
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:159
#define AVERROR_EOF
End of file.
Definition: error.h:55
static const AVFilterPad avfilter_vf_fifo_outputs[]
static const AVFilterPad avfilter_af_afifo_inputs[]
A filter pad used for either input or output.
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:130
struct Buf * next
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:183
AVFrame * ff_get_audio_buffer(AVFilterLink *link, int nb_samples)
Request an audio samples buffer with a specific set of permissions.
Definition: audio.c:84
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
void * priv
private data for use by the filter
Definition: avfilter.h:545
int av_get_channel_layout_nb_channels(uint64_t channel_layout)
Return the number of channels in the channel layout.
simple assert() macros that are a bit more flexible than ISO C assert().
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:246
static const uint8_t offset[127][2]
Definition: vf_spp.c:70
int allocated_samples
number of samples out was allocated for
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFilterBuffer structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a link
uint64_t channel_layout
Channel layout of the audio data.
Definition: frame.h:331
audio channel layout utility functions
#define FFMIN(a, b)
Definition: common.h:58
int av_samples_set_silence(uint8_t **audio_data, int offset, int nb_samples, int nb_channels, enum AVSampleFormat sample_fmt)
Fill an audio buffer with silence.
Definition: samplefmt.c:249
ret
Definition: avfilter.c:821
static void buffer_offset(AVFilterLink *link, AVFrame *frame, int offset)
Move data pointers and pts offset samples forward.
AVFrame * ff_null_get_audio_buffer(AVFilterLink *link, int nb_samples)
get_audio_buffer() handler for filters which simply pass audio along
Definition: audio.c:36
int av_get_bytes_per_sample(enum AVSampleFormat sample_fmt)
Return number of bytes per sample.
Definition: samplefmt.c:104
AVFrame * av_frame_clone(AVFrame *src)
Create a new frame that references the same data as src.
Definition: frame.c:317
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...
Definition: frame.h:134
NULL
Definition: eval.c:55
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:101
int av_samples_copy(uint8_t **dst, uint8_t *const *src, int dst_offset, int src_offset, int nb_samples, int nb_channels, enum AVSampleFormat sample_fmt)
Copy samples from src to dst.
Definition: samplefmt.c:225
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:148
void * buf
Definition: avisynth_c.h:594
static int request_frame(AVFilterLink *outlink)
Filter definition.
Definition: avfilter.h:436
int av_sample_fmt_is_planar(enum AVSampleFormat sample_fmt)
Check if the sample format is planar.
Definition: samplefmt.c:118
synthesis window for stochastic i
rational number numerator/denominator
Definition: rational.h:43
const char * name
filter name
Definition: avfilter.h:437
struct Buf Buf
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFilterBuffer structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later.That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another.Buffer references ownership and permissions
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:539
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:87
common internal and external API header
static int return_audio_frame(AVFilterContext *ctx)
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:108
int len
static const AVFilterPad avfilter_af_afifo_outputs[]
An instance of a filter.
Definition: avfilter.h:524
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(const int16_t *) pi >> 8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(const int16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(const int16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(const int32_t *) pi >> 24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(const int32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(const int32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(const float *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(const float *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(const float *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(const double *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(const double *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(const double *) pi *(1U<< 31))))#define SET_CONV_FUNC_GROUP(ofmt, ifmt) static void set_generic_function(AudioConvert *ac){}void ff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, int channels, int sample_rate, int apply_map){AudioConvert *ac;int in_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) return NULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt) > 2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);return NULL;}return ac;}in_planar=av_sample_fmt_is_planar(in_fmt);out_planar=av_sample_fmt_is_planar(out_fmt);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}else if(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;else ac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);return ac;}int ff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){int use_generic=1;int len=in->nb_samples;int p;if(ac->dc){av_dlog(ac->avr,"%d samples - audio_convert: %s to %s (dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));return ff_convert_dither(ac-> out
int ff_request_frame(AVFilterLink *link)
Request an input frame from the filter at the other end of the link.
Definition: avfilter.c:319
int nb_channels
internal API functions
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:117
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several inputs
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:127
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:190