FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 FFmpeg.
7  *
8  * FFmpeg 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  * FFmpeg 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 FFmpeg; 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 
32 /**
33  * Link two filters together.
34  *
35  * @see avfilter_link()
36  */
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 
52 /**
53  * Parse the name of a link, which has the format "[linkname]".
54  *
55  * @return a pointer (that need to be freed after use) to the name
56  * between parenthesis
57  */
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 /**
85  * Create an instance of a filter, initialize and insert it in the
86  * filtergraph in *ctx.
87  *
88  * @param filt_ctx put here a filter context in case of successful creation and configuration, NULL otherwise.
89  * @param ctx the filtergraph context
90  * @param index an index which is supposed to be unique for each filter instance added to the filtergraph
91  * @param filt_name the name of the filter to create
92  * @param args the arguments provided to the filter during its initialization
93  * @param log_ctx the log context to use
94  * @return >= 0 in case of success, a negative AVERROR code otherwise
95  */
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 = NULL;
102  int ret;
103 
104  snprintf(inst_name, sizeof(inst_name), "Parsed_%s_%d", filt_name, index);
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  *filt_ctx = avfilter_graph_alloc_filter(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 AVERROR(ENOMEM);
119  }
120 
121  if (!strcmp(filt_name, "scale") && args && !strstr(args, "flags") &&
122  ctx->scale_sws_opts) {
123  tmp_args = av_asprintf("%s:%s",
124  args, ctx->scale_sws_opts);
125  if (!tmp_args)
126  return AVERROR(ENOMEM);
127  args = tmp_args;
128  }
129 
130  ret = avfilter_init_str(*filt_ctx, args);
131  if (ret < 0) {
132  av_log(log_ctx, AV_LOG_ERROR,
133  "Error initializing filter '%s'", filt_name);
134  if (args)
135  av_log(log_ctx, AV_LOG_ERROR, " with args '%s'", args);
136  av_log(log_ctx, AV_LOG_ERROR, "\n");
137  avfilter_free(*filt_ctx);
138  *filt_ctx = NULL;
139  }
140 
141  av_free(tmp_args);
142  return ret;
143 }
144 
145 /**
146  * Parse a string of the form FILTER_NAME[=PARAMS], and create a
147  * corresponding filter instance which is added to graph with
148  * create_filter().
149  *
150  * @param filt_ctx Pointer that is set to the created and configured filter
151  * context on success, set to NULL on failure.
152  * @param filt_ctx put here a pointer to the created filter context on
153  * success, NULL otherwise
154  * @param buf pointer to the buffer to parse, *buf will be updated to
155  * point to the char next after the parsed string
156  * @param index an index which is assigned to the created filter
157  * instance, and which is supposed to be unique for each filter
158  * instance added to the filtergraph
159  * @return >= 0 in case of success, a negative AVERROR code otherwise
160  */
161 static int parse_filter(AVFilterContext **filt_ctx, const char **buf, AVFilterGraph *graph,
162  int index, void *log_ctx)
163 {
164  char *opts = NULL;
165  char *name = av_get_token(buf, "=,;[\n");
166  int ret;
167 
168  if (**buf == '=') {
169  (*buf)++;
170  opts = av_get_token(buf, "[],;\n");
171  }
172 
173  ret = create_filter(filt_ctx, graph, index, name, opts, log_ctx);
174  av_free(name);
175  av_free(opts);
176  return ret;
177 }
178 
180 {
181  return av_mallocz(sizeof(AVFilterInOut));
182 }
183 
185 {
186  while (*inout) {
187  AVFilterInOut *next = (*inout)->next;
188  av_freep(&(*inout)->name);
189  av_freep(inout);
190  *inout = next;
191  }
192 }
193 
194 static AVFilterInOut *extract_inout(const char *label, AVFilterInOut **links)
195 {
197 
198  while (*links && (!(*links)->name || strcmp((*links)->name, label)))
199  links = &((*links)->next);
200 
201  ret = *links;
202 
203  if (ret) {
204  *links = ret->next;
205  ret->next = NULL;
206  }
207 
208  return ret;
209 }
210 
211 static void insert_inout(AVFilterInOut **inouts, AVFilterInOut *element)
212 {
213  element->next = *inouts;
214  *inouts = element;
215 }
216 
217 static void append_inout(AVFilterInOut **inouts, AVFilterInOut **element)
218 {
219  while (*inouts && (*inouts)->next)
220  inouts = &((*inouts)->next);
221 
222  if (!*inouts)
223  *inouts = *element;
224  else
225  (*inouts)->next = *element;
226  *element = NULL;
227 }
228 
229 static int link_filter_inouts(AVFilterContext *filt_ctx,
230  AVFilterInOut **curr_inputs,
231  AVFilterInOut **open_inputs, void *log_ctx)
232 {
233  int pad, ret;
234 
235  for (pad = 0; pad < filt_ctx->nb_inputs; pad++) {
236  AVFilterInOut *p = *curr_inputs;
237 
238  if (p) {
239  *curr_inputs = (*curr_inputs)->next;
240  p->next = NULL;
241  } else if (!(p = av_mallocz(sizeof(*p))))
242  return AVERROR(ENOMEM);
243 
244  if (p->filter_ctx) {
245  ret = link_filter(p->filter_ctx, p->pad_idx, filt_ctx, pad, log_ctx);
246  av_freep(&p->name);
247  av_freep(&p);
248  if (ret < 0)
249  return ret;
250  } else {
251  p->filter_ctx = filt_ctx;
252  p->pad_idx = pad;
253  append_inout(open_inputs, &p);
254  }
255  }
256 
257  if (*curr_inputs) {
258  av_log(log_ctx, AV_LOG_ERROR,
259  "Too many inputs specified for the \"%s\" filter.\n",
260  filt_ctx->filter->name);
261  return AVERROR(EINVAL);
262  }
263 
264  pad = filt_ctx->nb_outputs;
265  while (pad--) {
266  AVFilterInOut *currlinkn = av_mallocz(sizeof(AVFilterInOut));
267  if (!currlinkn)
268  return AVERROR(ENOMEM);
269  currlinkn->filter_ctx = filt_ctx;
270  currlinkn->pad_idx = pad;
271  insert_inout(curr_inputs, currlinkn);
272  }
273 
274  return 0;
275 }
276 
277 static int parse_inputs(const char **buf, AVFilterInOut **curr_inputs,
278  AVFilterInOut **open_outputs, void *log_ctx)
279 {
280  AVFilterInOut *parsed_inputs = NULL;
281  int pad = 0;
282 
283  while (**buf == '[') {
284  char *name = parse_link_name(buf, log_ctx);
285  AVFilterInOut *match;
286 
287  if (!name)
288  return AVERROR(EINVAL);
289 
290  /* First check if the label is not in the open_outputs list */
291  match = extract_inout(name, open_outputs);
292 
293  if (match) {
294  av_free(name);
295  } else {
296  /* Not in the list, so add it as an input */
297  if (!(match = av_mallocz(sizeof(AVFilterInOut)))) {
298  av_free(name);
299  return AVERROR(ENOMEM);
300  }
301  match->name = name;
302  match->pad_idx = pad;
303  }
304 
305  append_inout(&parsed_inputs, &match);
306 
307  *buf += strspn(*buf, WHITESPACES);
308  pad++;
309  }
310 
311  append_inout(&parsed_inputs, curr_inputs);
312  *curr_inputs = parsed_inputs;
313 
314  return pad;
315 }
316 
317 static int parse_outputs(const char **buf, AVFilterInOut **curr_inputs,
318  AVFilterInOut **open_inputs,
319  AVFilterInOut **open_outputs, void *log_ctx)
320 {
321  int ret, pad = 0;
322 
323  while (**buf == '[') {
324  char *name = parse_link_name(buf, log_ctx);
325  AVFilterInOut *match;
326 
327  AVFilterInOut *input = *curr_inputs;
328 
329  if (!name)
330  return AVERROR(EINVAL);
331 
332  if (!input) {
333  av_log(log_ctx, AV_LOG_ERROR,
334  "No output pad can be associated to link label '%s'.\n", name);
335  av_free(name);
336  return AVERROR(EINVAL);
337  }
338  *curr_inputs = (*curr_inputs)->next;
339 
340  /* First check if the label is not in the open_inputs list */
341  match = extract_inout(name, open_inputs);
342 
343  if (match) {
344  if ((ret = link_filter(input->filter_ctx, input->pad_idx,
345  match->filter_ctx, match->pad_idx, log_ctx)) < 0) {
346  av_free(name);
347  return ret;
348  }
349  av_freep(&match->name);
350  av_freep(&name);
351  av_freep(&match);
352  av_freep(&input);
353  } else {
354  /* Not in the list, so add the first input as a open_output */
355  input->name = name;
356  insert_inout(open_outputs, input);
357  }
358  *buf += strspn(*buf, WHITESPACES);
359  pad++;
360  }
361 
362  return pad;
363 }
364 
365 static int parse_sws_flags(const char **buf, AVFilterGraph *graph)
366 {
367  char *p = strchr(*buf, ';');
368 
369  if (strncmp(*buf, "sws_flags=", 10))
370  return 0;
371 
372  if (!p) {
373  av_log(graph, AV_LOG_ERROR, "sws_flags not terminated with ';'.\n");
374  return AVERROR(EINVAL);
375  }
376 
377  *buf += 4; // keep the 'flags=' part
378 
379  av_freep(&graph->scale_sws_opts);
380  if (!(graph->scale_sws_opts = av_mallocz(p - *buf + 1)))
381  return AVERROR(ENOMEM);
382  av_strlcpy(graph->scale_sws_opts, *buf, p - *buf + 1);
383 
384  *buf = p + 1;
385  return 0;
386 }
387 
391 {
392  int index = 0, ret = 0;
393  char chr = 0;
394 
395  AVFilterInOut *curr_inputs = NULL, *open_inputs = NULL, *open_outputs = NULL;
396 
397  filters += strspn(filters, WHITESPACES);
398 
399  if ((ret = parse_sws_flags(&filters, graph)) < 0)
400  goto fail;
401 
402  do {
404  filters += strspn(filters, WHITESPACES);
405 
406  if ((ret = parse_inputs(&filters, &curr_inputs, &open_outputs, graph)) < 0)
407  goto end;
408  if ((ret = parse_filter(&filter, &filters, graph, index, graph)) < 0)
409  goto end;
410 
411 
412  if ((ret = link_filter_inouts(filter, &curr_inputs, &open_inputs, graph)) < 0)
413  goto end;
414 
415  if ((ret = parse_outputs(&filters, &curr_inputs, &open_inputs, &open_outputs,
416  graph)) < 0)
417  goto end;
418 
419  filters += strspn(filters, WHITESPACES);
420  chr = *filters++;
421 
422  if (chr == ';' && curr_inputs)
423  append_inout(&open_outputs, &curr_inputs);
424  index++;
425  } while (chr == ',' || chr == ';');
426 
427  if (chr) {
428  av_log(graph, AV_LOG_ERROR,
429  "Unable to parse graph description substring: \"%s\"\n",
430  filters - 1);
431  ret = AVERROR(EINVAL);
432  goto end;
433  }
434 
435  append_inout(&open_outputs, &curr_inputs);
436 
437 
438  *inputs = open_inputs;
439  *outputs = open_outputs;
440  return 0;
441 
442  fail:end:
443  while (graph->nb_filters)
444  avfilter_free(graph->filters[0]);
445  av_freep(&graph->filters);
446  avfilter_inout_free(&open_inputs);
447  avfilter_inout_free(&open_outputs);
448  avfilter_inout_free(&curr_inputs);
449 
450  *inputs = NULL;
451  *outputs = NULL;
452 
453  return ret;
454 }
455 
456 #if HAVE_INCOMPATIBLE_LIBAV_ABI || !FF_API_OLD_GRAPH_PARSE
458  AVFilterInOut *open_inputs,
459  AVFilterInOut *open_outputs, void *log_ctx)
460 {
461  int ret;
462  AVFilterInOut *cur, *match, *inputs = NULL, *outputs = NULL;
463 
464  if ((ret = avfilter_graph_parse2(graph, filters, &inputs, &outputs)) < 0)
465  goto fail;
466 
467  /* First input can be omitted if it is "[in]" */
468  if (inputs && !inputs->name)
469  inputs->name = av_strdup("in");
470  for (cur = inputs; cur; cur = cur->next) {
471  if (!cur->name) {
472  av_log(log_ctx, AV_LOG_ERROR,
473  "Not enough inputs specified for the \"%s\" filter.\n",
474  cur->filter_ctx->filter->name);
475  ret = AVERROR(EINVAL);
476  goto fail;
477  }
478  if (!(match = extract_inout(cur->name, &open_outputs)))
479  continue;
480  ret = avfilter_link(match->filter_ctx, match->pad_idx,
481  cur->filter_ctx, cur->pad_idx);
482  avfilter_inout_free(&match);
483  if (ret < 0)
484  goto fail;
485  }
486 
487  /* Last output can be omitted if it is "[out]" */
488  if (outputs && !outputs->name)
489  outputs->name = av_strdup("out");
490  for (cur = outputs; cur; cur = cur->next) {
491  if (!cur->name) {
492  av_log(log_ctx, AV_LOG_ERROR,
493  "Invalid filterchain containing an unlabelled output pad: \"%s\"\n",
494  filters);
495  ret = AVERROR(EINVAL);
496  goto fail;
497  }
498  if (!(match = extract_inout(cur->name, &open_inputs)))
499  continue;
500  ret = avfilter_link(cur->filter_ctx, cur->pad_idx,
501  match->filter_ctx, match->pad_idx);
502  avfilter_inout_free(&match);
503  if (ret < 0)
504  goto fail;
505  }
506 
507  fail:
508  if (ret < 0) {
509  while (graph->nb_filters)
510  avfilter_free(graph->filters[0]);
511  av_freep(&graph->filters);
512  }
513  avfilter_inout_free(&inputs);
514  avfilter_inout_free(&outputs);
515  avfilter_inout_free(&open_inputs);
516  avfilter_inout_free(&open_outputs);
517  return ret;
518 #else
519 int avfilter_graph_parse(AVFilterGraph *graph, const char *filters,
520  AVFilterInOut **inputs, AVFilterInOut **outputs,
521  void *log_ctx)
522 {
523  return avfilter_graph_parse_ptr(graph, filters, inputs, outputs, log_ctx);
524 #endif
525 }
526 
527 int avfilter_graph_parse_ptr(AVFilterGraph *graph, const char *filters,
528  AVFilterInOut **open_inputs_ptr, AVFilterInOut **open_outputs_ptr,
529  void *log_ctx)
530 {
531  int index = 0, ret = 0;
532  char chr = 0;
533 
534  AVFilterInOut *curr_inputs = NULL;
535  AVFilterInOut *open_inputs = open_inputs_ptr ? *open_inputs_ptr : NULL;
536  AVFilterInOut *open_outputs = open_outputs_ptr ? *open_outputs_ptr : NULL;
537 
538  if ((ret = parse_sws_flags(&filters, graph)) < 0)
539  goto end;
540 
541  do {
543  const char *filterchain = filters;
544  filters += strspn(filters, WHITESPACES);
545 
546  if ((ret = parse_inputs(&filters, &curr_inputs, &open_outputs, log_ctx)) < 0)
547  goto end;
548 
549  if ((ret = parse_filter(&filter, &filters, graph, index, log_ctx)) < 0)
550  goto end;
551 
552  if (filter->nb_inputs == 1 && !curr_inputs && !index) {
553  /* First input pad, assume it is "[in]" if not specified */
554  const char *tmp = "[in]";
555  if ((ret = parse_inputs(&tmp, &curr_inputs, &open_outputs, log_ctx)) < 0)
556  goto end;
557  }
558 
559  if ((ret = link_filter_inouts(filter, &curr_inputs, &open_inputs, log_ctx)) < 0)
560  goto end;
561 
562  if ((ret = parse_outputs(&filters, &curr_inputs, &open_inputs, &open_outputs,
563  log_ctx)) < 0)
564  goto end;
565 
566  filters += strspn(filters, WHITESPACES);
567  chr = *filters++;
568 
569  if (chr == ';' && curr_inputs) {
570  av_log(log_ctx, AV_LOG_ERROR,
571  "Invalid filterchain containing an unlabelled output pad: \"%s\"\n",
572  filterchain);
573  ret = AVERROR(EINVAL);
574  goto end;
575  }
576  index++;
577  } while (chr == ',' || chr == ';');
578 
579  if (chr) {
580  av_log(log_ctx, AV_LOG_ERROR,
581  "Unable to parse graph description substring: \"%s\"\n",
582  filters - 1);
583  ret = AVERROR(EINVAL);
584  goto end;
585  }
586 
587  if (curr_inputs) {
588  /* Last output pad, assume it is "[out]" if not specified */
589  const char *tmp = "[out]";
590  if ((ret = parse_outputs(&tmp, &curr_inputs, &open_inputs, &open_outputs,
591  log_ctx)) < 0)
592  goto end;
593  }
594 
595 end:
596  /* clear open_in/outputs only if not passed as parameters */
597  if (open_inputs_ptr) *open_inputs_ptr = open_inputs;
598  else avfilter_inout_free(&open_inputs);
599  if (open_outputs_ptr) *open_outputs_ptr = open_outputs;
600  else avfilter_inout_free(&open_outputs);
601  avfilter_inout_free(&curr_inputs);
602 
603  if (ret < 0) {
604  while (graph->nb_filters)
605  avfilter_free(graph->filters[0]);
606  av_freep(&graph->filters);
607  }
608  return ret;
609 }