Libav
graphparser.c
Go to the documentation of this file.
1 /*
2  * filter graph parser
3  * Copyright (c) 2008 Vitor Sessak
4  * Copyright (c) 2007 Bobby Bingham
5  *
6  * This file is part of Libav.
7  *
8  * Libav is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * Libav is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with Libav; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 #include <string.h>
24 #include <stdio.h>
25 
26 #include "libavutil/avstring.h"
27 #include "libavutil/mem.h"
28 #include "avfilter.h"
29 
30 #define WHITESPACES " \n\t"
31 
37 static int link_filter(AVFilterContext *src, int srcpad,
38  AVFilterContext *dst, int dstpad,
39  void *log_ctx)
40 {
41  int ret;
42  if ((ret = avfilter_link(src, srcpad, dst, dstpad))) {
43  av_log(log_ctx, AV_LOG_ERROR,
44  "Cannot create the link %s:%d -> %s:%d\n",
45  src->filter->name, srcpad, dst->filter->name, dstpad);
46  return ret;
47  }
48 
49  return 0;
50 }
51 
58 static char *parse_link_name(const char **buf, void *log_ctx)
59 {
60  const char *start = *buf;
61  char *name;
62  (*buf)++;
63 
64  name = av_get_token(buf, "]");
65  if (!name)
66  goto fail;
67 
68  if (!name[0]) {
69  av_log(log_ctx, AV_LOG_ERROR,
70  "Bad (empty?) label found in the following: \"%s\".\n", start);
71  goto fail;
72  }
73 
74  if (*(*buf)++ != ']') {
75  av_log(log_ctx, AV_LOG_ERROR,
76  "Mismatched '[' found in the following: \"%s\".\n", start);
77  fail:
78  av_freep(&name);
79  }
80 
81  return name;
82 }
83 
84 #define TMP_ARGS_SIZE 256
85 
86 static void append_sws_flags(const char **args, const char *sws_opts, char *tmp)
87 {
88  int nb_opts = 0;
89  const char *separator = ":";
90  const char *opt = *args;
91 
92  if (strstr(*args, "flags"))
93  return;
94 
95  if (strstr(*args, "="))
96  separator = ":flags=";
97 
98  while ((opt = strstr(opt, ":")) && *opt) {
99  av_log(NULL, AV_LOG_INFO, "opts '%s' \n", opt);
100  if (nb_opts > 2) {
101  return;
102  }
103  nb_opts++;
104  opt++;
105  }
106 
107  opt = strstr(sws_opts, "flags=");
108  if (opt && strlen(opt) > 6)
109  opt += 6;
110  else
111  opt = sws_opts;
112 
113  snprintf(tmp, TMP_ARGS_SIZE, "%s%s%s",
114  *args, separator, opt);
115 
116  *args = tmp;
117 }
118 
131 static int create_filter(AVFilterContext **filt_ctx, AVFilterGraph *ctx, int index,
132  const char *filt_name, const char *args, void *log_ctx)
133 {
134  AVFilter *filt;
135  char inst_name[30];
136  char tmp_args[TMP_ARGS_SIZE];
137  int ret;
138 
139  snprintf(inst_name, sizeof(inst_name), "Parsed filter %d %s", index, filt_name);
140 
141  filt = avfilter_get_by_name(filt_name);
142 
143  if (!filt) {
144  av_log(log_ctx, AV_LOG_ERROR,
145  "No such filter: '%s'\n", filt_name);
146  return AVERROR(EINVAL);
147  }
148 
149  *filt_ctx = avfilter_graph_alloc_filter(ctx, filt, inst_name);
150  if (!*filt_ctx) {
151  av_log(log_ctx, AV_LOG_ERROR,
152  "Error creating filter '%s'\n", filt_name);
153  return AVERROR(ENOMEM);
154  }
155 
156  if (!strcmp(filt_name, "scale") && args &&
157  ctx->scale_sws_opts) {
158  append_sws_flags(&args, ctx->scale_sws_opts, tmp_args);
159  }
160 
161  ret = avfilter_init_str(*filt_ctx, args);
162  if (ret < 0) {
163  av_log(log_ctx, AV_LOG_ERROR,
164  "Error initializing filter '%s'", filt_name);
165  if (args)
166  av_log(log_ctx, AV_LOG_ERROR, " with args '%s'", args);
167  av_log(log_ctx, AV_LOG_ERROR, "\n");
168  avfilter_free(*filt_ctx);
169  return ret;
170  }
171 
172  return 0;
173 }
174 
191 static int parse_filter(AVFilterContext **filt_ctx, const char **buf, AVFilterGraph *graph,
192  int index, void *log_ctx)
193 {
194  char *opts = NULL;
195  char *name = av_get_token(buf, "=,;[\n");
196  int ret;
197 
198  if (**buf == '=') {
199  (*buf)++;
200  opts = av_get_token(buf, "[],;\n");
201  }
202 
203  ret = create_filter(filt_ctx, graph, index, name, opts, log_ctx);
204  av_free(name);
205  av_free(opts);
206  return ret;
207 }
208 
210 {
211  return av_mallocz(sizeof(AVFilterInOut));
212 }
213 
215 {
216  while (*inout) {
217  AVFilterInOut *next = (*inout)->next;
218  av_freep(&(*inout)->name);
219  av_freep(inout);
220  *inout = next;
221  }
222 }
223 
224 static AVFilterInOut *extract_inout(const char *label, AVFilterInOut **links)
225 {
226  AVFilterInOut *ret;
227 
228  while (*links && (!(*links)->name || strcmp((*links)->name, label)))
229  links = &((*links)->next);
230 
231  ret = *links;
232 
233  if (ret) {
234  *links = ret->next;
235  ret->next = NULL;
236  }
237 
238  return ret;
239 }
240 
241 static void insert_inout(AVFilterInOut **inouts, AVFilterInOut *element)
242 {
243  element->next = *inouts;
244  *inouts = element;
245 }
246 
247 static void append_inout(AVFilterInOut **inouts, AVFilterInOut **element)
248 {
249  while (*inouts && (*inouts)->next)
250  inouts = &((*inouts)->next);
251 
252  if (!*inouts)
253  *inouts = *element;
254  else
255  (*inouts)->next = *element;
256  *element = NULL;
257 }
258 
259 static int link_filter_inouts(AVFilterContext *filt_ctx,
260  AVFilterInOut **curr_inputs,
261  AVFilterInOut **open_inputs, void *log_ctx)
262 {
263  int pad, ret;
264 
265  for (pad = 0; pad < filt_ctx->nb_inputs; pad++) {
266  AVFilterInOut *p = *curr_inputs;
267 
268  if (p) {
269  *curr_inputs = (*curr_inputs)->next;
270  p->next = NULL;
271  } else if (!(p = av_mallocz(sizeof(*p))))
272  return AVERROR(ENOMEM);
273 
274  if (p->filter_ctx) {
275  ret = link_filter(p->filter_ctx, p->pad_idx, filt_ctx, pad, log_ctx);
276  av_free(p->name);
277  av_free(p);
278  if (ret < 0)
279  return ret;
280  } else {
281  p->filter_ctx = filt_ctx;
282  p->pad_idx = pad;
283  append_inout(open_inputs, &p);
284  }
285  }
286 
287  if (*curr_inputs) {
288  av_log(log_ctx, AV_LOG_ERROR,
289  "Too many inputs specified for the \"%s\" filter.\n",
290  filt_ctx->filter->name);
291  return AVERROR(EINVAL);
292  }
293 
294  pad = filt_ctx->nb_outputs;
295  while (pad--) {
296  AVFilterInOut *currlinkn = av_mallocz(sizeof(AVFilterInOut));
297  if (!currlinkn)
298  return AVERROR(ENOMEM);
299  currlinkn->filter_ctx = filt_ctx;
300  currlinkn->pad_idx = pad;
301  insert_inout(curr_inputs, currlinkn);
302  }
303 
304  return 0;
305 }
306 
307 static int parse_inputs(const char **buf, AVFilterInOut **curr_inputs,
308  AVFilterInOut **open_outputs, void *log_ctx)
309 {
310  AVFilterInOut *parsed_inputs = NULL;
311  int pad = 0;
312 
313  while (**buf == '[') {
314  char *name = parse_link_name(buf, log_ctx);
315  AVFilterInOut *match;
316 
317  if (!name)
318  return AVERROR(EINVAL);
319 
320  /* First check if the label is not in the open_outputs list */
321  match = extract_inout(name, open_outputs);
322 
323  if (match) {
324  av_free(name);
325  } else {
326  /* Not in the list, so add it as an input */
327  if (!(match = av_mallocz(sizeof(AVFilterInOut)))) {
328  av_free(name);
329  return AVERROR(ENOMEM);
330  }
331  match->name = name;
332  match->pad_idx = pad;
333  }
334 
335  append_inout(&parsed_inputs, &match);
336 
337  *buf += strspn(*buf, WHITESPACES);
338  pad++;
339  }
340 
341  append_inout(&parsed_inputs, curr_inputs);
342  *curr_inputs = parsed_inputs;
343 
344  return pad;
345 }
346 
347 static int parse_outputs(const char **buf, AVFilterInOut **curr_inputs,
348  AVFilterInOut **open_inputs,
349  AVFilterInOut **open_outputs, void *log_ctx)
350 {
351  int ret, pad = 0;
352 
353  while (**buf == '[') {
354  char *name = parse_link_name(buf, log_ctx);
355  AVFilterInOut *match;
356 
357  AVFilterInOut *input = *curr_inputs;
358 
359  if (!name)
360  return AVERROR(EINVAL);
361 
362  if (!input) {
363  av_log(log_ctx, AV_LOG_ERROR,
364  "No output pad can be associated to link label '%s'.\n", name);
365  av_free(name);
366  return AVERROR(EINVAL);
367  }
368  *curr_inputs = (*curr_inputs)->next;
369 
370  /* First check if the label is not in the open_inputs list */
371  match = extract_inout(name, open_inputs);
372 
373  if (match) {
374  if ((ret = link_filter(input->filter_ctx, input->pad_idx,
375  match->filter_ctx, match->pad_idx, log_ctx)) < 0) {
376  av_free(name);
377  return ret;
378  }
379  av_free(match->name);
380  av_free(name);
381  av_free(match);
382  av_free(input);
383  } else {
384  /* Not in the list, so add the first input as a open_output */
385  input->name = name;
386  insert_inout(open_outputs, input);
387  }
388  *buf += strspn(*buf, WHITESPACES);
389  pad++;
390  }
391 
392  return pad;
393 }
394 
395 static int parse_sws_flags(const char **buf, AVFilterGraph *graph)
396 {
397  char *p = strchr(*buf, ';');
398 
399  if (strncmp(*buf, "sws_flags=", 10))
400  return 0;
401 
402  if (!p) {
403  av_log(graph, AV_LOG_ERROR, "sws_flags not terminated with ';'.\n");
404  return AVERROR(EINVAL);
405  }
406 
407  *buf += 4; // keep the 'flags=' part
408 
409  av_freep(&graph->scale_sws_opts);
410  if (!(graph->scale_sws_opts = av_mallocz(p - *buf + 1)))
411  return AVERROR(ENOMEM);
412  av_strlcpy(graph->scale_sws_opts, *buf, p - *buf + 1);
413 
414  *buf = p + 1;
415  return 0;
416 }
417 
418 int avfilter_graph_parse2(AVFilterGraph *graph, const char *filters,
421 {
422  int index = 0, ret;
423  char chr = 0;
424 
425  AVFilterInOut *curr_inputs = NULL, *open_inputs = NULL, *open_outputs = NULL;
426 
427  filters += strspn(filters, WHITESPACES);
428 
429  if ((ret = parse_sws_flags(&filters, graph)) < 0)
430  goto fail;
431 
432  do {
434  filters += strspn(filters, WHITESPACES);
435 
436  if ((ret = parse_inputs(&filters, &curr_inputs, &open_outputs, graph)) < 0)
437  goto fail;
438 
439  if ((ret = parse_filter(&filter, &filters, graph, index, graph)) < 0)
440  goto fail;
441 
442 
443  if ((ret = link_filter_inouts(filter, &curr_inputs, &open_inputs, graph)) < 0)
444  goto fail;
445 
446  if ((ret = parse_outputs(&filters, &curr_inputs, &open_inputs, &open_outputs,
447  graph)) < 0)
448  goto fail;
449 
450  filters += strspn(filters, WHITESPACES);
451  chr = *filters++;
452 
453  if (chr == ';' && curr_inputs)
454  append_inout(&open_outputs, &curr_inputs);
455  index++;
456  } while (chr == ',' || chr == ';');
457 
458  if (chr) {
459  av_log(graph, AV_LOG_ERROR,
460  "Unable to parse graph description substring: \"%s\"\n",
461  filters - 1);
462  ret = AVERROR(EINVAL);
463  goto fail;
464  }
465 
466  append_inout(&open_outputs, &curr_inputs);
467 
468  *inputs = open_inputs;
469  *outputs = open_outputs;
470  return 0;
471 
472  fail:
473  while (graph->nb_filters)
474  avfilter_free(graph->filters[0]);
475  av_freep(&graph->filters);
476  avfilter_inout_free(&open_inputs);
477  avfilter_inout_free(&open_outputs);
478  avfilter_inout_free(&curr_inputs);
479 
480  *inputs = NULL;
481  *outputs = NULL;
482 
483  return ret;
484 }
485 
486 int avfilter_graph_parse(AVFilterGraph *graph, const char *filters,
487  AVFilterInOut *open_inputs,
488  AVFilterInOut *open_outputs, void *log_ctx)
489 {
490  int ret;
491  AVFilterInOut *cur, *match, *inputs = NULL, *outputs = NULL;
492 
493  if ((ret = avfilter_graph_parse2(graph, filters, &inputs, &outputs)) < 0)
494  goto fail;
495 
496  /* First input can be omitted if it is "[in]" */
497  if (inputs && !inputs->name)
498  inputs->name = av_strdup("in");
499  for (cur = inputs; cur; cur = cur->next) {
500  if (!cur->name) {
501  av_log(log_ctx, AV_LOG_ERROR,
502  "Not enough inputs specified for the \"%s\" filter.\n",
503  cur->filter_ctx->filter->name);
504  ret = AVERROR(EINVAL);
505  goto fail;
506  }
507  if (!(match = extract_inout(cur->name, &open_outputs)))
508  continue;
509  ret = avfilter_link(match->filter_ctx, match->pad_idx,
510  cur->filter_ctx, cur->pad_idx);
511  avfilter_inout_free(&match);
512  if (ret < 0)
513  goto fail;
514  }
515 
516  /* Last output can be omitted if it is "[out]" */
517  if (outputs && !outputs->name)
518  outputs->name = av_strdup("out");
519  for (cur = outputs; cur; cur = cur->next) {
520  if (!cur->name) {
521  av_log(log_ctx, AV_LOG_ERROR,
522  "Invalid filterchain containing an unlabelled output pad: \"%s\"\n",
523  filters);
524  ret = AVERROR(EINVAL);
525  goto fail;
526  }
527  if (!(match = extract_inout(cur->name, &open_inputs)))
528  continue;
529  ret = avfilter_link(cur->filter_ctx, cur->pad_idx,
530  match->filter_ctx, match->pad_idx);
531  avfilter_inout_free(&match);
532  if (ret < 0)
533  goto fail;
534  }
535 
536  fail:
537  if (ret < 0) {
538  while (graph->nb_filters)
539  avfilter_free(graph->filters[0]);
540  av_freep(&graph->filters);
541  }
542  avfilter_inout_free(&inputs);
543  avfilter_inout_free(&outputs);
544  avfilter_inout_free(&open_inputs);
545  avfilter_inout_free(&open_outputs);
546  return ret;
547 }
AVFilterContext ** filters
Definition: avfilter.h:605
void avfilter_free(AVFilterContext *filter)
Free a filter context.
Definition: avfilter.c:508
static const AVFilterPad outputs[]
Definition: af_ashowinfo.c:259
Main libavfilter public API header.
memory handling functions
void avfilter_inout_free(AVFilterInOut **inout)
Free the supplied list of AVFilterInOut and set *inout to NULL.
Definition: graphparser.c:214
struct AVFilterInOut * next
next input/input in the list, NULL if this is the last
Definition: avfilter.h:758
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)
char * scale_sws_opts
sws options to use for the auto-inserted scale filters
Definition: avfilter.h:608
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
int avfilter_link(AVFilterContext *src, unsigned srcpad, AVFilterContext *dst, unsigned dstpad)
Link two filters together.
Definition: avfilter.c:77
static void insert_inout(AVFilterInOut **inouts, AVFilterInOut *element)
Definition: graphparser.c:241
int avfilter_graph_parse2(AVFilterGraph *graph, const char *filters, AVFilterInOut **inputs, AVFilterInOut **outputs)
Add a graph described by a string to a graph.
Definition: graphparser.c:418
static int parse_inputs(const char **buf, AVFilterInOut **curr_inputs, AVFilterInOut **open_outputs, void *log_ctx)
Definition: graphparser.c:307
#define src
Definition: vp8dsp.c:254
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:124
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:190
static AVFilterInOut * extract_inout(const char *label, AVFilterInOut **links)
Definition: graphparser.c:224
#define AVERROR(e)
Definition: error.h:43
unsigned nb_outputs
number of output pads
Definition: avfilter.h:275
size_t av_strlcpy(char *dst, const char *src, size_t size)
Copy the string src to dst, but no more than size - 1 bytes, and null-terminate dst.
Definition: avstring.c:81
#define fail()
Definition: checkasm.h:80
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
AVFilter * avfilter_get_by_name(const char *name)
Get a filter definition matching the given name.
Definition: avfilter.c:299
static void filter(MpegAudioContext *s, int ch, const short *samples, int incr)
Definition: mpegaudioenc.c:307
AVDictionary * opts
Definition: movenc.c:50
const char * name
Definition: qsvenc.c:44
unsigned nb_inputs
number of input pads
Definition: avfilter.h:271
static void append_sws_flags(const char **args, const char *sws_opts, char *tmp)
Definition: graphparser.c:86
AVFormatContext * ctx
Definition: movenc.c:48
AVFilterContext * filter_ctx
filter context associated to this input/output
Definition: avfilter.h:752
int avfilter_init_str(AVFilterContext *filter, const char *args)
Initialize a filter with the supplied parameters.
Definition: avfilter.c:615
A linked-list of the inputs/outputs of the filter chain.
Definition: avfilter.h:747
NULL
Definition: eval.c:55
#define AV_LOG_INFO
Standard information.
Definition: log.h:135
char * av_strdup(const char *s)
Duplicate the string s.
Definition: mem.c:219
static int parse_sws_flags(const char **buf, AVFilterGraph *graph)
Definition: graphparser.c:395
static void append_inout(AVFilterInOut **inouts, AVFilterInOut **element)
Definition: graphparser.c:247
int avfilter_graph_parse(AVFilterGraph *graph, const char *filters, AVFilterInOut *open_inputs, AVFilterInOut *open_outputs, void *log_ctx)
Add a graph described by a string to a graph.
Definition: graphparser.c:486
Filter definition.
Definition: avfilter.h:120
int index
Definition: gxfenc.c:72
static const AVFilterPad inputs[]
Definition: af_ashowinfo.c:248
int pad_idx
index of the filt_ctx pad to use for linking
Definition: avfilter.h:755
struct SwsContext * sws_opts
Definition: cmdutils.c:58
const char * name
Filter name.
Definition: avfilter.h:124
unsigned nb_filters
Definition: avfilter.h:606
static int parse_filter(AVFilterContext **filt_ctx, const char **buf, AVFilterGraph *graph, int index, void *log_ctx)
Parse a string of the form FILTER_NAME[=PARAMS], and create a corresponding filter instance which is ...
Definition: graphparser.c:191
char * name
unique name for this input/output in the list
Definition: avfilter.h:749
static int parse_outputs(const char **buf, AVFilterInOut **curr_inputs, AVFilterInOut **open_inputs, AVFilterInOut **open_outputs, void *log_ctx)
Definition: graphparser.c:347
AVFilterInOut * avfilter_inout_alloc(void)
Allocate a single AVFilterInOut entry.
Definition: graphparser.c:209
static int create_filter(AVFilterContext **filt_ctx, AVFilterGraph *ctx, int index, const char *filt_name, const char *args, void *log_ctx)
Create an instance of a filter, initialize and insert it in the filtergraph in *ctx.
Definition: graphparser.c:131
static int link_filter(AVFilterContext *src, int srcpad, AVFilterContext *dst, int dstpad, void *log_ctx)
Link two filters together.
Definition: graphparser.c:37
AVFilterContext * avfilter_graph_alloc_filter(AVFilterGraph *graph, const AVFilter *filter, const char *name)
Create a new filter instance in a filter graph.
static uint8_t tmp[8]
Definition: des.c:38
static char * parse_link_name(const char **buf, void *log_ctx)
Parse the name of a link, which has the format "[linkname]".
Definition: graphparser.c:58
static int link_filter_inouts(AVFilterContext *filt_ctx, AVFilterInOut **curr_inputs, AVFilterInOut **open_inputs, void *log_ctx)
Definition: graphparser.c:259
#define TMP_ARGS_SIZE
Definition: graphparser.c:84
An instance of a filter.
Definition: avfilter.h:262
#define WHITESPACES
Definition: graphparser.c:30
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