FFmpeg
 All Data Structures Namespaces 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 
26 #include <float.h>
27 
29 #include "libavutil/common.h"
30 #include "libavutil/fifo.h"
31 #include "libavutil/frame.h"
32 #include "libavutil/imgutils.h"
33 #include "libavutil/internal.h"
34 #include "libavutil/opt.h"
35 #include "libavutil/samplefmt.h"
36 #include "audio.h"
37 #include "avfilter.h"
38 #include "buffersrc.h"
39 #include "formats.h"
40 #include "internal.h"
41 #include "video.h"
42 #include "avcodec.h"
43 
44 typedef struct {
45  const AVClass *class;
47  AVRational time_base; ///< time_base to set in the output link
48  AVRational frame_rate; ///< frame_rate to set in the output link
50  unsigned warning_limit;
51 
52  /* video only */
53  int w, h;
56  char *sws_param;
57 
58  /* audio only */
62  int channels;
63  uint64_t channel_layout;
65 
66  int eof;
68 
69 #define CHECK_VIDEO_PARAM_CHANGE(s, c, width, height, format)\
70  if (c->w != width || c->h != height || c->pix_fmt != format) {\
71  av_log(s, AV_LOG_INFO, "Changing frame properties on the fly is not supported by all filters.\n");\
72  }
73 
74 #define CHECK_AUDIO_PARAM_CHANGE(s, c, srate, ch_layout, ch_count, format)\
75  if (c->sample_fmt != format || c->sample_rate != srate ||\
76  c->channel_layout != ch_layout || c->channels != ch_count) {\
77  av_log(s, AV_LOG_ERROR, "Changing frame properties on the fly is not supported.\n");\
78  return AVERROR(EINVAL);\
79  }
80 
82 {
83  return av_buffersrc_add_frame_flags(ctx, (AVFrame *)frame,
85 }
86 
88 {
89  return av_buffersrc_add_frame_flags(ctx, frame, 0);
90 }
91 
93  AVFrame *frame, int flags);
94 
96 {
97  AVFrame *copy = NULL;
98  int ret = 0;
99 
100  if (frame && frame->channel_layout &&
102  av_log(0, AV_LOG_ERROR, "Layout indicates a different number of channels than actually present\n");
103  return AVERROR(EINVAL);
104  }
105 
106  if (!(flags & AV_BUFFERSRC_FLAG_KEEP_REF) || !frame)
107  return av_buffersrc_add_frame_internal(ctx, frame, flags);
108 
109  if (!(copy = av_frame_alloc()))
110  return AVERROR(ENOMEM);
111  ret = av_frame_ref(copy, frame);
112  if (ret >= 0)
113  ret = av_buffersrc_add_frame_internal(ctx, copy, flags);
114 
115  av_frame_free(&copy);
116  return ret;
117 }
118 
120  AVFrame *frame, int flags)
121 {
122  BufferSourceContext *s = ctx->priv;
123  AVFrame *copy;
124  int ret;
125 
126  s->nb_failed_requests = 0;
127 
128  if (!frame) {
129  s->eof = 1;
130  return 0;
131  } else if (s->eof)
132  return AVERROR(EINVAL);
133 
134  if (!(flags & AV_BUFFERSRC_FLAG_NO_CHECK_FORMAT)) {
135 
136  switch (ctx->outputs[0]->type) {
137  case AVMEDIA_TYPE_VIDEO:
138  CHECK_VIDEO_PARAM_CHANGE(ctx, s, frame->width, frame->height,
139  frame->format);
140  break;
141  case AVMEDIA_TYPE_AUDIO:
142  /* For layouts unknown on input but known on link after negotiation. */
143  if (!frame->channel_layout)
144  frame->channel_layout = s->channel_layout;
145  CHECK_AUDIO_PARAM_CHANGE(ctx, s, frame->sample_rate, frame->channel_layout,
146  av_frame_get_channels(frame), frame->format);
147  break;
148  default:
149  return AVERROR(EINVAL);
150  }
151 
152  }
153 
154  if (!av_fifo_space(s->fifo) &&
155  (ret = av_fifo_realloc2(s->fifo, av_fifo_size(s->fifo) +
156  sizeof(copy))) < 0)
157  return ret;
158 
159  if (!(copy = av_frame_alloc()))
160  return AVERROR(ENOMEM);
161  av_frame_move_ref(copy, frame);
162 
163  if ((ret = av_fifo_generic_write(s->fifo, &copy, sizeof(copy), NULL)) < 0) {
164  av_frame_move_ref(frame, copy);
165  av_frame_free(&copy);
166  return ret;
167  }
168 
169  if ((flags & AV_BUFFERSRC_FLAG_PUSH))
170  if ((ret = ctx->output_pads[0].request_frame(ctx->outputs[0])) < 0)
171  return ret;
172 
173  return 0;
174 }
175 
176 #if FF_API_AVFILTERBUFFER
178 static void compat_free_buffer(void *opaque, uint8_t *data)
179 {
180  AVFilterBufferRef *buf = opaque;
182  avfilter_unref_buffer(buf);
183  )
184 }
185 
186 static void compat_unref_buffer(void *opaque, uint8_t *data)
187 {
188  AVBufferRef *buf = opaque;
190  av_buffer_unref(&buf);
191  )
192 }
193 
194 int av_buffersrc_add_ref(AVFilterContext *ctx, AVFilterBufferRef *buf,
195  int flags)
196 {
197  BufferSourceContext *s = ctx->priv;
198  AVFrame *frame = NULL;
199  AVBufferRef *dummy_buf = NULL;
200  int ret = 0, planes, i;
201 
202  if (!buf) {
203  s->eof = 1;
204  return 0;
205  } else if (s->eof)
206  return AVERROR(EINVAL);
207 
208  frame = av_frame_alloc();
209  if (!frame)
210  return AVERROR(ENOMEM);
211 
212  dummy_buf = av_buffer_create(NULL, 0, compat_free_buffer, buf,
213  (buf->perms & AV_PERM_WRITE) ? 0 : AV_BUFFER_FLAG_READONLY);
214  if (!dummy_buf) {
215  ret = AVERROR(ENOMEM);
216  goto fail;
217  }
218 
220  if ((ret = avfilter_copy_buf_props(frame, buf)) < 0)
221  goto fail;
222  )
223 
224 #define WRAP_PLANE(ref_out, data, data_size) \
225 do { \
226  AVBufferRef *dummy_ref = av_buffer_ref(dummy_buf); \
227  if (!dummy_ref) { \
228  ret = AVERROR(ENOMEM); \
229  goto fail; \
230  } \
231  ref_out = av_buffer_create(data, data_size, compat_unref_buffer, \
232  dummy_ref, (buf->perms & AV_PERM_WRITE) ? 0 : AV_BUFFER_FLAG_READONLY); \
233  if (!ref_out) { \
234  av_frame_unref(frame); \
235  ret = AVERROR(ENOMEM); \
236  goto fail; \
237  } \
238 } while (0)
239 
240  if (ctx->outputs[0]->type == AVMEDIA_TYPE_VIDEO) {
241  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
242 
243  planes = av_pix_fmt_count_planes(frame->format);
244  if (!desc || planes <= 0) {
245  ret = AVERROR(EINVAL);
246  goto fail;
247  }
248 
249  for (i = 0; i < planes; i++) {
250  int v_shift = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
251  int plane_size = (frame->height >> v_shift) * frame->linesize[i];
252 
253  WRAP_PLANE(frame->buf[i], frame->data[i], plane_size);
254  }
255  } else {
256  int planar = av_sample_fmt_is_planar(frame->format);
257  int channels = av_get_channel_layout_nb_channels(frame->channel_layout);
258 
259  planes = planar ? channels : 1;
260 
261  if (planes > FF_ARRAY_ELEMS(frame->buf)) {
262  frame->nb_extended_buf = planes - FF_ARRAY_ELEMS(frame->buf);
263  frame->extended_buf = av_mallocz(sizeof(*frame->extended_buf) *
264  frame->nb_extended_buf);
265  if (!frame->extended_buf) {
266  ret = AVERROR(ENOMEM);
267  goto fail;
268  }
269  }
270 
271  for (i = 0; i < FFMIN(planes, FF_ARRAY_ELEMS(frame->buf)); i++)
272  WRAP_PLANE(frame->buf[i], frame->extended_data[i], frame->linesize[0]);
273 
274  for (i = 0; i < planes - FF_ARRAY_ELEMS(frame->buf); i++)
275  WRAP_PLANE(frame->extended_buf[i],
276  frame->extended_data[i + FF_ARRAY_ELEMS(frame->buf)],
277  frame->linesize[0]);
278  }
279 
280  ret = av_buffersrc_add_frame_flags(ctx, frame, flags);
281 
282 fail:
283  av_buffer_unref(&dummy_buf);
284  av_frame_free(&frame);
285 
286  return ret;
287 }
289 
290 int av_buffersrc_buffer(AVFilterContext *ctx, AVFilterBufferRef *buf)
291 {
292  return av_buffersrc_add_ref(ctx, buf, 0);
293 }
294 #endif
295 
297 {
298  BufferSourceContext *c = ctx->priv;
299 
300  if (c->pix_fmt == AV_PIX_FMT_NONE || !c->w || !c->h || av_q2d(c->time_base) <= 0) {
301  av_log(ctx, AV_LOG_ERROR, "Invalid parameters provided.\n");
302  return AVERROR(EINVAL);
303  }
304 
305  if (!(c->fifo = av_fifo_alloc(sizeof(AVFrame*))))
306  return AVERROR(ENOMEM);
307 
308  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",
309  c->w, c->h, av_get_pix_fmt_name(c->pix_fmt),
311  c->pixel_aspect.num, c->pixel_aspect.den, (char *)av_x_if_null(c->sws_param, ""));
312  c->warning_limit = 100;
313  return 0;
314 }
315 
317 {
318  return ((BufferSourceContext *)buffer_src->priv)->nb_failed_requests;
319 }
320 
321 #define OFFSET(x) offsetof(BufferSourceContext, x)
322 #define A AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_AUDIO_PARAM
323 #define V AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
324 
325 static const AVOption buffer_options[] = {
326  { "width", NULL, OFFSET(w), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, V },
327  { "video_size", NULL, OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, .flags = V },
328  { "height", NULL, OFFSET(h), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, V },
329  { "pix_fmt", NULL, OFFSET(pix_fmt), AV_OPT_TYPE_PIXEL_FMT, .flags = V },
330 #if FF_API_OLD_FILTER_OPTS
331  /* those 4 are for compatibility with the old option passing system where each filter
332  * did its own parsing */
333  { "time_base_num", "deprecated, do not use", OFFSET(time_base.num), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, V },
334  { "time_base_den", "deprecated, do not use", OFFSET(time_base.den), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, V },
335  { "sar_num", "deprecated, do not use", OFFSET(pixel_aspect.num), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, V },
336  { "sar_den", "deprecated, do not use", OFFSET(pixel_aspect.den), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, V },
337 #endif
338  { "sar", "sample aspect ratio", OFFSET(pixel_aspect), AV_OPT_TYPE_RATIONAL, { .dbl = 1 }, 0, DBL_MAX, V },
339  { "pixel_aspect", "sample aspect ratio", OFFSET(pixel_aspect), AV_OPT_TYPE_RATIONAL, { .dbl = 1 }, 0, DBL_MAX, V },
340  { "time_base", NULL, OFFSET(time_base), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, DBL_MAX, V },
341  { "frame_rate", NULL, OFFSET(frame_rate), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, DBL_MAX, V },
342  { "sws_param", NULL, OFFSET(sws_param), AV_OPT_TYPE_STRING, .flags = V },
343  { NULL },
344 };
345 
347 
348 static const AVOption abuffer_options[] = {
349  { "time_base", NULL, OFFSET(time_base), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, INT_MAX, A },
350  { "sample_rate", NULL, OFFSET(sample_rate), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, A },
351  { "sample_fmt", NULL, OFFSET(sample_fmt_str), AV_OPT_TYPE_STRING, .flags = A },
352  { "channel_layout", NULL, OFFSET(channel_layout_str), AV_OPT_TYPE_STRING, .flags = A },
353  { "channels", NULL, OFFSET(channels), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, A },
354  { NULL },
355 };
356 
357 AVFILTER_DEFINE_CLASS(abuffer);
358 
360 {
361  BufferSourceContext *s = ctx->priv;
362  int ret = 0;
363 
365  if (s->sample_fmt == AV_SAMPLE_FMT_NONE) {
366  av_log(ctx, AV_LOG_ERROR, "Invalid sample format %s\n",
367  s->sample_fmt_str);
368  return AVERROR(EINVAL);
369  }
370 
371  if (s->channel_layout_str) {
372  int n;
373  /* TODO reindent */
375  if (!s->channel_layout) {
376  av_log(ctx, AV_LOG_ERROR, "Invalid channel layout %s.\n",
377  s->channel_layout_str);
378  return AVERROR(EINVAL);
379  }
381  if (s->channels) {
382  if (n != s->channels) {
383  av_log(ctx, AV_LOG_ERROR,
384  "Mismatching channel count %d and layout '%s' "
385  "(%d channels)\n",
386  s->channels, s->channel_layout_str, n);
387  return AVERROR(EINVAL);
388  }
389  }
390  s->channels = n;
391  } else if (!s->channels) {
392  av_log(ctx, AV_LOG_ERROR, "Neither number of channels nor "
393  "channel layout specified\n");
394  return AVERROR(EINVAL);
395  }
396 
397  if (!(s->fifo = av_fifo_alloc(sizeof(AVFrame*))))
398  return AVERROR(ENOMEM);
399 
400  if (!s->time_base.num)
401  s->time_base = (AVRational){1, s->sample_rate};
402 
403  av_log(ctx, AV_LOG_VERBOSE,
404  "tb:%d/%d samplefmt:%s samplerate:%d chlayout:%s\n",
407  s->warning_limit = 100;
408 
409  return ret;
410 }
411 
412 static av_cold void uninit(AVFilterContext *ctx)
413 {
414  BufferSourceContext *s = ctx->priv;
415  while (s->fifo && av_fifo_size(s->fifo)) {
416  AVFrame *frame;
417  av_fifo_generic_read(s->fifo, &frame, sizeof(frame), NULL);
418  av_frame_free(&frame);
419  }
420  av_fifo_free(s->fifo);
421  s->fifo = NULL;
422 }
423 
425 {
426  BufferSourceContext *c = ctx->priv;
427  AVFilterChannelLayouts *channel_layouts = NULL;
428  AVFilterFormats *formats = NULL;
429  AVFilterFormats *samplerates = NULL;
430 
431  switch (ctx->outputs[0]->type) {
432  case AVMEDIA_TYPE_VIDEO:
433  ff_add_format(&formats, c->pix_fmt);
434  ff_set_common_formats(ctx, formats);
435  break;
436  case AVMEDIA_TYPE_AUDIO:
437  ff_add_format(&formats, c->sample_fmt);
438  ff_set_common_formats(ctx, formats);
439 
440  ff_add_format(&samplerates, c->sample_rate);
441  ff_set_common_samplerates(ctx, samplerates);
442 
443  ff_add_channel_layout(&channel_layouts,
446  ff_set_common_channel_layouts(ctx, channel_layouts);
447  break;
448  default:
449  return AVERROR(EINVAL);
450  }
451 
452  return 0;
453 }
454 
455 static int config_props(AVFilterLink *link)
456 {
457  BufferSourceContext *c = link->src->priv;
458 
459  switch (link->type) {
460  case AVMEDIA_TYPE_VIDEO:
461  link->w = c->w;
462  link->h = c->h;
464  break;
465  case AVMEDIA_TYPE_AUDIO:
466  if (!c->channel_layout)
467  c->channel_layout = link->channel_layout;
468  break;
469  default:
470  return AVERROR(EINVAL);
471  }
472 
473  link->time_base = c->time_base;
474  link->frame_rate = c->frame_rate;
475  return 0;
476 }
477 
478 static int request_frame(AVFilterLink *link)
479 {
480  BufferSourceContext *c = link->src->priv;
481  AVFrame *frame;
482 
483  if (!av_fifo_size(c->fifo)) {
484  if (c->eof)
485  return AVERROR_EOF;
486  c->nb_failed_requests++;
487  return AVERROR(EAGAIN);
488  }
489  av_fifo_generic_read(c->fifo, &frame, sizeof(frame), NULL);
490 
491  return ff_filter_frame(link, frame);
492 }
493 
494 static int poll_frame(AVFilterLink *link)
495 {
496  BufferSourceContext *c = link->src->priv;
497  int size = av_fifo_size(c->fifo);
498  if (!size && c->eof)
499  return AVERROR_EOF;
500  return size/sizeof(AVFrame*);
501 }
502 
504  {
505  .name = "default",
506  .type = AVMEDIA_TYPE_VIDEO,
507  .request_frame = request_frame,
508  .poll_frame = poll_frame,
509  .config_props = config_props,
510  },
511  { NULL }
512 };
513 
515  .name = "buffer",
516  .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them accessible to the filterchain."),
517  .priv_size = sizeof(BufferSourceContext),
519 
520  .init = init_video,
521  .uninit = uninit,
522 
523  .inputs = NULL,
524  .outputs = avfilter_vsrc_buffer_outputs,
525  .priv_class = &buffer_class,
526 };
527 
529  {
530  .name = "default",
531  .type = AVMEDIA_TYPE_AUDIO,
532  .request_frame = request_frame,
533  .poll_frame = poll_frame,
534  .config_props = config_props,
535  },
536  { NULL }
537 };
538 
540  .name = "abuffer",
541  .description = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them accessible to the filterchain."),
542  .priv_size = sizeof(BufferSourceContext),
544 
545  .init = init_audio,
546  .uninit = uninit,
547 
548  .inputs = NULL,
549  .outputs = avfilter_asrc_abuffer_outputs,
550  .priv_class = &abuffer_class,
551 };