libavfilter/graphparser.c
Go to the documentation of this file.
00001 /*
00002  * filter graph parser
00003  * Copyright (c) 2008 Vitor Sessak
00004  * Copyright (c) 2007 Bobby Bingham
00005  *
00006  * This file is part of Libav.
00007  *
00008  * Libav is free software; you can redistribute it and/or
00009  * modify it under the terms of the GNU Lesser General Public
00010  * License as published by the Free Software Foundation; either
00011  * version 2.1 of the License, or (at your option) any later version.
00012  *
00013  * Libav is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016  * Lesser General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU Lesser General Public
00019  * License along with Libav; if not, write to the Free Software
00020  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00021  */
00022 
00023 #include <ctype.h>
00024 #include <string.h>
00025 
00026 #include "libavutil/avstring.h"
00027 #include "avfilter.h"
00028 #include "avfiltergraph.h"
00029 
00030 #define WHITESPACES " \n\t"
00031 
00037 static int link_filter(AVFilterContext *src, int srcpad,
00038                        AVFilterContext *dst, int dstpad,
00039                        void *log_ctx)
00040 {
00041     int ret;
00042     if ((ret = avfilter_link(src, srcpad, dst, dstpad))) {
00043         av_log(log_ctx, AV_LOG_ERROR,
00044                "Cannot create the link %s:%d -> %s:%d\n",
00045                src->filter->name, srcpad, dst->filter->name, dstpad);
00046         return ret;
00047     }
00048 
00049     return 0;
00050 }
00051 
00058 static char *parse_link_name(const char **buf, void *log_ctx)
00059 {
00060     const char *start = *buf;
00061     char *name;
00062     (*buf)++;
00063 
00064     name = av_get_token(buf, "]");
00065 
00066     if (!name[0]) {
00067         av_log(log_ctx, AV_LOG_ERROR,
00068                "Bad (empty?) label found in the following: \"%s\".\n", start);
00069         goto fail;
00070     }
00071 
00072     if (*(*buf)++ != ']') {
00073         av_log(log_ctx, AV_LOG_ERROR,
00074                "Mismatched '[' found in the following: \"%s\".\n", start);
00075     fail:
00076         av_freep(&name);
00077     }
00078 
00079     return name;
00080 }
00081 
00094 static int create_filter(AVFilterContext **filt_ctx, AVFilterGraph *ctx, int index,
00095                          const char *filt_name, const char *args, void *log_ctx)
00096 {
00097     AVFilter *filt;
00098     char inst_name[30];
00099     char tmp_args[256];
00100     int ret;
00101 
00102     snprintf(inst_name, sizeof(inst_name), "Parsed filter %d %s", index, filt_name);
00103 
00104     filt = avfilter_get_by_name(filt_name);
00105 
00106     if (!filt) {
00107         av_log(log_ctx, AV_LOG_ERROR,
00108                "No such filter: '%s'\n", filt_name);
00109         return AVERROR(EINVAL);
00110     }
00111 
00112     ret = avfilter_open(filt_ctx, filt, inst_name);
00113     if (!*filt_ctx) {
00114         av_log(log_ctx, AV_LOG_ERROR,
00115                "Error creating filter '%s'\n", filt_name);
00116         return ret;
00117     }
00118 
00119     if ((ret = avfilter_graph_add_filter(ctx, *filt_ctx)) < 0) {
00120         avfilter_free(*filt_ctx);
00121         return ret;
00122     }
00123 
00124     if (!strcmp(filt_name, "scale") && args && !strstr(args, "flags") &&
00125         ctx->scale_sws_opts) {
00126         snprintf(tmp_args, sizeof(tmp_args), "%s:%s",
00127                  args, ctx->scale_sws_opts);
00128         args = tmp_args;
00129     }
00130 
00131     if ((ret = avfilter_init_filter(*filt_ctx, args, NULL)) < 0) {
00132         av_log(log_ctx, AV_LOG_ERROR,
00133                "Error initializing filter '%s' with args '%s'\n", filt_name, args);
00134         return ret;
00135     }
00136 
00137     return 0;
00138 }
00139 
00156 static int parse_filter(AVFilterContext **filt_ctx, const char **buf, AVFilterGraph *graph,
00157                         int index, void *log_ctx)
00158 {
00159     char *opts = NULL;
00160     char *name = av_get_token(buf, "=,;[\n");
00161     int ret;
00162 
00163     if (**buf == '=') {
00164         (*buf)++;
00165         opts = av_get_token(buf, "[],;\n");
00166     }
00167 
00168     ret = create_filter(filt_ctx, graph, index, name, opts, log_ctx);
00169     av_free(name);
00170     av_free(opts);
00171     return ret;
00172 }
00173 
00174 static void free_inout(AVFilterInOut *head)
00175 {
00176     while (head) {
00177         AVFilterInOut *next = head->next;
00178         av_free(head->name);
00179         av_free(head);
00180         head = next;
00181     }
00182 }
00183 
00184 static AVFilterInOut *extract_inout(const char *label, AVFilterInOut **links)
00185 {
00186     AVFilterInOut *ret;
00187 
00188     while (*links && strcmp((*links)->name, label))
00189         links = &((*links)->next);
00190 
00191     ret = *links;
00192 
00193     if (ret)
00194         *links = ret->next;
00195 
00196     return ret;
00197 }
00198 
00199 static void insert_inout(AVFilterInOut **inouts, AVFilterInOut *element)
00200 {
00201     element->next = *inouts;
00202     *inouts = element;
00203 }
00204 
00205 static int link_filter_inouts(AVFilterContext *filt_ctx,
00206                               AVFilterInOut **curr_inputs,
00207                               AVFilterInOut **open_inputs, void *log_ctx)
00208 {
00209     int pad = filt_ctx->input_count, ret;
00210 
00211     while (pad--) {
00212         AVFilterInOut *p = *curr_inputs;
00213         if (!p) {
00214             av_log(log_ctx, AV_LOG_ERROR,
00215                    "Not enough inputs specified for the \"%s\" filter.\n",
00216                    filt_ctx->filter->name);
00217             return AVERROR(EINVAL);
00218         }
00219 
00220         *curr_inputs = (*curr_inputs)->next;
00221 
00222         if (p->filter_ctx) {
00223             if ((ret = link_filter(p->filter_ctx, p->pad_idx, filt_ctx, pad, log_ctx)) < 0)
00224                 return ret;
00225             av_free(p->name);
00226             av_free(p);
00227         } else {
00228             p->filter_ctx = filt_ctx;
00229             p->pad_idx = pad;
00230             insert_inout(open_inputs, p);
00231         }
00232     }
00233 
00234     if (*curr_inputs) {
00235         av_log(log_ctx, AV_LOG_ERROR,
00236                "Too many inputs specified for the \"%s\" filter.\n",
00237                filt_ctx->filter->name);
00238         return AVERROR(EINVAL);
00239     }
00240 
00241     pad = filt_ctx->output_count;
00242     while (pad--) {
00243         AVFilterInOut *currlinkn = av_mallocz(sizeof(AVFilterInOut));
00244         if (!currlinkn)
00245             return AVERROR(ENOMEM);
00246         currlinkn->filter_ctx  = filt_ctx;
00247         currlinkn->pad_idx = pad;
00248         insert_inout(curr_inputs, currlinkn);
00249     }
00250 
00251     return 0;
00252 }
00253 
00254 static int parse_inputs(const char **buf, AVFilterInOut **curr_inputs,
00255                         AVFilterInOut **open_outputs, void *log_ctx)
00256 {
00257     int pad = 0;
00258 
00259     while (**buf == '[') {
00260         char *name = parse_link_name(buf, log_ctx);
00261         AVFilterInOut *match;
00262 
00263         if (!name)
00264             return AVERROR(EINVAL);
00265 
00266         /* First check if the label is not in the open_outputs list */
00267         match = extract_inout(name, open_outputs);
00268 
00269         if (match) {
00270             av_free(name);
00271         } else {
00272             /* Not in the list, so add it as an input */
00273             if (!(match = av_mallocz(sizeof(AVFilterInOut))))
00274                 return AVERROR(ENOMEM);
00275             match->name    = name;
00276             match->pad_idx = pad;
00277         }
00278 
00279         insert_inout(curr_inputs, match);
00280 
00281         *buf += strspn(*buf, WHITESPACES);
00282         pad++;
00283     }
00284 
00285     return pad;
00286 }
00287 
00288 static int parse_outputs(const char **buf, AVFilterInOut **curr_inputs,
00289                          AVFilterInOut **open_inputs,
00290                          AVFilterInOut **open_outputs, void *log_ctx)
00291 {
00292     int ret, pad = 0;
00293 
00294     while (**buf == '[') {
00295         char *name = parse_link_name(buf, log_ctx);
00296         AVFilterInOut *match;
00297 
00298         AVFilterInOut *input = *curr_inputs;
00299         if (!input) {
00300             av_log(log_ctx, AV_LOG_ERROR,
00301                    "No output pad can be associated to link label '%s'.\n",
00302                    name);
00303             return AVERROR(EINVAL);
00304         }
00305         *curr_inputs = (*curr_inputs)->next;
00306 
00307         if (!name)
00308             return AVERROR(EINVAL);
00309 
00310         /* First check if the label is not in the open_inputs list */
00311         match = extract_inout(name, open_inputs);
00312 
00313         if (match) {
00314             if ((ret = link_filter(input->filter_ctx, input->pad_idx,
00315                                    match->filter_ctx, match->pad_idx, log_ctx)) < 0)
00316                 return ret;
00317             av_free(match->name);
00318             av_free(name);
00319             av_free(match);
00320             av_free(input);
00321         } else {
00322             /* Not in the list, so add the first input as a open_output */
00323             input->name = name;
00324             insert_inout(open_outputs, input);
00325         }
00326         *buf += strspn(*buf, WHITESPACES);
00327         pad++;
00328     }
00329 
00330     return pad;
00331 }
00332 
00333 int avfilter_graph_parse(AVFilterGraph *graph, const char *filters,
00334                          AVFilterInOut *open_inputs,
00335                          AVFilterInOut *open_outputs, void *log_ctx)
00336 {
00337     int index = 0, ret;
00338     char chr = 0;
00339 
00340     AVFilterInOut *curr_inputs = NULL;
00341 
00342     do {
00343         AVFilterContext *filter;
00344         const char *filterchain = filters;
00345         filters += strspn(filters, WHITESPACES);
00346 
00347         if ((ret = parse_inputs(&filters, &curr_inputs, &open_outputs, log_ctx)) < 0)
00348             goto fail;
00349 
00350         if ((ret = parse_filter(&filter, &filters, graph, index, log_ctx)) < 0)
00351             goto fail;
00352 
00353         if (filter->input_count == 1 && !curr_inputs && !index) {
00354             /* First input can be omitted if it is "[in]" */
00355             const char *tmp = "[in]";
00356             if ((ret = parse_inputs(&tmp, &curr_inputs, &open_outputs, log_ctx)) < 0)
00357                 goto fail;
00358         }
00359 
00360         if ((ret = link_filter_inouts(filter, &curr_inputs, &open_inputs, log_ctx)) < 0)
00361             goto fail;
00362 
00363         if ((ret = parse_outputs(&filters, &curr_inputs, &open_inputs, &open_outputs,
00364                                  log_ctx)) < 0)
00365             goto fail;
00366 
00367         filters += strspn(filters, WHITESPACES);
00368         chr = *filters++;
00369 
00370         if (chr == ';' && curr_inputs) {
00371             av_log(log_ctx, AV_LOG_ERROR,
00372                    "Invalid filterchain containing an unlabelled output pad: \"%s\"\n",
00373                    filterchain);
00374             ret = AVERROR(EINVAL);
00375             goto fail;
00376         }
00377         index++;
00378     } while (chr == ',' || chr == ';');
00379 
00380     if (chr) {
00381         av_log(log_ctx, AV_LOG_ERROR,
00382                "Unable to parse graph description substring: \"%s\"\n",
00383                filters - 1);
00384         ret = AVERROR(EINVAL);
00385         goto fail;
00386     }
00387 
00388     if (open_inputs && !strcmp(open_inputs->name, "out") && curr_inputs) {
00389         /* Last output can be omitted if it is "[out]" */
00390         const char *tmp = "[out]";
00391         if ((ret = parse_outputs(&tmp, &curr_inputs, &open_inputs, &open_outputs,
00392                                  log_ctx)) < 0)
00393             goto fail;
00394     }
00395 
00396     return 0;
00397 
00398  fail:
00399     for (; graph->filter_count > 0; graph->filter_count--)
00400         avfilter_free(graph->filters[graph->filter_count - 1]);
00401     av_freep(&graph->filters);
00402     free_inout(open_inputs);
00403     free_inout(open_outputs);
00404     free_inout(curr_inputs);
00405     return ret;
00406 }