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 
26 #include "libavutil/avstring.h"
27 #include "avfilter.h"
28 #include "avfiltergraph.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 
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 
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 filter %d %s", index, filt_name);
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  ret = avfilter_open(filt_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 ret;
117  }
118 
119  if ((ret = avfilter_graph_add_filter(ctx, *filt_ctx)) < 0) {
120  avfilter_free(*filt_ctx);
121  return ret;
122  }
123 
124  if (!strcmp(filt_name, "scale") && args && !strstr(args, "flags")) {
125  snprintf(tmp_args, sizeof(tmp_args), "%s:%s",
126  args, ctx->scale_sws_opts);
127  args = tmp_args;
128  }
129 
130  if ((ret = avfilter_init_filter(*filt_ctx, args, NULL)) < 0) {
131  av_log(log_ctx, AV_LOG_ERROR,
132  "Error initializing filter '%s' with args '%s'\n", filt_name, args);
133  return ret;
134  }
135 
136  return 0;
137 }
138 
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 
173 static void free_inout(AVFilterInOut *head)
174 {
175  while (head) {
176  AVFilterInOut *next = head->next;
177  av_free(head->name);
178  av_free(head);
179  head = next;
180  }
181 }
182 
183 static AVFilterInOut *extract_inout(const char *label, AVFilterInOut **links)
184 {
185  AVFilterInOut *ret;
186 
187  while (*links && strcmp((*links)->name, label))
188  links = &((*links)->next);
189 
190  ret = *links;
191 
192  if (ret)
193  *links = ret->next;
194 
195  return ret;
196 }
197 
198 static void insert_inout(AVFilterInOut **inouts, AVFilterInOut *element)
199 {
200  element->next = *inouts;
201  *inouts = element;
202 }
203 
204 static int link_filter_inouts(AVFilterContext *filt_ctx,
205  AVFilterInOut **curr_inputs,
206  AVFilterInOut **open_inputs, void *log_ctx)
207 {
208  int pad = filt_ctx->input_count, ret;
209 
210  while (pad--) {
211  AVFilterInOut *p = *curr_inputs;
212  if (!p) {
213  av_log(log_ctx, AV_LOG_ERROR,
214  "Not enough inputs specified for the \"%s\" filter.\n",
215  filt_ctx->filter->name);
216  return AVERROR(EINVAL);
217  }
218 
219  *curr_inputs = (*curr_inputs)->next;
220 
221  if (p->filter_ctx) {
222  if ((ret = link_filter(p->filter_ctx, p->pad_idx, filt_ctx, pad, log_ctx)) < 0)
223  return ret;
224  av_free(p->name);
225  av_free(p);
226  } else {
227  p->filter_ctx = filt_ctx;
228  p->pad_idx = pad;
229  insert_inout(open_inputs, p);
230  }
231  }
232 
233  if (*curr_inputs) {
234  av_log(log_ctx, AV_LOG_ERROR,
235  "Too many inputs specified for the \"%s\" filter.\n",
236  filt_ctx->filter->name);
237  return AVERROR(EINVAL);
238  }
239 
240  pad = filt_ctx->output_count;
241  while (pad--) {
242  AVFilterInOut *currlinkn = av_mallocz(sizeof(AVFilterInOut));
243  if (!currlinkn)
244  return AVERROR(ENOMEM);
245  currlinkn->filter_ctx = filt_ctx;
246  currlinkn->pad_idx = pad;
247  insert_inout(curr_inputs, currlinkn);
248  }
249 
250  return 0;
251 }
252 
253 static int parse_inputs(const char **buf, AVFilterInOut **curr_inputs,
254  AVFilterInOut **open_outputs, void *log_ctx)
255 {
256  int pad = 0;
257 
258  while (**buf == '[') {
259  char *name = parse_link_name(buf, log_ctx);
260  AVFilterInOut *match;
261 
262  if (!name)
263  return AVERROR(EINVAL);
264 
265  /* First check if the label is not in the open_outputs list */
266  match = extract_inout(name, open_outputs);
267 
268  if (match) {
269  av_free(name);
270  } else {
271  /* Not in the list, so add it as an input */
272  if (!(match = av_mallocz(sizeof(AVFilterInOut))))
273  return AVERROR(ENOMEM);
274  match->name = name;
275  match->pad_idx = pad;
276  }
277 
278  insert_inout(curr_inputs, match);
279 
280  *buf += strspn(*buf, WHITESPACES);
281  pad++;
282  }
283 
284  return pad;
285 }
286 
287 static int parse_outputs(const char **buf, AVFilterInOut **curr_inputs,
288  AVFilterInOut **open_inputs,
289  AVFilterInOut **open_outputs, void *log_ctx)
290 {
291  int ret, pad = 0;
292 
293  while (**buf == '[') {
294  char *name = parse_link_name(buf, log_ctx);
295  AVFilterInOut *match;
296 
297  AVFilterInOut *input = *curr_inputs;
298  if (!input) {
299  av_log(log_ctx, AV_LOG_ERROR,
300  "No output pad can be associated to link label '%s'.\n",
301  name);
302  return AVERROR(EINVAL);
303  }
304  *curr_inputs = (*curr_inputs)->next;
305 
306  if (!name)
307  return AVERROR(EINVAL);
308 
309  /* First check if the label is not in the open_inputs list */
310  match = extract_inout(name, open_inputs);
311 
312  if (match) {
313  if ((ret = link_filter(input->filter_ctx, input->pad_idx,
314  match->filter_ctx, match->pad_idx, log_ctx)) < 0)
315  return ret;
316  av_free(match->name);
317  av_free(name);
318  av_free(match);
319  av_free(input);
320  } else {
321  /* Not in the list, so add the first input as a open_output */
322  input->name = name;
323  insert_inout(open_outputs, input);
324  }
325  *buf += strspn(*buf, WHITESPACES);
326  pad++;
327  }
328 
329  return pad;
330 }
331 
333  AVFilterInOut *open_inputs,
334  AVFilterInOut *open_outputs, void *log_ctx)
335 {
336  int index = 0, ret;
337  char chr = 0;
338 
339  AVFilterInOut *curr_inputs = NULL;
340 
341  do {
343  const char *filterchain = filters;
344  filters += strspn(filters, WHITESPACES);
345 
346  if ((ret = parse_inputs(&filters, &curr_inputs, &open_outputs, log_ctx)) < 0)
347  goto fail;
348 
349  if ((ret = parse_filter(&filter, &filters, graph, index, log_ctx)) < 0)
350  goto fail;
351 
352  if (filter->input_count == 1 && !curr_inputs && !index) {
353  /* First input can be omitted if it is "[in]" */
354  const char *tmp = "[in]";
355  if ((ret = parse_inputs(&tmp, &curr_inputs, &open_outputs, log_ctx)) < 0)
356  goto fail;
357  }
358 
359  if ((ret = link_filter_inouts(filter, &curr_inputs, &open_inputs, log_ctx)) < 0)
360  goto fail;
361 
362  if ((ret = parse_outputs(&filters, &curr_inputs, &open_inputs, &open_outputs,
363  log_ctx)) < 0)
364  goto fail;
365 
366  filters += strspn(filters, WHITESPACES);
367  chr = *filters++;
368 
369  if (chr == ';' && curr_inputs) {
370  av_log(log_ctx, AV_LOG_ERROR,
371  "Invalid filterchain containing an unlabelled output pad: \"%s\"\n",
372  filterchain);
373  ret = AVERROR(EINVAL);
374  goto fail;
375  }
376  index++;
377  } while (chr == ',' || chr == ';');
378 
379  if (chr) {
380  av_log(log_ctx, AV_LOG_ERROR,
381  "Unable to parse graph description substring: \"%s\"\n",
382  filters - 1);
383  ret = AVERROR(EINVAL);
384  goto fail;
385  }
386 
387  if (open_inputs && !strcmp(open_inputs->name, "out") && curr_inputs) {
388  /* Last output can be omitted if it is "[out]" */
389  const char *tmp = "[out]";
390  if ((ret = parse_outputs(&tmp, &curr_inputs, &open_inputs, &open_outputs,
391  log_ctx)) < 0)
392  goto fail;
393  }
394 
395  return 0;
396 
397  fail:
398  for (; graph->filter_count > 0; graph->filter_count--)
399  avfilter_free(graph->filters[graph->filter_count - 1]);
400  av_freep(&graph->filters);
401  free_inout(open_inputs);
402  free_inout(open_outputs);
403  free_inout(curr_inputs);
404  return ret;
405 }