FFmpeg
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 buf_out was allocated for
52 } FifoContext;
53 
54 static av_cold int init(AVFilterContext *ctx, const char *args)
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;
70  av_free(buf);
71  }
72 
74 }
75 
76 static int add_to_queue(AVFilterLink *inlink, AVFilterBufferRef *buf)
77 {
78  FifoContext *fifo = inlink->dst->priv;
79 
80  fifo->last->next = av_mallocz(sizeof(Buf));
81  if (!fifo->last->next) {
83  return AVERROR(ENOMEM);
84  }
85 
86  fifo->last = fifo->last->next;
87  fifo->last->buf = buf;
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(buf->audio->nb_samples > offset);
114 
115  for (i = 0; i < planes; i++)
116  buf->extended_data[i] += block_align*offset;
117  if (buf->data != buf->extended_data)
118  memcpy(buf->data, buf->extended_data,
119  FFMIN(planes, FF_ARRAY_ELEMS(buf->data)) * sizeof(*buf->data));
120  buf->linesize[0] -= block_align*offset;
121  buf->audio->nb_samples -= offset;
122 
123  if (buf->pts != AV_NOPTS_VALUE) {
124  buf->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(buf->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)buf->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  AVFilterBufferRef *head = s->root.next->buf;
151  AVFilterBufferRef *buf_out;
152  int ret;
153 
154  if (!s->buf_out &&
155  head->audio->nb_samples >= link->request_samples &&
156  calc_ptr_alignment(head) >= 32) {
157  if (head->audio->nb_samples == link->request_samples) {
158  buf_out = head;
159  queue_pop(s);
160  } else {
161  buf_out = avfilter_ref_buffer(head, AV_PERM_READ);
162  if (!buf_out)
163  return AVERROR(ENOMEM);
164 
165  buf_out->audio->nb_samples = link->request_samples;
166  buffer_offset(link, head, link->request_samples);
167  }
168  } else {
170 
171  if (!s->buf_out) {
173  link->request_samples);
174  if (!s->buf_out)
175  return AVERROR(ENOMEM);
176 
177  s->buf_out->audio->nb_samples = 0;
178  s->buf_out->pts = head->pts;
180  } else if (link->request_samples != s->allocated_samples) {
181  av_log(ctx, AV_LOG_ERROR, "request_samples changed before the "
182  "buffer was returned.\n");
183  return AVERROR(EINVAL);
184  }
185 
186  while (s->buf_out->audio->nb_samples < s->allocated_samples) {
187  int len;
188 
189  if (!s->root.next &&
190  (ret = ff_request_frame(ctx->inputs[0])) < 0) {
191  if (ret == AVERROR_EOF) {
193  s->buf_out->audio->nb_samples,
194  s->allocated_samples -
195  s->buf_out->audio->nb_samples,
196  nb_channels, link->format);
198  break;
199  }
200  return ret;
201  }
202  head = s->root.next->buf;
203 
205  head->audio->nb_samples);
206 
208  s->buf_out->audio->nb_samples, 0, len, nb_channels,
209  link->format);
210  s->buf_out->audio->nb_samples += len;
211 
212  if (len == head->audio->nb_samples) {
213  avfilter_unref_buffer(head);
214  queue_pop(s);
215  } else {
216  buffer_offset(link, head, len);
217  }
218  }
219  buf_out = s->buf_out;
220  s->buf_out = NULL;
221  }
222  return ff_filter_frame(link, buf_out);
223 }
224 
225 static int request_frame(AVFilterLink *outlink)
226 {
227  FifoContext *fifo = outlink->src->priv;
228  int ret = 0;
229 
230  if (!fifo->root.next) {
231  if ((ret = ff_request_frame(outlink->src->inputs[0])) < 0)
232  return ret;
233  av_assert0(fifo->root.next);
234  }
235 
236  if (outlink->request_samples) {
237  return return_audio_frame(outlink->src);
238  } else {
239  ret = ff_filter_frame(outlink, fifo->root.next->buf);
240  queue_pop(fifo);
241  }
242 
243  return ret;
244 }
245 
247  {
248  .name = "default",
249  .type = AVMEDIA_TYPE_VIDEO,
250  .get_video_buffer = ff_null_get_video_buffer,
251  .filter_frame = add_to_queue,
252  .min_perms = AV_PERM_PRESERVE,
253  },
254  { NULL }
255 };
256 
258  {
259  .name = "default",
260  .type = AVMEDIA_TYPE_VIDEO,
261  .request_frame = request_frame,
262  },
263  { NULL }
264 };
265 
267  .name = "fifo",
268  .description = NULL_IF_CONFIG_SMALL("Buffer input images and send them when they are requested."),
269 
270  .init = init,
271  .uninit = uninit,
272 
273  .priv_size = sizeof(FifoContext),
274 
275  .inputs = avfilter_vf_fifo_inputs,
276  .outputs = avfilter_vf_fifo_outputs,
277 };
278 
280  {
281  .name = "default",
282  .type = AVMEDIA_TYPE_AUDIO,
283  .get_audio_buffer = ff_null_get_audio_buffer,
284  .filter_frame = add_to_queue,
285  .min_perms = AV_PERM_PRESERVE,
286  },
287  { NULL }
288 };
289 
291  {
292  .name = "default",
293  .type = AVMEDIA_TYPE_AUDIO,
294  .request_frame = request_frame,
295  },
296  { NULL }
297 };
298 
300  .name = "afifo",
301  .description = NULL_IF_CONFIG_SMALL("Buffer input frames and send them when they are requested."),
302 
303  .init = init,
304  .uninit = uninit,
305 
306  .priv_size = sizeof(FifoContext),
307 
308  .inputs = avfilter_af_afifo_inputs,
309  .outputs = avfilter_af_afifo_outputs,
310 };