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 FFmpeg.
00007  *
00008  * FFmpeg 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  * FFmpeg 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 FFmpeg; 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_%s_%d", filt_name, index);
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         snprintf(tmp_args, sizeof(tmp_args), "%s:%s",
00126                  args, ctx->scale_sws_opts);
00127         args = tmp_args;
00128     }
00129 
00130     if ((ret = avfilter_init_filter(*filt_ctx, args, NULL)) < 0) {
00131         av_log(log_ctx, AV_LOG_ERROR,
00132                "Error initializing filter '%s' with args '%s'\n", filt_name, args);
00133         return ret;
00134     }
00135 
00136     return 0;
00137 }
00138 
00155 static int parse_filter(AVFilterContext **filt_ctx, const char **buf, AVFilterGraph *graph,
00156                         int index, void *log_ctx)
00157 {
00158     char *opts = NULL;
00159     char *name = av_get_token(buf, "=,;[\n");
00160     int ret;
00161 
00162     if (**buf == '=') {
00163         (*buf)++;
00164         opts = av_get_token(buf, "[],;\n");
00165     }
00166 
00167     ret = create_filter(filt_ctx, graph, index, name, opts, log_ctx);
00168     av_free(name);
00169     av_free(opts);
00170     return ret;
00171 }
00172 
00173 AVFilterInOut *avfilter_inout_alloc(void)
00174 {
00175     return av_mallocz(sizeof(AVFilterInOut));
00176 }
00177 
00178 void avfilter_inout_free(AVFilterInOut **inout)
00179 {
00180     while (*inout) {
00181         AVFilterInOut *next = (*inout)->next;
00182         av_freep(&(*inout)->name);
00183         av_freep(inout);
00184         *inout = next;
00185     }
00186 }
00187 
00188 static AVFilterInOut *extract_inout(const char *label, AVFilterInOut **links)
00189 {
00190     AVFilterInOut *ret;
00191 
00192     while (*links && strcmp((*links)->name, label))
00193         links = &((*links)->next);
00194 
00195     ret = *links;
00196 
00197     if (ret)
00198         *links = ret->next;
00199 
00200     return ret;
00201 }
00202 
00203 static void insert_inout(AVFilterInOut **inouts, AVFilterInOut *element)
00204 {
00205     element->next = *inouts;
00206     *inouts = element;
00207 }
00208 
00209 static int link_filter_inouts(AVFilterContext *filt_ctx,
00210                               AVFilterInOut **curr_inputs,
00211                               AVFilterInOut **open_inputs, void *log_ctx)
00212 {
00213     int pad = filt_ctx->input_count, ret;
00214 
00215     while (pad--) {
00216         AVFilterInOut *p = *curr_inputs;
00217         if (!p) {
00218             av_log(log_ctx, AV_LOG_ERROR,
00219                    "Not enough inputs specified for the \"%s\" filter.\n",
00220                    filt_ctx->filter->name);
00221             return AVERROR(EINVAL);
00222         }
00223 
00224         *curr_inputs = (*curr_inputs)->next;
00225 
00226         if (p->filter_ctx) {
00227             if ((ret = link_filter(p->filter_ctx, p->pad_idx, filt_ctx, pad, log_ctx)) < 0)
00228                 return ret;
00229             av_free(p->name);
00230             av_free(p);
00231         } else {
00232             p->filter_ctx = filt_ctx;
00233             p->pad_idx = pad;
00234             insert_inout(open_inputs, p);
00235         }
00236     }
00237 
00238     if (*curr_inputs) {
00239         av_log(log_ctx, AV_LOG_ERROR,
00240                "Too many inputs specified for the \"%s\" filter.\n",
00241                filt_ctx->filter->name);
00242         return AVERROR(EINVAL);
00243     }
00244 
00245     pad = filt_ctx->output_count;
00246     while (pad--) {
00247         AVFilterInOut *currlinkn = av_mallocz(sizeof(AVFilterInOut));
00248         if (!currlinkn)
00249             return AVERROR(ENOMEM);
00250         currlinkn->filter_ctx  = filt_ctx;
00251         currlinkn->pad_idx = pad;
00252         insert_inout(curr_inputs, currlinkn);
00253     }
00254 
00255     return 0;
00256 }
00257 
00258 static int parse_inputs(const char **buf, AVFilterInOut **curr_inputs,
00259                         AVFilterInOut **open_outputs, void *log_ctx)
00260 {
00261     int pad = 0;
00262 
00263     while (**buf == '[') {
00264         char *name = parse_link_name(buf, log_ctx);
00265         AVFilterInOut *match;
00266 
00267         if (!name)
00268             return AVERROR(EINVAL);
00269 
00270         /* First check if the label is not in the open_outputs list */
00271         match = extract_inout(name, open_outputs);
00272 
00273         if (match) {
00274             av_free(name);
00275         } else {
00276             /* Not in the list, so add it as an input */
00277             if (!(match = av_mallocz(sizeof(AVFilterInOut))))
00278                 return AVERROR(ENOMEM);
00279             match->name    = name;
00280             match->pad_idx = pad;
00281         }
00282 
00283         insert_inout(curr_inputs, match);
00284 
00285         *buf += strspn(*buf, WHITESPACES);
00286         pad++;
00287     }
00288 
00289     return pad;
00290 }
00291 
00292 static int parse_outputs(const char **buf, AVFilterInOut **curr_inputs,
00293                          AVFilterInOut **open_inputs,
00294                          AVFilterInOut **open_outputs, void *log_ctx)
00295 {
00296     int ret, pad = 0;
00297 
00298     while (**buf == '[') {
00299         char *name = parse_link_name(buf, log_ctx);
00300         AVFilterInOut *match;
00301 
00302         AVFilterInOut *input = *curr_inputs;
00303         if (!input) {
00304             av_log(log_ctx, AV_LOG_ERROR,
00305                    "No output pad can be associated to link label '%s'.\n",
00306                    name);
00307             return AVERROR(EINVAL);
00308         }
00309         *curr_inputs = (*curr_inputs)->next;
00310 
00311         if (!name)
00312             return AVERROR(EINVAL);
00313 
00314         /* First check if the label is not in the open_inputs list */
00315         match = extract_inout(name, open_inputs);
00316 
00317         if (match) {
00318             if ((ret = link_filter(input->filter_ctx, input->pad_idx,
00319                                    match->filter_ctx, match->pad_idx, log_ctx)) < 0)
00320                 return ret;
00321             av_free(match->name);
00322             av_free(name);
00323             av_free(match);
00324             av_free(input);
00325         } else {
00326             /* Not in the list, so add the first input as a open_output */
00327             input->name = name;
00328             insert_inout(open_outputs, input);
00329         }
00330         *buf += strspn(*buf, WHITESPACES);
00331         pad++;
00332     }
00333 
00334     return pad;
00335 }
00336 
00337 int avfilter_graph_parse(AVFilterGraph *graph, const char *filters,
00338                          AVFilterInOut **open_inputs_ptr, AVFilterInOut **open_outputs_ptr,
00339                          void *log_ctx)
00340 {
00341     int index = 0, ret = 0;
00342     char chr = 0;
00343 
00344     AVFilterInOut *curr_inputs = NULL;
00345     AVFilterInOut *open_inputs  = open_inputs_ptr  ? *open_inputs_ptr  : NULL;
00346     AVFilterInOut *open_outputs = open_outputs_ptr ? *open_outputs_ptr : NULL;
00347 
00348     do {
00349         AVFilterContext *filter;
00350         const char *filterchain = filters;
00351         filters += strspn(filters, WHITESPACES);
00352 
00353         if ((ret = parse_inputs(&filters, &curr_inputs, &open_outputs, log_ctx)) < 0)
00354             goto end;
00355 
00356         if ((ret = parse_filter(&filter, &filters, graph, index, log_ctx)) < 0)
00357             goto end;
00358 
00359         if (filter->input_count == 1 && !curr_inputs && !index) {
00360             /* First input pad, assume it is "[in]" if not specified */
00361             const char *tmp = "[in]";
00362             if ((ret = parse_inputs(&tmp, &curr_inputs, &open_outputs, log_ctx)) < 0)
00363                 goto end;
00364         }
00365 
00366         if ((ret = link_filter_inouts(filter, &curr_inputs, &open_inputs, log_ctx)) < 0)
00367             goto end;
00368 
00369         if ((ret = parse_outputs(&filters, &curr_inputs, &open_inputs, &open_outputs,
00370                                  log_ctx)) < 0)
00371             goto end;
00372 
00373         filters += strspn(filters, WHITESPACES);
00374         chr = *filters++;
00375 
00376         if (chr == ';' && curr_inputs) {
00377             av_log(log_ctx, AV_LOG_ERROR,
00378                    "Invalid filterchain containing an unlabelled output pad: \"%s\"\n",
00379                    filterchain);
00380             ret = AVERROR(EINVAL);
00381             goto end;
00382         }
00383         index++;
00384     } while (chr == ',' || chr == ';');
00385 
00386     if (chr) {
00387         av_log(log_ctx, AV_LOG_ERROR,
00388                "Unable to parse graph description substring: \"%s\"\n",
00389                filters - 1);
00390         ret = AVERROR(EINVAL);
00391         goto end;
00392     }
00393 
00394     if (curr_inputs) {
00395         /* Last output pad, assume it is "[out]" if not specified */
00396         const char *tmp = "[out]";
00397         if ((ret = parse_outputs(&tmp, &curr_inputs, &open_inputs, &open_outputs,
00398                                  log_ctx)) < 0)
00399             goto end;
00400     }
00401 
00402 end:
00403     /* clear open_in/outputs only if not passed as parameters */
00404     if (open_inputs_ptr) *open_inputs_ptr = open_inputs;
00405     else avfilter_inout_free(&open_inputs);
00406     if (open_outputs_ptr) *open_outputs_ptr = open_outputs;
00407     else avfilter_inout_free(&open_outputs);
00408     avfilter_inout_free(&curr_inputs);
00409 
00410     if (ret < 0) {
00411         for (; graph->filter_count > 0; graph->filter_count--)
00412             avfilter_free(graph->filters[graph->filter_count - 1]);
00413         av_freep(&graph->filters);
00414     }
00415     return ret;
00416 }