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