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