• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

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         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 static void free_inout(AVFilterInOut *head)
00174 {
00175     while (head) {
00176         AVFilterInOut *next = head->next;
00177         av_free(head->name);
00178         av_free(head);
00179         head = next;
00180     }
00181 }
00182 
00183 static AVFilterInOut *extract_inout(const char *label, AVFilterInOut **links)
00184 {
00185     AVFilterInOut *ret;
00186 
00187     while (*links && strcmp((*links)->name, label))
00188         links = &((*links)->next);
00189 
00190     ret = *links;
00191 
00192     if (ret)
00193         *links = ret->next;
00194 
00195     return ret;
00196 }
00197 
00198 static void insert_inout(AVFilterInOut **inouts, AVFilterInOut *element)
00199 {
00200     element->next = *inouts;
00201     *inouts = element;
00202 }
00203 
00204 static int link_filter_inouts(AVFilterContext *filt_ctx,
00205                               AVFilterInOut **curr_inputs,
00206                               AVFilterInOut **open_inputs, void *log_ctx)
00207 {
00208     int pad = filt_ctx->input_count, ret;
00209 
00210     while (pad--) {
00211         AVFilterInOut *p = *curr_inputs;
00212         if (!p) {
00213             av_log(log_ctx, AV_LOG_ERROR,
00214                    "Not enough inputs specified for the \"%s\" filter.\n",
00215                    filt_ctx->filter->name);
00216             return AVERROR(EINVAL);
00217         }
00218 
00219         *curr_inputs = (*curr_inputs)->next;
00220 
00221         if (p->filter_ctx) {
00222             if ((ret = link_filter(p->filter_ctx, p->pad_idx, filt_ctx, pad, log_ctx)) < 0)
00223                 return ret;
00224             av_free(p->name);
00225             av_free(p);
00226         } else {
00227             p->filter_ctx = filt_ctx;
00228             p->pad_idx = pad;
00229             insert_inout(open_inputs, p);
00230         }
00231     }
00232 
00233     if (*curr_inputs) {
00234         av_log(log_ctx, AV_LOG_ERROR,
00235                "Too many inputs specified for the \"%s\" filter.\n",
00236                filt_ctx->filter->name);
00237         return AVERROR(EINVAL);
00238     }
00239 
00240     pad = filt_ctx->output_count;
00241     while (pad--) {
00242         AVFilterInOut *currlinkn = av_mallocz(sizeof(AVFilterInOut));
00243         if (!currlinkn)
00244             return AVERROR(ENOMEM);
00245         currlinkn->filter_ctx  = filt_ctx;
00246         currlinkn->pad_idx = pad;
00247         insert_inout(curr_inputs, currlinkn);
00248     }
00249 
00250     return 0;
00251 }
00252 
00253 static int parse_inputs(const char **buf, AVFilterInOut **curr_inputs,
00254                         AVFilterInOut **open_outputs, void *log_ctx)
00255 {
00256     int pad = 0;
00257 
00258     while (**buf == '[') {
00259         char *name = parse_link_name(buf, log_ctx);
00260         AVFilterInOut *match;
00261 
00262         if (!name)
00263             return AVERROR(EINVAL);
00264 
00265         /* First check if the label is not in the open_outputs list */
00266         match = extract_inout(name, open_outputs);
00267 
00268         if (match) {
00269             av_free(name);
00270         } else {
00271             /* Not in the list, so add it as an input */
00272             if (!(match = av_mallocz(sizeof(AVFilterInOut))))
00273                 return AVERROR(ENOMEM);
00274             match->name    = name;
00275             match->pad_idx = pad;
00276         }
00277 
00278         insert_inout(curr_inputs, match);
00279 
00280         *buf += strspn(*buf, WHITESPACES);
00281         pad++;
00282     }
00283 
00284     return pad;
00285 }
00286 
00287 static int parse_outputs(const char **buf, AVFilterInOut **curr_inputs,
00288                          AVFilterInOut **open_inputs,
00289                          AVFilterInOut **open_outputs, void *log_ctx)
00290 {
00291     int ret, pad = 0;
00292 
00293     while (**buf == '[') {
00294         char *name = parse_link_name(buf, log_ctx);
00295         AVFilterInOut *match;
00296 
00297         AVFilterInOut *input = *curr_inputs;
00298         if (!input) {
00299             av_log(log_ctx, AV_LOG_ERROR,
00300                    "No output pad can be associated to link label '%s'.\n",
00301                    name);
00302             return AVERROR(EINVAL);
00303         }
00304         *curr_inputs = (*curr_inputs)->next;
00305 
00306         if (!name)
00307             return AVERROR(EINVAL);
00308 
00309         /* First check if the label is not in the open_inputs list */
00310         match = extract_inout(name, open_inputs);
00311 
00312         if (match) {
00313             if ((ret = link_filter(input->filter_ctx, input->pad_idx,
00314                                    match->filter_ctx, match->pad_idx, log_ctx)) < 0)
00315                 return ret;
00316             av_free(match->name);
00317             av_free(name);
00318             av_free(match);
00319             av_free(input);
00320         } else {
00321             /* Not in the list, so add the first input as a open_output */
00322             input->name = name;
00323             insert_inout(open_outputs, input);
00324         }
00325         *buf += strspn(*buf, WHITESPACES);
00326         pad++;
00327     }
00328 
00329     return pad;
00330 }
00331 
00332 int avfilter_graph_parse(AVFilterGraph *graph, const char *filters,
00333                          AVFilterInOut *open_inputs,
00334                          AVFilterInOut *open_outputs, void *log_ctx)
00335 {
00336     int index = 0, ret;
00337     char chr = 0;
00338 
00339     AVFilterInOut *curr_inputs = NULL;
00340 
00341     do {
00342         AVFilterContext *filter;
00343         const char *filterchain = filters;
00344         filters += strspn(filters, WHITESPACES);
00345 
00346         if ((ret = parse_inputs(&filters, &curr_inputs, &open_outputs, log_ctx)) < 0)
00347             goto fail;
00348 
00349         if ((ret = parse_filter(&filter, &filters, graph, index, log_ctx)) < 0)
00350             goto fail;
00351 
00352         if (filter->input_count == 1 && !curr_inputs && !index) {
00353             /* First input can be omitted if it is "[in]" */
00354             const char *tmp = "[in]";
00355             if ((ret = parse_inputs(&tmp, &curr_inputs, &open_outputs, log_ctx)) < 0)
00356                 goto fail;
00357         }
00358 
00359         if ((ret = link_filter_inouts(filter, &curr_inputs, &open_inputs, log_ctx)) < 0)
00360             goto fail;
00361 
00362         if ((ret = parse_outputs(&filters, &curr_inputs, &open_inputs, &open_outputs,
00363                                  log_ctx)) < 0)
00364             goto fail;
00365 
00366         filters += strspn(filters, WHITESPACES);
00367         chr = *filters++;
00368 
00369         if (chr == ';' && curr_inputs) {
00370             av_log(log_ctx, AV_LOG_ERROR,
00371                    "Invalid filterchain containing an unlabelled output pad: \"%s\"\n",
00372                    filterchain);
00373             ret = AVERROR(EINVAL);
00374             goto fail;
00375         }
00376         index++;
00377     } while (chr == ',' || chr == ';');
00378 
00379     if (chr) {
00380         av_log(log_ctx, AV_LOG_ERROR,
00381                "Unable to parse graph description substring: \"%s\"\n",
00382                filters - 1);
00383         ret = AVERROR(EINVAL);
00384         goto fail;
00385     }
00386 
00387     if (open_inputs && !strcmp(open_inputs->name, "out") && curr_inputs) {
00388         /* Last output can be omitted if it is "[out]" */
00389         const char *tmp = "[out]";
00390         if ((ret = parse_outputs(&tmp, &curr_inputs, &open_inputs, &open_outputs,
00391                                  log_ctx)) < 0)
00392             goto fail;
00393     }
00394 
00395     return 0;
00396 
00397  fail:
00398     for (; graph->filter_count > 0; graph->filter_count--)
00399         avfilter_free(graph->filters[graph->filter_count - 1]);
00400     av_freep(&graph->filters);
00401     free_inout(open_inputs);
00402     free_inout(open_outputs);
00403     free_inout(curr_inputs);
00404     return ret;
00405 }
Generated on Sat Mar 17 2012 12:57:52 for Libav by doxygen 1.7.1