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 <ctype.h>
24 #include <string.h>
25 #include <stdio.h>
26 
27 #include "libavutil/avstring.h"
28 #include "libavutil/mem.h"
29 #include "avfilter.h"
30 #include "avfiltergraph.h"
31 
32 #define WHITESPACES " \n\t"
33 
39 static int link_filter(AVFilterContext *src, int srcpad,
40  AVFilterContext *dst, int dstpad,
41  void *log_ctx)
42 {
43  int ret;
44  if ((ret = avfilter_link(src, srcpad, dst, dstpad))) {
45  av_log(log_ctx, AV_LOG_ERROR,
46  "Cannot create the link %s:%d -> %s:%d\n",
47  src->filter->name, srcpad, dst->filter->name, dstpad);
48  return ret;
49  }
50 
51  return 0;
52 }
53 
60 static char *parse_link_name(const char **buf, void *log_ctx)
61 {
62  const char *start = *buf;
63  char *name;
64  (*buf)++;
65 
66  name = av_get_token(buf, "]");
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 
96 static int create_filter(AVFilterContext **filt_ctx, AVFilterGraph *ctx, int index,
97  const char *filt_name, const char *args, void *log_ctx)
98 {
99  AVFilter *filt;
100  char inst_name[30];
101  char tmp_args[256];
102  int ret;
103 
104  snprintf(inst_name, sizeof(inst_name), "Parsed filter %d %s", index, filt_name);
105 
106  filt = avfilter_get_by_name(filt_name);
107 
108  if (!filt) {
109  av_log(log_ctx, AV_LOG_ERROR,
110  "No such filter: '%s'\n", filt_name);
111  return AVERROR(EINVAL);
112  }
113 
114  ret = avfilter_open(filt_ctx, filt, inst_name);
115  if (!*filt_ctx) {
116  av_log(log_ctx, AV_LOG_ERROR,
117  "Error creating filter '%s'\n", filt_name);
118  return ret;
119  }
120 
121  if ((ret = avfilter_graph_add_filter(ctx, *filt_ctx)) < 0) {
122  avfilter_free(*filt_ctx);
123  return ret;
124  }
125 
126  if (!strcmp(filt_name, "scale") && args && !strstr(args, "flags") &&
127  ctx->scale_sws_opts) {
128  snprintf(tmp_args, sizeof(tmp_args), "%s:%s",
129  args, ctx->scale_sws_opts);
130  args = tmp_args;
131  }
132 
133  if ((ret = avfilter_init_filter(*filt_ctx, args, NULL)) < 0) {
134  av_log(log_ctx, AV_LOG_ERROR,
135  "Error initializing filter '%s' with args '%s'\n", filt_name, args);
136  return ret;
137  }
138 
139  return 0;
140 }
141 
158 static int parse_filter(AVFilterContext **filt_ctx, const char **buf, AVFilterGraph *graph,
159  int index, void *log_ctx)
160 {
161  char *opts = NULL;
162  char *name = av_get_token(buf, "=,;[\n");
163  int ret;
164 
165  if (**buf == '=') {
166  (*buf)++;
167  opts = av_get_token(buf, "[],;\n");
168  }
169 
170  ret = create_filter(filt_ctx, graph, index, name, opts, log_ctx);
171  av_free(name);
172  av_free(opts);
173  return ret;
174 }
175 
177 {
178  return av_mallocz(sizeof(AVFilterInOut));
179 }
180 
182 {
183  while (*inout) {
184  AVFilterInOut *next = (*inout)->next;
185  av_freep(&(*inout)->name);
186  av_freep(inout);
187  *inout = next;
188  }
189 }
190 
191 static AVFilterInOut *extract_inout(const char *label, AVFilterInOut **links)
192 {
193  AVFilterInOut *ret;
194 
195  while (*links && (!(*links)->name || strcmp((*links)->name, label)))
196  links = &((*links)->next);
197 
198  ret = *links;
199 
200  if (ret) {
201  *links = ret->next;
202  ret->next = NULL;
203  }
204 
205  return ret;
206 }
207 
208 static void insert_inout(AVFilterInOut **inouts, AVFilterInOut *element)
209 {
210  element->next = *inouts;
211  *inouts = element;
212 }
213 
214 static void append_inout(AVFilterInOut **inouts, AVFilterInOut **element)
215 {
216  while (*inouts && (*inouts)->next)
217  inouts = &((*inouts)->next);
218 
219  if (!*inouts)
220  *inouts = *element;
221  else
222  (*inouts)->next = *element;
223  *element = NULL;
224 }
225 
226 static int link_filter_inouts(AVFilterContext *filt_ctx,
227  AVFilterInOut **curr_inputs,
228  AVFilterInOut **open_inputs, void *log_ctx)
229 {
230  int pad, ret;
231 
232  for (pad = 0; pad < filt_ctx->nb_inputs; pad++) {
233  AVFilterInOut *p = *curr_inputs;
234 
235  if (p) {
236  *curr_inputs = (*curr_inputs)->next;
237  p->next = NULL;
238  } else if (!(p = av_mallocz(sizeof(*p))))
239  return AVERROR(ENOMEM);
240 
241  if (p->filter_ctx) {
242  ret = link_filter(p->filter_ctx, p->pad_idx, filt_ctx, pad, log_ctx);
243  av_free(p->name);
244  av_free(p);
245  if (ret < 0)
246  return ret;
247  } else {
248  p->filter_ctx = filt_ctx;
249  p->pad_idx = pad;
250  append_inout(open_inputs, &p);
251  }
252  }
253 
254  if (*curr_inputs) {
255  av_log(log_ctx, AV_LOG_ERROR,
256  "Too many inputs specified for the \"%s\" filter.\n",
257  filt_ctx->filter->name);
258  return AVERROR(EINVAL);
259  }
260 
261  pad = filt_ctx->nb_outputs;
262  while (pad--) {
263  AVFilterInOut *currlinkn = av_mallocz(sizeof(AVFilterInOut));
264  if (!currlinkn)
265  return AVERROR(ENOMEM);
266  currlinkn->filter_ctx = filt_ctx;
267  currlinkn->pad_idx = pad;
268  insert_inout(curr_inputs, currlinkn);
269  }
270 
271  return 0;
272 }
273 
274 static int parse_inputs(const char **buf, AVFilterInOut **curr_inputs,
275  AVFilterInOut **open_outputs, void *log_ctx)
276 {
277  AVFilterInOut *parsed_inputs = NULL;
278  int pad = 0;
279 
280  while (**buf == '[') {
281  char *name = parse_link_name(buf, log_ctx);
282  AVFilterInOut *match;
283 
284  if (!name)
285  return AVERROR(EINVAL);
286 
287  /* First check if the label is not in the open_outputs list */
288  match = extract_inout(name, open_outputs);
289 
290  if (match) {
291  av_free(name);
292  } else {
293  /* Not in the list, so add it as an input */
294  if (!(match = av_mallocz(sizeof(AVFilterInOut)))) {
295  av_free(name);
296  return AVERROR(ENOMEM);
297  }
298  match->name = name;
299  match->pad_idx = pad;
300  }
301 
302  append_inout(&parsed_inputs, &match);
303 
304  *buf += strspn(*buf, WHITESPACES);
305  pad++;
306  }
307 
308  append_inout(&parsed_inputs, curr_inputs);
309  *curr_inputs = parsed_inputs;
310 
311  return pad;
312 }
313 
314 static int parse_outputs(const char **buf, AVFilterInOut **curr_inputs,
315  AVFilterInOut **open_inputs,
316  AVFilterInOut **open_outputs, void *log_ctx)
317 {
318  int ret, pad = 0;
319 
320  while (**buf == '[') {
321  char *name = parse_link_name(buf, log_ctx);
322  AVFilterInOut *match;
323 
324  AVFilterInOut *input = *curr_inputs;
325 
326  if (!name)
327  return AVERROR(EINVAL);
328 
329  if (!input) {
330  av_log(log_ctx, AV_LOG_ERROR,
331  "No output pad can be associated to link label '%s'.\n", name);
332  av_free(name);
333  return AVERROR(EINVAL);
334  }
335  *curr_inputs = (*curr_inputs)->next;
336 
337  /* First check if the label is not in the open_inputs list */
338  match = extract_inout(name, open_inputs);
339 
340  if (match) {
341  if ((ret = link_filter(input->filter_ctx, input->pad_idx,
342  match->filter_ctx, match->pad_idx, log_ctx)) < 0) {
343  av_free(name);
344  return ret;
345  }
346  av_free(match->name);
347  av_free(name);
348  av_free(match);
349  av_free(input);
350  } else {
351  /* Not in the list, so add the first input as a open_output */
352  input->name = name;
353  insert_inout(open_outputs, input);
354  }
355  *buf += strspn(*buf, WHITESPACES);
356  pad++;
357  }
358 
359  return pad;
360 }
361 
362 static int parse_sws_flags(const char **buf, AVFilterGraph *graph)
363 {
364  char *p = strchr(*buf, ';');
365 
366  if (strncmp(*buf, "sws_flags=", 10))
367  return 0;
368 
369  if (!p) {
370  av_log(graph, AV_LOG_ERROR, "sws_flags not terminated with ';'.\n");
371  return AVERROR(EINVAL);
372  }
373 
374  *buf += 4; // keep the 'flags=' part
375 
376  av_freep(&graph->scale_sws_opts);
377  if (!(graph->scale_sws_opts = av_mallocz(p - *buf + 1)))
378  return AVERROR(ENOMEM);
379  av_strlcpy(graph->scale_sws_opts, *buf, p - *buf + 1);
380 
381  *buf = p + 1;
382  return 0;
383 }
384 
385 int avfilter_graph_parse2(AVFilterGraph *graph, const char *filters,
388 {
389  int index = 0, ret;
390  char chr = 0;
391 
392  AVFilterInOut *curr_inputs = NULL, *open_inputs = NULL, *open_outputs = NULL;
393 
394  filters += strspn(filters, WHITESPACES);
395 
396  if ((ret = parse_sws_flags(&filters, graph)) < 0)
397  goto fail;
398 
399  do {
401  filters += strspn(filters, WHITESPACES);
402 
403  if ((ret = parse_inputs(&filters, &curr_inputs, &open_outputs, graph)) < 0)
404  goto fail;
405 
406  if ((ret = parse_filter(&filter, &filters, graph, index, graph)) < 0)
407  goto fail;
408 
409 
410  if ((ret = link_filter_inouts(filter, &curr_inputs, &open_inputs, graph)) < 0)
411  goto fail;
412 
413  if ((ret = parse_outputs(&filters, &curr_inputs, &open_inputs, &open_outputs,
414  graph)) < 0)
415  goto fail;
416 
417  filters += strspn(filters, WHITESPACES);
418  chr = *filters++;
419 
420  if (chr == ';' && curr_inputs)
421  append_inout(&open_outputs, &curr_inputs);
422  index++;
423  } while (chr == ',' || chr == ';');
424 
425  if (chr) {
426  av_log(graph, AV_LOG_ERROR,
427  "Unable to parse graph description substring: \"%s\"\n",
428  filters - 1);
429  ret = AVERROR(EINVAL);
430  goto fail;
431  }
432 
433  append_inout(&open_outputs, &curr_inputs);
434 
435  *inputs = open_inputs;
436  *outputs = open_outputs;
437  return 0;
438 
439  fail:
440  for (; graph->filter_count > 0; graph->filter_count--)
441  avfilter_free(graph->filters[graph->filter_count - 1]);
442  av_freep(&graph->filters);
443  avfilter_inout_free(&open_inputs);
444  avfilter_inout_free(&open_outputs);
445  avfilter_inout_free(&curr_inputs);
446 
447  *inputs = NULL;
448  *outputs = NULL;
449 
450  return ret;
451 }
452 
453 int avfilter_graph_parse(AVFilterGraph *graph, const char *filters,
454  AVFilterInOut *open_inputs,
455  AVFilterInOut *open_outputs, void *log_ctx)
456 {
457  int ret;
458  AVFilterInOut *cur, *match, *inputs = NULL, *outputs = NULL;
459 
460  if ((ret = avfilter_graph_parse2(graph, filters, &inputs, &outputs)) < 0)
461  goto fail;
462 
463  /* First input can be omitted if it is "[in]" */
464  if (inputs && !inputs->name)
465  inputs->name = av_strdup("in");
466  for (cur = inputs; cur; cur = cur->next) {
467  if (!cur->name) {
468  av_log(log_ctx, AV_LOG_ERROR,
469  "Not enough inputs specified for the \"%s\" filter.\n",
470  cur->filter_ctx->filter->name);
471  ret = AVERROR(EINVAL);
472  goto fail;
473  }
474  if (!(match = extract_inout(cur->name, &open_outputs)))
475  continue;
476  ret = avfilter_link(match->filter_ctx, match->pad_idx,
477  cur->filter_ctx, cur->pad_idx);
478  avfilter_inout_free(&match);
479  if (ret < 0)
480  goto fail;
481  }
482 
483  /* Last output can be omitted if it is "[out]" */
484  if (outputs && !outputs->name)
485  outputs->name = av_strdup("out");
486  for (cur = outputs; cur; cur = cur->next) {
487  if (!cur->name) {
488  av_log(log_ctx, AV_LOG_ERROR,
489  "Invalid filterchain containing an unlabelled output pad: \"%s\"\n",
490  filters);
491  ret = AVERROR(EINVAL);
492  goto fail;
493  }
494  if (!(match = extract_inout(cur->name, &open_inputs)))
495  continue;
496  ret = avfilter_link(cur->filter_ctx, cur->pad_idx,
497  match->filter_ctx, match->pad_idx);
498  avfilter_inout_free(&match);
499  if (ret < 0)
500  goto fail;
501  }
502 
503  fail:
504  if (ret < 0) {
505  for (; graph->filter_count > 0; graph->filter_count--)
506  avfilter_free(graph->filters[graph->filter_count - 1]);
507  av_freep(&graph->filters);
508  }
509  avfilter_inout_free(&inputs);
510  avfilter_inout_free(&outputs);
511  avfilter_inout_free(&open_inputs);
512  avfilter_inout_free(&open_outputs);
513  return ret;
514 }