Libav
avfilter.c
Go to the documentation of this file.
1 /*
2  * filter layer
3  * Copyright (c) 2007 Bobby Bingham
4  *
5  * This file is part of Libav.
6  *
7  * Libav is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * Libav is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 #include "libavutil/avstring.h"
23 #include "libavutil/buffer.h"
25 #include "libavutil/common.h"
26 #include "libavutil/hwcontext.h"
27 #include "libavutil/imgutils.h"
28 #include "libavutil/internal.h"
29 #include "libavutil/opt.h"
30 #include "libavutil/pixdesc.h"
31 #include "libavutil/rational.h"
32 #include "libavutil/samplefmt.h"
33 
34 #include "audio.h"
35 #include "avfilter.h"
36 #include "formats.h"
37 #include "internal.h"
38 #include "video.h"
39 
40 unsigned avfilter_version(void)
41 {
43 }
44 
45 const char *avfilter_configuration(void)
46 {
47  return LIBAV_CONFIGURATION;
48 }
49 
50 const char *avfilter_license(void)
51 {
52 #define LICENSE_PREFIX "libavfilter license: "
53  return LICENSE_PREFIX LIBAV_LICENSE + sizeof(LICENSE_PREFIX) - 1;
54 }
55 
56 void ff_insert_pad(unsigned idx, unsigned *count, size_t padidx_off,
57  AVFilterPad **pads, AVFilterLink ***links,
58  AVFilterPad *newpad)
59 {
60  unsigned i;
61 
62  idx = FFMIN(idx, *count);
63 
64  *pads = av_realloc(*pads, sizeof(AVFilterPad) * (*count + 1));
65  *links = av_realloc(*links, sizeof(AVFilterLink*) * (*count + 1));
66  memmove(*pads + idx + 1, *pads + idx, sizeof(AVFilterPad) * (*count - idx));
67  memmove(*links + idx + 1, *links + idx, sizeof(AVFilterLink*) * (*count - idx));
68  memcpy(*pads + idx, newpad, sizeof(AVFilterPad));
69  (*links)[idx] = NULL;
70 
71  (*count)++;
72  for (i = idx + 1; i < *count; i++)
73  if (*links[i])
74  (*(unsigned *)((uint8_t *) *links[i] + padidx_off))++;
75 }
76 
77 int avfilter_link(AVFilterContext *src, unsigned srcpad,
78  AVFilterContext *dst, unsigned dstpad)
79 {
80  AVFilterLink *link;
81 
82  if (src->nb_outputs <= srcpad || dst->nb_inputs <= dstpad ||
83  src->outputs[srcpad] || dst->inputs[dstpad])
84  return AVERROR(EINVAL);
85 
86  if (src->output_pads[srcpad].type != dst->input_pads[dstpad].type) {
87  av_log(src, AV_LOG_ERROR,
88  "Media type mismatch between the '%s' filter output pad %d and the '%s' filter input pad %d\n",
89  src->name, srcpad, dst->name, dstpad);
90  return AVERROR(EINVAL);
91  }
92 
93  link = av_mallocz(sizeof(*link));
94  if (!link)
95  return AVERROR(ENOMEM);
96 
97  src->outputs[srcpad] = dst->inputs[dstpad] = link;
98 
99  link->src = src;
100  link->dst = dst;
101  link->srcpad = &src->output_pads[srcpad];
102  link->dstpad = &dst->input_pads[dstpad];
103  link->type = src->output_pads[srcpad].type;
104  assert(AV_PIX_FMT_NONE == -1 && AV_SAMPLE_FMT_NONE == -1);
105  link->format = -1;
106 
107  return 0;
108 }
109 
111  unsigned filt_srcpad_idx, unsigned filt_dstpad_idx)
112 {
113  int ret;
114  unsigned dstpad_idx = link->dstpad - link->dst->input_pads;
115 
116  av_log(link->dst, AV_LOG_VERBOSE, "auto-inserting filter '%s' "
117  "between the filter '%s' and the filter '%s'\n",
118  filt->name, link->src->name, link->dst->name);
119 
120  link->dst->inputs[dstpad_idx] = NULL;
121  if ((ret = avfilter_link(filt, filt_dstpad_idx, link->dst, dstpad_idx)) < 0) {
122  /* failed to link output filter to new filter */
123  link->dst->inputs[dstpad_idx] = link;
124  return ret;
125  }
126 
127  /* re-hookup the link to the new destination filter we inserted */
128  link->dst = filt;
129  link->dstpad = &filt->input_pads[filt_srcpad_idx];
130  filt->inputs[filt_srcpad_idx] = link;
131 
132  /* if any information on supported media formats already exists on the
133  * link, we need to preserve that */
134  if (link->out_formats)
136  &filt->outputs[filt_dstpad_idx]->out_formats);
137  if (link->out_samplerates)
139  &filt->outputs[filt_dstpad_idx]->out_samplerates);
140  if (link->out_channel_layouts)
142  &filt->outputs[filt_dstpad_idx]->out_channel_layouts);
143 
144  return 0;
145 }
146 
148 {
149  int (*config_link)(AVFilterLink *);
150  unsigned i;
151  int ret;
152 
153  for (i = 0; i < filter->nb_inputs; i ++) {
154  AVFilterLink *link = filter->inputs[i];
155 
156  if (!link) continue;
157  if (!link->src || !link->dst) {
158  av_log(filter, AV_LOG_ERROR,
159  "Not all input and output are properly linked (%d).\n", i);
160  return AVERROR(EINVAL);
161  }
162 
163  switch (link->init_state) {
164  case AVLINK_INIT:
165  continue;
166  case AVLINK_STARTINIT:
167  av_log(filter, AV_LOG_INFO, "circular filter chain detected\n");
168  return 0;
169  case AVLINK_UNINIT:
170  link->init_state = AVLINK_STARTINIT;
171 
172  if ((ret = avfilter_config_links(link->src)) < 0)
173  return ret;
174 
175  if (!(config_link = link->srcpad->config_props)) {
176  if (link->src->nb_inputs != 1) {
177  av_log(link->src, AV_LOG_ERROR, "Source filters and filters "
178  "with more than one input "
179  "must set config_props() "
180  "callbacks on all outputs\n");
181  return AVERROR(EINVAL);
182  }
183  } else if ((ret = config_link(link)) < 0) {
184  av_log(link->src, AV_LOG_ERROR,
185  "Failed to configure output pad on %s\n",
186  link->src->name);
187  return ret;
188  }
189 
190  if (link->time_base.num == 0 && link->time_base.den == 0)
191  link->time_base = link->src->nb_inputs ?
192  link->src->inputs[0]->time_base : AV_TIME_BASE_Q;
193 
194  if (link->type == AVMEDIA_TYPE_VIDEO) {
195  if (!link->sample_aspect_ratio.num && !link->sample_aspect_ratio.den)
196  link->sample_aspect_ratio = link->src->nb_inputs ?
197  link->src->inputs[0]->sample_aspect_ratio : (AVRational){1,1};
198 
199  if (link->src->nb_inputs) {
200  if (!link->frame_rate.num && !link->frame_rate.den)
201  link->frame_rate = link->src->inputs[0]->frame_rate;
202  if (!link->w)
203  link->w = link->src->inputs[0]->w;
204  if (!link->h)
205  link->h = link->src->inputs[0]->h;
206  } else if (!link->w || !link->h) {
207  av_log(link->src, AV_LOG_ERROR,
208  "Video source filters must set their output link's "
209  "width and height\n");
210  return AVERROR(EINVAL);
211  }
212  }
213 
214  if (link->src->nb_inputs && link->src->inputs[0]->hw_frames_ctx &&
215  !link->hw_frames_ctx) {
216  AVHWFramesContext *input_ctx = (AVHWFramesContext*)link->src->inputs[0]->hw_frames_ctx->data;
217 
218  if (input_ctx->format == link->format) {
219  link->hw_frames_ctx = av_buffer_ref(link->src->inputs[0]->hw_frames_ctx);
220  if (!link->hw_frames_ctx)
221  return AVERROR(ENOMEM);
222  }
223  }
224 
225  if ((config_link = link->dstpad->config_props))
226  if ((ret = config_link(link)) < 0) {
227  av_log(link->dst, AV_LOG_ERROR,
228  "Failed to configure input pad on %s\n",
229  link->dst->name);
230  return ret;
231  }
232 
233  link->init_state = AVLINK_INIT;
234  }
235  }
236 
237  return 0;
238 }
239 
240 void ff_dlog_link(void *ctx, AVFilterLink *link, int end)
241 {
242  if (link->type == AVMEDIA_TYPE_VIDEO) {
243  av_log(ctx, AV_LOG_TRACE,
244  "link[%p s:%dx%d fmt:%-16s %-16s->%-16s]%s",
245  link, link->w, link->h,
247  link->src ? link->src->filter->name : "",
248  link->dst ? link->dst->filter->name : "",
249  end ? "\n" : "");
250  } else {
251  char buf[128];
252  av_get_channel_layout_string(buf, sizeof(buf), -1, link->channel_layout);
253 
254  av_log(ctx, AV_LOG_TRACE,
255  "link[%p r:%d cl:%s fmt:%-16s %-16s->%-16s]%s",
256  link, link->sample_rate, buf,
258  link->src ? link->src->filter->name : "",
259  link->dst ? link->dst->filter->name : "",
260  end ? "\n" : "");
261  }
262 }
263 
265 {
267 
268  if (link->srcpad->request_frame)
269  return link->srcpad->request_frame(link);
270  else if (link->src->inputs[0])
271  return ff_request_frame(link->src->inputs[0]);
272  else
273  return AVERROR(EINVAL);
274 }
275 
277 {
278  int i, min = INT_MAX;
279 
280  if (link->srcpad->poll_frame)
281  return link->srcpad->poll_frame(link);
282 
283  for (i = 0; i < link->src->nb_inputs; i++) {
284  int val;
285  if (!link->src->inputs[i])
286  return AVERROR(EINVAL);
287  val = ff_poll_frame(link->src->inputs[i]);
288  min = FFMIN(min, val);
289  }
290 
291  return min;
292 }
293 
295 
296 #if !FF_API_NOCONST_GET_NAME
297 const
298 #endif
300 {
301  const AVFilter *f = NULL;
302 
303  if (!name)
304  return NULL;
305 
306  while ((f = avfilter_next(f)))
307  if (!strcmp(f->name, name))
308  return f;
309 
310  return NULL;
311 }
312 
314 {
315  AVFilter **f = &first_filter;
316  while (*f)
317  f = &(*f)->next;
318  *f = filter;
319  filter->next = NULL;
320  return 0;
321 }
322 
323 const AVFilter *avfilter_next(const AVFilter *prev)
324 {
325  return prev ? prev->next : first_filter;
326 }
327 
328 #if FF_API_OLD_FILTER_REGISTER
330 {
331  return filter ? &(*filter)->next : &first_filter;
332 }
333 
334 void avfilter_uninit(void)
335 {
336 }
337 #endif
338 
340 {
341  int count;
342 
343  if (!pads)
344  return 0;
345 
346  for (count = 0; pads->name; count++)
347  pads++;
348  return count;
349 }
350 
351 static const char *filter_name(void *p)
352 {
353  AVFilterContext *filter = p;
354  return filter->filter->name;
355 }
356 
357 static void *filter_child_next(void *obj, void *prev)
358 {
359  AVFilterContext *ctx = obj;
360  if (!prev && ctx->filter && ctx->filter->priv_class && ctx->priv)
361  return ctx->priv;
362  return NULL;
363 }
364 
365 static const AVClass *filter_child_class_next(const AVClass *prev)
366 {
367  const AVFilter *f = NULL;
368 
369  while (prev && (f = avfilter_next(f)))
370  if (f->priv_class == prev)
371  break;
372 
373  while ((f = avfilter_next(f)))
374  if (f->priv_class)
375  return f->priv_class;
376 
377  return NULL;
378 }
379 
380 #define OFFSET(x) offsetof(AVFilterContext, x)
381 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM
382 static const AVOption avfilter_options[] = {
383  { "thread_type", "Allowed thread types", OFFSET(thread_type), AV_OPT_TYPE_FLAGS,
384  { .i64 = AVFILTER_THREAD_SLICE }, 0, INT_MAX, FLAGS, "thread_type" },
385  { "slice", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AVFILTER_THREAD_SLICE }, .unit = "thread_type" },
386  { NULL },
387 };
388 
389 static const AVClass avfilter_class = {
390  .class_name = "AVFilter",
391  .item_name = filter_name,
392  .version = LIBAVUTIL_VERSION_INT,
393  .child_next = filter_child_next,
394  .child_class_next = filter_child_class_next,
396 };
397 
399  int *ret, int nb_jobs)
400 {
401  int i;
402 
403  for (i = 0; i < nb_jobs; i++) {
404  int r = func(ctx, arg, i, nb_jobs);
405  if (ret)
406  ret[i] = r;
407  }
408  return 0;
409 }
410 
411 AVFilterContext *ff_filter_alloc(const AVFilter *filter, const char *inst_name)
412 {
413  AVFilterContext *ret;
414 
415  if (!filter)
416  return NULL;
417 
418  ret = av_mallocz(sizeof(AVFilterContext));
419  if (!ret)
420  return NULL;
421 
422  ret->av_class = &avfilter_class;
423  ret->filter = filter;
424  ret->name = inst_name ? av_strdup(inst_name) : NULL;
425  if (filter->priv_size) {
426  ret->priv = av_mallocz(filter->priv_size);
427  if (!ret->priv)
428  goto err;
429  }
430 
431  av_opt_set_defaults(ret);
432  if (filter->priv_class) {
433  *(const AVClass**)ret->priv = filter->priv_class;
435  }
436 
437  ret->internal = av_mallocz(sizeof(*ret->internal));
438  if (!ret->internal)
439  goto err;
441 
442  ret->nb_inputs = avfilter_pad_count(filter->inputs);
443  if (ret->nb_inputs ) {
444  ret->input_pads = av_malloc(sizeof(AVFilterPad) * ret->nb_inputs);
445  if (!ret->input_pads)
446  goto err;
447  memcpy(ret->input_pads, filter->inputs, sizeof(AVFilterPad) * ret->nb_inputs);
448  ret->inputs = av_mallocz(sizeof(AVFilterLink*) * ret->nb_inputs);
449  if (!ret->inputs)
450  goto err;
451  }
452 
453  ret->nb_outputs = avfilter_pad_count(filter->outputs);
454  if (ret->nb_outputs) {
455  ret->output_pads = av_malloc(sizeof(AVFilterPad) * ret->nb_outputs);
456  if (!ret->output_pads)
457  goto err;
458  memcpy(ret->output_pads, filter->outputs, sizeof(AVFilterPad) * ret->nb_outputs);
459  ret->outputs = av_mallocz(sizeof(AVFilterLink*) * ret->nb_outputs);
460  if (!ret->outputs)
461  goto err;
462  }
463 
464  return ret;
465 
466 err:
467  av_freep(&ret->inputs);
468  av_freep(&ret->input_pads);
469  ret->nb_inputs = 0;
470  av_freep(&ret->outputs);
471  av_freep(&ret->output_pads);
472  ret->nb_outputs = 0;
473  av_freep(&ret->priv);
474  av_freep(&ret->internal);
475  av_free(ret);
476  return NULL;
477 }
478 
479 #if FF_API_AVFILTER_OPEN
480 int avfilter_open(AVFilterContext **filter_ctx, AVFilter *filter, const char *inst_name)
481 {
482  *filter_ctx = ff_filter_alloc(filter, inst_name);
483  return *filter_ctx ? 0 : AVERROR(ENOMEM);
484 }
485 #endif
486 
487 static void free_link(AVFilterLink *link)
488 {
489  if (!link)
490  return;
491 
492  if (link->src)
493  link->src->outputs[link->srcpad - link->src->output_pads] = NULL;
494  if (link->dst)
495  link->dst->inputs[link->dstpad - link->dst->input_pads] = NULL;
496 
498 
505  av_freep(&link);
506 }
507 
509 {
510  int i;
511 
512  if (filter->graph)
513  ff_filter_graph_remove_filter(filter->graph, filter);
514 
515  if (filter->filter->uninit)
516  filter->filter->uninit(filter);
517 
518  for (i = 0; i < filter->nb_inputs; i++) {
519  free_link(filter->inputs[i]);
520  }
521  for (i = 0; i < filter->nb_outputs; i++) {
522  free_link(filter->outputs[i]);
523  }
524 
525  if (filter->filter->priv_class)
526  av_opt_free(filter->priv);
527 
528  av_buffer_unref(&filter->hw_device_ctx);
529 
530  av_freep(&filter->name);
531  av_freep(&filter->input_pads);
532  av_freep(&filter->output_pads);
533  av_freep(&filter->inputs);
534  av_freep(&filter->outputs);
535  av_freep(&filter->priv);
536  av_freep(&filter->internal);
537  av_free(filter);
538 }
539 
540 /* process a list of value1:value2:..., each value corresponding
541  * to subsequent AVOption, in the order they are declared */
543  const char *args)
544 {
545  const AVOption *o = NULL;
546  const char *p = args;
547  char *val;
548 
549  while (*p) {
550  o = av_opt_next(ctx->priv, o);
551  if (!o) {
552  av_log(ctx, AV_LOG_ERROR, "More options provided than "
553  "this filter supports.\n");
554  return AVERROR(EINVAL);
555  }
556  if (o->type == AV_OPT_TYPE_CONST)
557  continue;
558 
559  val = av_get_token(&p, ":");
560  if (!val)
561  return AVERROR(ENOMEM);
562 
563  av_dict_set(options, o->name, val, 0);
564 
565  av_freep(&val);
566  if (*p)
567  p++;
568  }
569 
570  return 0;
571 }
572 
573 #if FF_API_AVFILTER_INIT_FILTER
574 int avfilter_init_filter(AVFilterContext *filter, const char *args, void *opaque)
575 {
576  return avfilter_init_str(filter, args);
577 }
578 #endif
579 
581 {
582  int ret = 0;
583 
584  ret = av_opt_set_dict(ctx, options);
585  if (ret < 0) {
586  av_log(ctx, AV_LOG_ERROR, "Error applying generic filter options.\n");
587  return ret;
588  }
589 
592  ctx->graph->internal->thread_execute) {
595  } else {
596  ctx->thread_type = 0;
597  }
598 
599  if (ctx->filter->priv_class) {
600  ret = av_opt_set_dict(ctx->priv, options);
601  if (ret < 0) {
602  av_log(ctx, AV_LOG_ERROR, "Error applying options to the filter.\n");
603  return ret;
604  }
605  }
606 
607  if (ctx->filter->init)
608  ret = ctx->filter->init(ctx);
609  else if (ctx->filter->init_dict)
610  ret = ctx->filter->init_dict(ctx, options);
611 
612  return ret;
613 }
614 
615 int avfilter_init_str(AVFilterContext *filter, const char *args)
616 {
619  int ret = 0;
620 
621  if (args && *args) {
622  if (!filter->filter->priv_class) {
623  av_log(filter, AV_LOG_ERROR, "This filter does not take any "
624  "options, but options were provided: %s.\n", args);
625  return AVERROR(EINVAL);
626  }
627 
628 #if FF_API_OLD_FILTER_OPTS
629  if (!strcmp(filter->filter->name, "scale") &&
630  strchr(args, ':') && strchr(args, ':') < strchr(args, '=')) {
631  /* old w:h:flags=<flags> syntax */
632  char *copy = av_strdup(args);
633  char *p;
634 
635  av_log(filter, AV_LOG_WARNING, "The <w>:<h>:flags=<flags> option "
636  "syntax is deprecated. Use either <w>:<h>:<flags> or "
637  "w=<w>:h=<h>:flags=<flags>.\n");
638 
639  if (!copy) {
640  ret = AVERROR(ENOMEM);
641  goto fail;
642  }
643 
644  p = strrchr(copy, ':');
645  if (p) {
646  *p++ = 0;
647  ret = av_dict_parse_string(&options, p, "=", ":", 0);
648  }
649  if (ret >= 0)
650  ret = process_unnamed_options(filter, &options, copy);
651  av_freep(&copy);
652 
653  if (ret < 0)
654  goto fail;
655  } else
656 #endif
657 
658  if (strchr(args, '=')) {
659  /* assume a list of key1=value1:key2=value2:... */
660  ret = av_dict_parse_string(&options, args, "=", ":", 0);
661  if (ret < 0)
662  goto fail;
663 #if FF_API_OLD_FILTER_OPTS
664  } else if (!strcmp(filter->filter->name, "format") ||
665  !strcmp(filter->filter->name, "noformat") ||
666  !strcmp(filter->filter->name, "frei0r") ||
667  !strcmp(filter->filter->name, "frei0r_src") ||
668  !strcmp(filter->filter->name, "ocv")) {
669  /* a hack for compatibility with the old syntax
670  * replace colons with |s */
671  char *copy = av_strdup(args);
672  char *p = copy;
673  int nb_leading = 0; // number of leading colons to skip
674 
675  if (!copy) {
676  ret = AVERROR(ENOMEM);
677  goto fail;
678  }
679 
680  if (!strcmp(filter->filter->name, "frei0r") ||
681  !strcmp(filter->filter->name, "ocv"))
682  nb_leading = 1;
683  else if (!strcmp(filter->filter->name, "frei0r_src"))
684  nb_leading = 3;
685 
686  while (nb_leading--) {
687  p = strchr(p, ':');
688  if (!p) {
689  p = copy + strlen(copy);
690  break;
691  }
692  p++;
693  }
694 
695  if (strchr(p, ':')) {
696  av_log(filter, AV_LOG_WARNING, "This syntax is deprecated. Use "
697  "'|' to separate the list items.\n");
698  }
699 
700  while ((p = strchr(p, ':')))
701  *p++ = '|';
702 
703  ret = process_unnamed_options(filter, &options, copy);
704  av_freep(&copy);
705 
706  if (ret < 0)
707  goto fail;
708 #endif
709  } else {
710  ret = process_unnamed_options(filter, &options, args);
711  if (ret < 0)
712  goto fail;
713  }
714  }
715 
716  ret = avfilter_init_dict(filter, &options);
717  if (ret < 0)
718  goto fail;
719 
720  if ((e = av_dict_get(options, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
721  av_log(filter, AV_LOG_ERROR, "No such option: %s.\n", e->key);
723  goto fail;
724  }
725 
726 fail:
727  av_dict_free(&options);
728 
729  return ret;
730 }
731 
732 const char *avfilter_pad_get_name(const AVFilterPad *pads, int pad_idx)
733 {
734  return pads[pad_idx].name;
735 }
736 
737 enum AVMediaType avfilter_pad_get_type(const AVFilterPad *pads, int pad_idx)
738 {
739  return pads[pad_idx].type;
740 }
741 
742 static int default_filter_frame(AVFilterLink *link, AVFrame *frame)
743 {
744  return ff_filter_frame(link->dst->outputs[0], frame);
745 }
746 
748 {
749  int (*filter_frame)(AVFilterLink *, AVFrame *);
750  AVFilterPad *dst = link->dstpad;
751  AVFrame *out = NULL;
752  int ret;
753 
755  ff_dlog_link(NULL, link, 1);
756 
757  if (!(filter_frame = dst->filter_frame))
759 
760  /* copy the frame if needed */
761  if (dst->needs_writable && !av_frame_is_writable(frame)) {
762  av_log(link->dst, AV_LOG_DEBUG, "Copying data in avfilter.\n");
763 
764  switch (link->type) {
765  case AVMEDIA_TYPE_VIDEO:
766  out = ff_get_video_buffer(link, link->w, link->h);
767  break;
768  case AVMEDIA_TYPE_AUDIO:
769  out = ff_get_audio_buffer(link, frame->nb_samples);
770  break;
771  default:
772  ret = AVERROR(EINVAL);
773  goto fail;
774  }
775  if (!out) {
776  ret = AVERROR(ENOMEM);
777  goto fail;
778  }
779 
780  ret = av_frame_copy_props(out, frame);
781  if (ret < 0)
782  goto fail;
783 
784  switch (link->type) {
785  case AVMEDIA_TYPE_VIDEO:
786  av_image_copy(out->data, out->linesize, frame->data, frame->linesize,
787  frame->format, frame->width, frame->height);
788  break;
789  case AVMEDIA_TYPE_AUDIO:
791  0, 0, frame->nb_samples,
793  frame->format);
794  break;
795  default:
796  ret = AVERROR(EINVAL);
797  goto fail;
798  }
799 
800  av_frame_free(&frame);
801  } else
802  out = frame;
803 
804  return filter_frame(link, out);
805 
806 fail:
807  av_frame_free(&out);
808  av_frame_free(&frame);
809  return ret;
810 }
811 
813 {
814  return &avfilter_class;
815 }
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:62
void av_buffer_unref(AVBufferRef **buf)
Free a given reference and automatically free the buffer if there are no more references to it...
Definition: buffer.c:106
AVFilterContext * ff_filter_alloc(const AVFilter *filter, const char *inst_name)
Allocate a new filter context and return it.
Definition: avfilter.c:411
This structure describes decoded (raw) audio or video data.
Definition: frame.h:140
int thread_type
Type of multithreading allowed for filters in this graph.
Definition: avfilter.h:623
static int default_filter_frame(AVFilterLink *link, AVFrame *frame)
Definition: avfilter.c:742
AVOption.
Definition: opt.h:234
#define LIBAV_CONFIGURATION
Definition: config.h:4
void avfilter_free(AVFilterContext *filter)
Free a filter context.
Definition: avfilter.c:508
static const char * filter_name(void *p)
Definition: avfilter.c:351
misc image utilities
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:130
Main libavfilter public API header.
void ff_channel_layouts_changeref(AVFilterChannelLayouts **oldref, AVFilterChannelLayouts **newref)
Definition: formats.c:335
void av_opt_set_defaults(void *s)
Set the values of all AVOption fields to their default values.
Definition: opt.c:599
enum AVMediaType avfilter_pad_get_type(const AVFilterPad *pads, int pad_idx)
Get the type of an AVFilterPad.
Definition: avfilter.c:737
int num
numerator
Definition: rational.h:44
av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (%s)\, len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt), use_generic ? ac->func_descr_generic :ac->func_descr)
AVBufferRef * hw_device_ctx
For filters which will create hardware frames, sets the device the filter should create them in...
Definition: avfilter.h:311
enum AVPixelFormat format
The pixel format identifying the underlying HW surface type.
Definition: hwcontext.h:202
enum AVMediaType type
AVFilterPad type.
Definition: internal.h:46
#define LIBAVFILTER_VERSION_INT
Definition: version.h:36
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition: video.c:57
int thread_type
Type of multithreading being allowed/used.
Definition: avfilter.h:297
void(* uninit)(AVFilterContext *ctx)
Filter uninitialization function.
Definition: avfilter.h:219
#define AVFILTER_THREAD_SLICE
Process multiple parts of the frame concurrently.
Definition: avfilter.h:257
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:202
struct AVFilterGraph * graph
filtergraph this filter belongs to
Definition: avfilter.h:279
int(* init_dict)(AVFilterContext *ctx, AVDictionary **options)
Should be set instead of init by the filters that want to pass a dictionary of AVOptions to nested co...
Definition: avfilter.h:207
const char * name
Pad name.
Definition: internal.h:41
int priv_size
size of private data to allocate for the filter
Definition: avfilter.h:245
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:39
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:270
char * name
name of this filter instance
Definition: avfilter.h:267
const char * name
Definition: opt.h:235
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:747
int avfilter_link(AVFilterContext *src, unsigned srcpad, AVFilterContext *dst, unsigned dstpad)
Link two filters together.
Definition: avfilter.c:77
AVFilterPad * output_pads
array of output pads
Definition: avfilter.h:273
void ff_dlog_link(void *ctx, AVFilterLink *link, int end)
Definition: avfilter.c:240
const char * avfilter_license(void)
Return the libavfilter license.
Definition: avfilter.c:50
uint8_t
AVOptions.
#define AV_LOG_TRACE
Extremely verbose debugging, useful for libav* development.
Definition: log.h:150
int(* poll_frame)(AVFilterLink *link)
Frame poll callback.
Definition: internal.h:85
const struct AVOption * option
a pointer to the first option specified in the class if any or NULL
Definition: log.h:52
int flags
A combination of AVFILTER_FLAG_*.
Definition: avfilter.h:163
AVDictionaryEntry * av_dict_get(const AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags)
Get a dictionary entry with matching key.
Definition: dict.c:38
int avfilter_config_links(AVFilterContext *filter)
Negotiate the media format, dimensions, etc of all inputs to a filter.
Definition: avfilter.c:147
const AVFilter * avfilter_next(const AVFilter *prev)
Iterate over all registered filters.
Definition: avfilter.c:323
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:140
static void copy(LZOContext *c, int cnt)
Copies bytes from input to output buffer with checking.
Definition: lzo.c:79
static void free_link(AVFilterLink *link)
Definition: avfilter.c:487
#define r
Definition: input.c:51
const OptionDef options[]
Definition: avconv_opt.c:2447
A filter pad used for either input or output.
Definition: internal.h:35
static const AVClass avfilter_class
Definition: avfilter.c:389
#define src
Definition: vp8dsp.c:254
AVFilterPad * input_pads
array of input pads
Definition: avfilter.h:269
int width
width and height of the video frame
Definition: frame.h:179
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:124
void ff_formats_changeref(AVFilterFormats **oldref, AVFilterFormats **newref)
Before After |formats |<------—.
Definition: formats.c:341
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:190
int avfilter_init_filter(AVFilterContext *filter, const char *args, void *opaque)
Definition: avfilter.c:574
AVFrame * ff_get_audio_buffer(AVFilterLink *link, int nb_samples)
Request an audio samples buffer with a specific set of permissions.
Definition: audio.c:57
#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:80
unsigned nb_outputs
number of output pads
Definition: avfilter.h:275
unsigned avfilter_version(void)
Return the LIBAVFILTER_VERSION_INT constant.
Definition: avfilter.c:40
void * priv
private data for use by the filter
Definition: avfilter.h:277
#define AVFILTER_FLAG_SLICE_THREADS
The filter supports multithreading by splitting frames into multiple parts and processing them concur...
Definition: avfilter.h:114
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:145
void av_dict_free(AVDictionary **pm)
Free all the memory allocated for an AVDictionary struct and all keys and values. ...
Definition: dict.c:175
int av_get_channel_layout_nb_channels(uint64_t channel_layout)
Return the number of channels in the channel layout.
const AVOption * av_opt_next(const void *obj, const AVOption *last)
Iterate over all AVOptions belonging to obj.
Definition: opt.c:37
const char * av_get_sample_fmt_name(enum AVSampleFormat sample_fmt)
Return the name of sample_fmt, or NULL if sample_fmt is not recognized.
Definition: samplefmt.c:47
#define fail()
Definition: checkasm.h:80
void av_image_copy(uint8_t *dst_data[4], int dst_linesizes[4], const uint8_t *src_data[4], const int src_linesizes[4], enum AVPixelFormat pix_fmt, int width, int height)
Copy image in src_data to dst_data.
Definition: imgutils.c:268
char * av_get_token(const char **buf, const char *term)
Unescape the given string until a non escaped terminating char, and return the token corresponding to...
Definition: avstring.c:121
void avfilter_uninit(void)
Definition: avfilter.c:334
uint64_t channel_layout
Channel layout of the audio data.
Definition: frame.h:294
AVFilter * avfilter_get_by_name(const char *name)
Get a filter definition matching the given name.
Definition: avfilter.c:299
common internal API header
static void filter(MpegAudioContext *s, int ch, const short *samples, int incr)
Definition: mpegaudioenc.c:307
static void * filter_child_next(void *obj, void *prev)
Definition: avfilter.c:357
const char * name
Definition: qsvenc.c:44
int(* config_props)(AVFilterLink *link)
Link configuration callback.
Definition: internal.h:110
audio channel layout utility functions
unsigned nb_inputs
number of input pads
Definition: avfilter.h:271
#define FFMIN(a, b)
Definition: common.h:66
static AVFilter * first_filter
Definition: avfilter.c:294
static const AVOption avfilter_options[]
Definition: avfilter.c:382
int() avfilter_action_func(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
A function pointer passed to the AVFilterGraph::execute callback to be executed multiple times...
Definition: avfilter.h:586
AVFormatContext * ctx
Definition: movenc.c:48
int(* init)(AVFilterContext *ctx)
Filter initialization function.
Definition: avfilter.h:194
int avfilter_open(AVFilterContext **filter_ctx, AVFilter *filter, const char *inst_name)
Definition: avfilter.c:480
int needs_writable
The filter expects writable frames from its input link, duplicating data buffers if needed...
Definition: internal.h:126
AVFilter ** av_filter_next(AVFilter **filter)
Definition: avfilter.c:329
static int process_unnamed_options(AVFilterContext *ctx, AVDictionary **options, const char *args)
Definition: avfilter.c:542
LIBAVUTIL_VERSION_INT
Definition: eval.c:55
int avfilter_init_str(AVFilterContext *filter, const char *args)
Initialize a filter with the supplied parameters.
Definition: avfilter.c:615
if(ac->has_optimized_func)
const AVFilterPad * inputs
List of inputs, terminated by a zeroed element.
Definition: avfilter.h:140
const AVClass * avfilter_get_class(void)
Definition: avfilter.c:812
int avfilter_insert_filter(AVFilterLink *link, AVFilterContext *filt, unsigned filt_srcpad_idx, unsigned filt_dstpad_idx)
Insert a filter in the middle of an existing link.
Definition: avfilter.c:110
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...
Definition: frame.h:191
int av_dict_parse_string(AVDictionary **pm, const char *str, const char *key_val_sep, const char *pairs_sep, int flags)
Parse the key/value pairs list and add to a dictionary.
Definition: dict.c:152
#define LIBAV_LICENSE
Definition: config.h:5
NULL
Definition: eval.c:55
const AVClass * priv_class
A class for the private data, used to declare filter private AVOptions.
Definition: avfilter.h:158
#define AV_LOG_INFO
Standard information.
Definition: log.h:135
#define AV_TIME_BASE_Q
Internal time base represented as fractional value.
Definition: avutil.h:247
AVFilterGraphInternal * internal
Opaque object for libavfilter internal use.
Definition: avfilter.h:635
int av_frame_is_writable(AVFrame *frame)
Check if the frame data is writable.
Definition: frame.c:328
int av_opt_set_dict(void *obj, AVDictionary **options)
Definition: opt.c:735
char * av_strdup(const char *s)
Duplicate the string s.
Definition: mem.c:219
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:158
void(* func)(void)
Definition: checkasm.c:65
uint8_t * data
The data buffer.
Definition: buffer.h:89
void ff_channel_layouts_unref(AVFilterChannelLayouts **ref)
Remove a reference to a channel layouts list.
Definition: formats.c:317
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:187
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
Definition: dict.c:68
Describe the class of an AVClass context structure.
Definition: log.h:34
Filter definition.
Definition: avfilter.h:120
rational number numerator/denominator
Definition: rational.h:43
struct AVFilter * next
Used by the filter registration system.
Definition: avfilter.h:251
This struct describes a set or pool of "hardware" frames (i.e.
Definition: hwcontext.h:117
AVMediaType
Definition: avutil.h:192
refcounted data buffer API
void ff_formats_unref(AVFilterFormats **ref)
If *ref is non-NULL, remove *ref as a reference to the format list it currently points to...
Definition: formats.c:312
const char * name
Filter name.
Definition: avfilter.h:124
const char * avfilter_configuration(void)
Return the libavfilter build-time configuration.
Definition: avfilter.c:45
const char * avfilter_pad_get_name(const AVFilterPad *pads, int pad_idx)
Get the name of an AVFilterPad.
Definition: avfilter.c:732
#define FLAGS
Definition: avfilter.c:381
#define LICENSE_PREFIX
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:274
static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
Definition: af_amix.c:459
#define OFFSET(x)
Definition: avfilter.c:380
AVFilterInternal * internal
An opaque struct for libavfilter internal use.
Definition: avfilter.h:302
static int default_execute(AVFilterContext *ctx, avfilter_action_func *func, void *arg, int *ret, int nb_jobs)
Definition: avfilter.c:398
int(* filter_frame)(AVFilterLink *link, AVFrame *frame)
Filtering callback.
Definition: internal.h:74
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:146
int avfilter_init_dict(AVFilterContext *ctx, AVDictionary **options)
Initialize a filter with the supplied dictionary of options.
Definition: avfilter.c:580
void av_opt_free(void *obj)
Free all allocated objects in obj.
Definition: opt.c:715
common internal and external API header
rational numbers
void * av_realloc(void *ptr, size_t size)
Allocate or reallocate a block of memory.
Definition: mem.c:117
static int request_frame(AVFilterLink *outlink)
Definition: af_amix.c:392
AVBufferRef * av_buffer_ref(AVBufferRef *buf)
Create a new reference to an AVBuffer.
Definition: buffer.c:92
char * key
Definition: dict.h:73
int den
denominator
Definition: rational.h:45
avfilter_execute_func * execute
Definition: internal.h:135
void ff_filter_graph_remove_filter(AVFilterGraph *graph, AVFilterContext *filter)
Remove a filter from a graph;.
Definition: avfiltergraph.c:89
avfilter_execute_func * thread_execute
Definition: internal.h:131
#define AVERROR_OPTION_NOT_FOUND
Option not found.
Definition: error.h:56
int avfilter_register(AVFilter *filter)
Register a filter.
Definition: avfilter.c:313
enum AVOptionType type
Definition: opt.h:248
const AVClass * av_class
needed for av_log()
Definition: avfilter.h:263
int(* request_frame)(AVFilterLink *link)
Frame request callback.
Definition: internal.h:94
An instance of a filter.
Definition: avfilter.h:262
int avfilter_pad_count(const AVFilterPad *pads)
Get the number of elements in a NULL-terminated array of AVFilterPads (e.g.
Definition: avfilter.c:339
#define FF_DPRINTF_START(ctx, func)
Definition: internal.h:141
int height
Definition: frame.h:179
FILE * out
Definition: movenc.c:54
const AVFilterPad * outputs
List of outputs, terminated by a zeroed element.
Definition: avfilter.h:148
#define AV_DICT_IGNORE_SUFFIX
Definition: dict.h:60
void ff_insert_pad(unsigned idx, unsigned *count, size_t padidx_off, AVFilterPad **pads, AVFilterLink ***links, AVFilterPad *newpad)
Insert a new pad.
Definition: avfilter.c:56
int ff_request_frame(AVFilterLink *link)
Request an input frame from the filter at the other end of the link.
Definition: avfilter.c:264
const char * av_get_pix_fmt_name(enum AVPixelFormat pix_fmt)
Return the short name for a pixel format, NULL in case pix_fmt is unknown.
Definition: pixdesc.c:1704
static const AVClass * filter_child_class_next(const AVClass *prev)
Definition: avfilter.c:365
internal API functions
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:174
int ff_poll_frame(AVFilterLink *link)
Poll a frame from the filter chain.
Definition: avfilter.c:276
float min
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:184
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:211
const AVFilter * filter
the AVFilter of which this is an instance
Definition: avfilter.h:265
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition: frame.c:386
void av_get_channel_layout_string(char *buf, int buf_size, int nb_channels, uint64_t channel_layout)
Return a description of a channel layout.