FFmpeg
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
buffersrc.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008 Vitor Sessak
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  * memory buffer source filter
24  */
25 
27 #include "libavutil/common.h"
28 #include "libavutil/fifo.h"
29 #include "libavutil/imgutils.h"
30 #include "libavutil/opt.h"
31 #include "libavutil/samplefmt.h"
32 #include "audio.h"
33 #include "avfilter.h"
34 #include "buffersrc.h"
35 #include "formats.h"
36 #include "internal.h"
37 #include "video.h"
38 #include "avcodec.h"
39 
40 typedef struct {
41  const AVClass *class;
43  AVRational time_base; ///< time_base to set in the output link
44  AVRational frame_rate; ///< frame_rate to set in the output link
46  unsigned warning_limit;
47 
48  /* video only */
49  int w, h;
52  char *sws_param;
53 
54  /* audio only */
58  uint64_t channel_layout;
60 
61  int eof;
63 
64 #define CHECK_VIDEO_PARAM_CHANGE(s, c, width, height, format)\
65  if (c->w != width || c->h != height || c->pix_fmt != format) {\
66  av_log(s, AV_LOG_INFO, "Changing frame properties on the fly is not supported by all filters.\n");\
67  }
68 
69 #define CHECK_AUDIO_PARAM_CHANGE(s, c, srate, ch_layout, format)\
70  if (c->sample_fmt != format || c->sample_rate != srate ||\
71  c->channel_layout != ch_layout) {\
72  av_log(s, AV_LOG_ERROR, "Changing frame properties on the fly is not supported.\n");\
73  return AVERROR(EINVAL);\
74  }
75 
77  const AVFrame *frame, int flags)
78 {
79  AVFilterBufferRef *picref;
80  int ret;
81 
82  if (!frame) /* NULL for EOF */
83  return av_buffersrc_add_ref(buffer_src, NULL, flags);
84 
85  picref = avfilter_get_buffer_ref_from_frame(buffer_src->outputs[0]->type,
86  frame, AV_PERM_WRITE);
87  if (!picref)
88  return AVERROR(ENOMEM);
89  ret = av_buffersrc_add_ref(buffer_src, picref, flags);
90  picref->buf->data[0] = NULL;
91  avfilter_unref_buffer(picref);
92  return ret;
93 }
94 
96 {
97  return av_buffersrc_add_frame(buffer_filter, frame, 0);
98 }
99 
101 {
102  BufferSourceContext *c = s->priv;
103  AVFilterBufferRef *to_free = NULL;
104  int ret;
105 
106  if (!buf) {
107  c->eof = 1;
108  return 0;
109  } else if (c->eof)
110  return AVERROR(EINVAL);
111 
112  if (!av_fifo_space(c->fifo) &&
113  (ret = av_fifo_realloc2(c->fifo, av_fifo_size(c->fifo) +
114  sizeof(buf))) < 0)
115  return ret;
116 
117  if (!(flags & AV_BUFFERSRC_FLAG_NO_CHECK_FORMAT)) {
118  switch (s->outputs[0]->type) {
119  case AVMEDIA_TYPE_VIDEO:
120  CHECK_VIDEO_PARAM_CHANGE(s, c, buf->video->w, buf->video->h, buf->format);
121  break;
122  case AVMEDIA_TYPE_AUDIO:
124  buf->format);
125  break;
126  default:
127  return AVERROR(EINVAL);
128  }
129  }
130  if (!(flags & AV_BUFFERSRC_FLAG_NO_COPY))
131  to_free = buf = ff_copy_buffer_ref(s->outputs[0], buf);
132  if(!buf)
133  return -1;
134 
135  if ((ret = av_fifo_generic_write(c->fifo, &buf, sizeof(buf), NULL)) < 0) {
136  avfilter_unref_buffer(to_free);
137  return ret;
138  }
139  c->nb_failed_requests = 0;
140  if (c->warning_limit &&
141  av_fifo_size(c->fifo) / sizeof(buf) >= c->warning_limit) {
143  "%d buffers queued in %s, something may be wrong.\n",
144  c->warning_limit,
145  (char *)av_x_if_null(s->name, s->filter->name));
146  c->warning_limit *= 10;
147  }
148 
149  if ((flags & AV_BUFFERSRC_FLAG_PUSH))
150  if ((ret = s->output_pads[0].request_frame(s->outputs[0])) < 0)
151  return ret;
152 
153  return 0;
154 }
155 
156 #ifdef FF_API_BUFFERSRC_BUFFER
157 int av_buffersrc_buffer(AVFilterContext *s, AVFilterBufferRef *buf)
158 {
160 }
161 #endif
162 
164 {
165  return ((BufferSourceContext *)buffer_src->priv)->nb_failed_requests;
166 }
167 
168 #define OFFSET(x) offsetof(BufferSourceContext, x)
169 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
170 static const AVOption buffer_options[] = {
171  { "time_base", NULL, OFFSET(time_base), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, INT_MAX, FLAGS },
172  { "frame_rate", NULL, OFFSET(frame_rate), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, INT_MAX, FLAGS },
173  { "video_size", NULL, OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, .flags = FLAGS },
174  { "pix_fmt", NULL, OFFSET(pix_fmt), AV_OPT_TYPE_PIXEL_FMT, .flags = FLAGS },
175  { "pixel_aspect", NULL, OFFSET(pixel_aspect), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, INT_MAX, FLAGS },
176  { "sws_param", NULL, OFFSET(sws_param), AV_OPT_TYPE_STRING, .flags = FLAGS },
177  { NULL },
178 };
179 #undef FLAGS
180 
182 
183 static av_cold int init_video(AVFilterContext *ctx, const char *args)
184 {
185  BufferSourceContext *c = ctx->priv;
186  char pix_fmt_str[128], sws_param[256] = "", *colon, *equal;
187  int ret, n = 0;
188 
189  c->class = &buffer_class;
190 
191  if (!args) {
192  av_log(ctx, AV_LOG_ERROR, "Arguments required\n");
193  return AVERROR(EINVAL);
194  }
195  colon = strchr(args, ':');
196  equal = strchr(args, '=');
197  if (equal && (!colon || equal < colon)) {
199  ret = av_set_options_string(c, args, "=", ":");
200  if (ret < 0)
201  goto fail;
202  } else {
203  if ((n = sscanf(args, "%d:%d:%127[^:]:%d:%d:%d:%d:%255c", &c->w, &c->h, pix_fmt_str,
204  &c->time_base.num, &c->time_base.den,
205  &c->pixel_aspect.num, &c->pixel_aspect.den, sws_param)) < 7) {
206  av_log(ctx, AV_LOG_ERROR, "Expected at least 7 arguments, but only %d found in '%s'\n", n, args);
207  ret = AVERROR(EINVAL);
208  goto fail;
209  }
210  av_log(ctx, AV_LOG_WARNING, "Flat options syntax is deprecated, use key=value pairs\n");
211 
212  if ((ret = ff_parse_pixel_format(&c->pix_fmt, pix_fmt_str, ctx)) < 0)
213  goto fail;
214  c->sws_param = av_strdup(sws_param);
215  if (!c->sws_param) {
216  ret = AVERROR(ENOMEM);
217  goto fail;
218  }
219  }
220 
221  if (!(c->fifo = av_fifo_alloc(sizeof(AVFilterBufferRef*)))) {
222  ret = AVERROR(ENOMEM);
223  goto fail;
224  }
225 
226  av_log(ctx, AV_LOG_VERBOSE, "w:%d h:%d pixfmt:%s tb:%d/%d fr:%d/%d sar:%d/%d sws_param:%s\n",
227  c->w, c->h, av_get_pix_fmt_name(c->pix_fmt),
229  c->pixel_aspect.num, c->pixel_aspect.den, (char *)av_x_if_null(c->sws_param, ""));
230  c->warning_limit = 100;
231  return 0;
232 
233 fail:
234  av_opt_free(c);
235  return ret;
236 }
237 
238 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_AUDIO_PARAM
239 static const AVOption abuffer_options[] = {
240  { "time_base", NULL, OFFSET(time_base), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, INT_MAX, FLAGS },
241  { "sample_rate", NULL, OFFSET(sample_rate), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS },
242  { "sample_fmt", NULL, OFFSET(sample_fmt_str), AV_OPT_TYPE_STRING, .flags = FLAGS },
243  { "channel_layout", NULL, OFFSET(channel_layout_str), AV_OPT_TYPE_STRING, .flags = FLAGS },
244  { NULL },
245 };
246 
247 AVFILTER_DEFINE_CLASS(abuffer);
248 
249 static av_cold int init_audio(AVFilterContext *ctx, const char *args)
250 {
251  BufferSourceContext *s = ctx->priv;
252  int ret = 0;
253 
254  s->class = &abuffer_class;
256 
257  if ((ret = av_set_options_string(s, args, "=", ":")) < 0)
258  goto fail;
259 
261  if (s->sample_fmt == AV_SAMPLE_FMT_NONE) {
262  av_log(ctx, AV_LOG_ERROR, "Invalid sample format '%s'\n",
263  s->sample_fmt_str);
264  ret = AVERROR(EINVAL);
265  goto fail;
266  }
267 
269  if (!s->channel_layout) {
270  av_log(ctx, AV_LOG_ERROR, "Invalid channel layout '%s'\n",
271  s->channel_layout_str);
272  ret = AVERROR(EINVAL);
273  goto fail;
274  }
275 
276  if (!(s->fifo = av_fifo_alloc(sizeof(AVFilterBufferRef*)))) {
277  ret = AVERROR(ENOMEM);
278  goto fail;
279  }
280 
281  if (!s->time_base.num)
282  s->time_base = (AVRational){1, s->sample_rate};
283 
284  av_log(ctx, AV_LOG_VERBOSE,
285  "tb:%d/%d samplefmt:%s samplerate:%d chlayout:%s\n",
288  s->warning_limit = 100;
289 
290 fail:
291  av_opt_free(s);
292  return ret;
293 }
294 
295 static av_cold void uninit(AVFilterContext *ctx)
296 {
297  BufferSourceContext *s = ctx->priv;
298  while (s->fifo && av_fifo_size(s->fifo)) {
299  AVFilterBufferRef *buf;
300  av_fifo_generic_read(s->fifo, &buf, sizeof(buf), NULL);
302  }
303  av_fifo_free(s->fifo);
304  s->fifo = NULL;
305  av_freep(&s->sws_param);
306 }
307 
309 {
310  BufferSourceContext *c = ctx->priv;
311  AVFilterChannelLayouts *channel_layouts = NULL;
313  AVFilterFormats *samplerates = NULL;
314 
315  switch (ctx->outputs[0]->type) {
316  case AVMEDIA_TYPE_VIDEO:
317  ff_add_format(&formats, c->pix_fmt);
318  ff_set_common_formats(ctx, formats);
319  break;
320  case AVMEDIA_TYPE_AUDIO:
321  ff_add_format(&formats, c->sample_fmt);
322  ff_set_common_formats(ctx, formats);
323 
324  ff_add_format(&samplerates, c->sample_rate);
325  ff_set_common_samplerates(ctx, samplerates);
326 
327  ff_add_channel_layout(&channel_layouts, c->channel_layout);
328  ff_set_common_channel_layouts(ctx, channel_layouts);
329  break;
330  default:
331  return AVERROR(EINVAL);
332  }
333 
334  return 0;
335 }
336 
337 static int config_props(AVFilterLink *link)
338 {
339  BufferSourceContext *c = link->src->priv;
340 
341  switch (link->type) {
342  case AVMEDIA_TYPE_VIDEO:
343  link->w = c->w;
344  link->h = c->h;
346  break;
347  case AVMEDIA_TYPE_AUDIO:
348  break;
349  default:
350  return AVERROR(EINVAL);
351  }
352 
353  link->time_base = c->time_base;
354  link->frame_rate = c->frame_rate;
355  return 0;
356 }
357 
358 static int request_frame(AVFilterLink *link)
359 {
360  BufferSourceContext *c = link->src->priv;
361  AVFilterBufferRef *buf;
362  int ret = 0;
363 
364  if (!av_fifo_size(c->fifo)) {
365  if (c->eof)
366  return AVERROR_EOF;
367  c->nb_failed_requests++;
368  return AVERROR(EAGAIN);
369  }
370  av_fifo_generic_read(c->fifo, &buf, sizeof(buf), NULL);
371 
372  ff_filter_frame(link, buf);
373 
374  return ret;
375 }
376 
377 static int poll_frame(AVFilterLink *link)
378 {
379  BufferSourceContext *c = link->src->priv;
380  int size = av_fifo_size(c->fifo);
381  if (!size && c->eof)
382  return AVERROR_EOF;
383  return size/sizeof(AVFilterBufferRef*);
384 }
385 
387  {
388  .name = "default",
389  .type = AVMEDIA_TYPE_VIDEO,
390  .request_frame = request_frame,
391  .poll_frame = poll_frame,
392  .config_props = config_props,
393  },
394  { NULL }
395 };
396 
398  .name = "buffer",
399  .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them accessible to the filterchain."),
400  .priv_size = sizeof(BufferSourceContext),
402 
403  .init = init_video,
404  .uninit = uninit,
405 
406  .inputs = NULL,
407  .outputs = avfilter_vsrc_buffer_outputs,
408  .priv_class = &buffer_class,
409 };
410 
412  {
413  .name = "default",
414  .type = AVMEDIA_TYPE_AUDIO,
415  .request_frame = request_frame,
416  .poll_frame = poll_frame,
417  .config_props = config_props,
418  },
419  { NULL }
420 };
421 
423  .name = "abuffer",
424  .description = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them accessible to the filterchain."),
425  .priv_size = sizeof(BufferSourceContext),
427 
428  .init = init_audio,
429  .uninit = uninit,
430 
431  .inputs = NULL,
432  .outputs = avfilter_asrc_abuffer_outputs,
433  .priv_class = &abuffer_class,
434 };