2008-05-24 20:35:43 +00:00
|
|
|
/*
|
|
|
|
* filter graph parser
|
|
|
|
* copyright (c) 2008 Vitor Sessak
|
|
|
|
* copyright (c) 2007 Bobby Bingham
|
|
|
|
*
|
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "avfilter.h"
|
|
|
|
#include "avfiltergraph.h"
|
|
|
|
|
|
|
|
/**
|
|
|
|
* For use in av_log
|
|
|
|
*/
|
|
|
|
static const char *log_name(void *p)
|
|
|
|
{
|
|
|
|
return "Filter parser";
|
|
|
|
}
|
|
|
|
|
|
|
|
static const AVClass filter_parser_class = {
|
|
|
|
"Filter parser",
|
|
|
|
log_name
|
|
|
|
};
|
|
|
|
|
|
|
|
static const AVClass *log_ctx = &filter_parser_class;
|
|
|
|
|
2008-05-24 20:38:25 +00:00
|
|
|
static AVFilterContext *create_filter(AVFilterGraph *ctx, int index,
|
2008-05-24 20:39:45 +00:00
|
|
|
const char *name, const char *args)
|
2008-05-24 20:35:43 +00:00
|
|
|
{
|
|
|
|
AVFilterContext *filt;
|
|
|
|
|
|
|
|
AVFilter *filterdef;
|
2008-05-24 20:39:05 +00:00
|
|
|
char inst_name[30];
|
2008-05-24 20:35:43 +00:00
|
|
|
|
2008-05-24 20:39:05 +00:00
|
|
|
snprintf(inst_name, sizeof(inst_name), "Parsed filter %d", index);
|
2008-05-24 20:38:23 +00:00
|
|
|
|
|
|
|
if(!(filterdef = avfilter_get_by_name(name))) {
|
|
|
|
av_log(&log_ctx, AV_LOG_ERROR,
|
|
|
|
"no such filter: '%s'\n", name);
|
2008-05-24 20:38:25 +00:00
|
|
|
return NULL;
|
2008-05-24 20:38:23 +00:00
|
|
|
}
|
|
|
|
|
2008-05-24 20:39:05 +00:00
|
|
|
if(!(filt = avfilter_open(filterdef, inst_name))) {
|
2008-05-24 20:35:43 +00:00
|
|
|
av_log(&log_ctx, AV_LOG_ERROR,
|
|
|
|
"error creating filter '%s'\n", name);
|
2008-05-24 20:38:25 +00:00
|
|
|
return NULL;
|
2008-05-24 20:35:43 +00:00
|
|
|
}
|
2008-05-24 20:38:15 +00:00
|
|
|
|
2008-05-24 20:39:32 +00:00
|
|
|
if(avfilter_graph_add_filter(ctx, filt) < 0)
|
2008-05-24 20:38:25 +00:00
|
|
|
return NULL;
|
2008-05-24 20:38:15 +00:00
|
|
|
|
2008-05-24 20:35:43 +00:00
|
|
|
if(avfilter_init_filter(filt, args, NULL)) {
|
|
|
|
av_log(&log_ctx, AV_LOG_ERROR,
|
2008-05-24 20:38:23 +00:00
|
|
|
"error initializing filter '%s' with args '%s'\n", name, args);
|
2008-05-24 20:38:25 +00:00
|
|
|
return NULL;
|
2008-05-24 20:35:43 +00:00
|
|
|
}
|
|
|
|
|
2008-05-24 20:38:25 +00:00
|
|
|
return filt;
|
2008-05-24 20:35:43 +00:00
|
|
|
}
|
|
|
|
|
2008-05-24 20:38:25 +00:00
|
|
|
static int link_filter(AVFilterContext *src, int srcpad,
|
|
|
|
AVFilterContext *dst, int dstpad)
|
2008-05-24 20:35:43 +00:00
|
|
|
{
|
2008-05-24 20:38:25 +00:00
|
|
|
if(avfilter_link(src, srcpad, dst, dstpad)) {
|
|
|
|
av_log(&log_ctx, AV_LOG_ERROR,
|
|
|
|
"cannot create the link %s:%d -> %s:%d\n",
|
|
|
|
src->filter->name, srcpad, dst->filter->name, dstpad);
|
2008-05-24 20:35:43 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void consume_whitespace(const char **buf)
|
|
|
|
{
|
|
|
|
*buf += strspn(*buf, " \n\t");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Consumes a string from *buf.
|
|
|
|
* @return a copy of the consumed string, which should be free'd after use
|
|
|
|
*/
|
|
|
|
static char *consume_string(const char **buf)
|
|
|
|
{
|
2008-05-24 20:39:26 +00:00
|
|
|
char *out = av_malloc(strlen(*buf) + 1);
|
2008-05-24 20:39:12 +00:00
|
|
|
const char *in = *buf;
|
|
|
|
char *ret = out;
|
2008-05-24 20:35:43 +00:00
|
|
|
|
|
|
|
consume_whitespace(buf);
|
|
|
|
|
2008-05-24 20:39:12 +00:00
|
|
|
do{
|
|
|
|
char c = *in++;
|
|
|
|
switch (c) {
|
|
|
|
case '\\':
|
|
|
|
*out++= *in++;
|
2008-05-24 20:35:43 +00:00
|
|
|
break;
|
2008-05-24 20:39:12 +00:00
|
|
|
case '\'':
|
|
|
|
while(*in && *in != '\'')
|
|
|
|
*out++= *in++;
|
|
|
|
if(*in) in++;
|
|
|
|
break;
|
|
|
|
case 0:
|
2008-05-24 20:39:22 +00:00
|
|
|
case ']':
|
|
|
|
case '[':
|
2008-05-24 20:39:12 +00:00
|
|
|
case '=':
|
|
|
|
case ',':
|
|
|
|
*out++= 0;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
*out++= c;
|
|
|
|
}
|
|
|
|
} while(out[-1]);
|
2008-05-24 20:35:43 +00:00
|
|
|
|
2008-05-24 20:39:12 +00:00
|
|
|
*buf = in-1;
|
2008-05-24 20:35:43 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2008-05-24 20:39:29 +00:00
|
|
|
* Parse "[linkname]"
|
2008-05-24 20:35:43 +00:00
|
|
|
* @arg name a pointer (that need to be free'd after use) to the name between
|
|
|
|
* parenthesis
|
|
|
|
*/
|
|
|
|
static void parse_link_name(const char **buf, char **name)
|
|
|
|
{
|
2008-05-24 20:39:42 +00:00
|
|
|
const char *start = *buf;
|
2008-05-24 20:35:43 +00:00
|
|
|
(*buf)++;
|
|
|
|
|
|
|
|
*name = consume_string(buf);
|
|
|
|
|
2008-05-24 20:39:42 +00:00
|
|
|
if(!*name[0]) {
|
|
|
|
av_log(&log_ctx, AV_LOG_ERROR,
|
|
|
|
"Bad (empty?) label found in the following: \"%s\".\n", start);
|
2008-05-24 20:35:43 +00:00
|
|
|
goto fail;
|
2008-05-24 20:39:42 +00:00
|
|
|
}
|
2008-05-24 20:35:43 +00:00
|
|
|
|
2008-05-24 20:39:42 +00:00
|
|
|
if(*(*buf)++ != ']') {
|
|
|
|
av_log(&log_ctx, AV_LOG_ERROR,
|
|
|
|
"Mismatched '[' found in the following: \"%s\".\n", start);
|
2008-05-24 20:35:43 +00:00
|
|
|
goto fail;
|
2008-05-24 20:39:42 +00:00
|
|
|
}
|
2008-05-24 20:35:43 +00:00
|
|
|
|
|
|
|
return;
|
2008-05-24 20:39:42 +00:00
|
|
|
|
2008-05-24 20:35:43 +00:00
|
|
|
fail:
|
|
|
|
av_freep(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse "filter=params"
|
|
|
|
* @arg name a pointer (that need to be free'd after use) to the name of the
|
|
|
|
* filter
|
|
|
|
* @arg ars a pointer (that need to be free'd after use) to the args of the
|
|
|
|
* filter
|
|
|
|
*/
|
2008-05-24 20:38:25 +00:00
|
|
|
static AVFilterContext *parse_filter(const char **buf, AVFilterGraph *graph, int index)
|
2008-05-24 20:35:43 +00:00
|
|
|
{
|
|
|
|
char *name, *opts;
|
|
|
|
name = consume_string(buf);
|
|
|
|
|
2008-05-24 20:39:32 +00:00
|
|
|
if(**buf == '=') {
|
2008-05-24 20:35:43 +00:00
|
|
|
(*buf)++;
|
|
|
|
opts = consume_string(buf);
|
|
|
|
} else {
|
|
|
|
opts = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return create_filter(graph, index, name, opts);
|
|
|
|
}
|
|
|
|
|
|
|
|
enum LinkType {
|
|
|
|
LinkTypeIn,
|
|
|
|
LinkTypeOut,
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A linked-list of the inputs/outputs of the filter chain.
|
|
|
|
*/
|
|
|
|
typedef struct AVFilterInOut {
|
|
|
|
enum LinkType type;
|
|
|
|
char *name;
|
2008-05-24 20:39:16 +00:00
|
|
|
AVFilterContext *filter;
|
2008-05-24 20:35:43 +00:00
|
|
|
int pad_idx;
|
|
|
|
|
|
|
|
struct AVFilterInOut *next;
|
|
|
|
} AVFilterInOut;
|
|
|
|
|
|
|
|
static void free_inout(AVFilterInOut *head)
|
|
|
|
{
|
|
|
|
while (head) {
|
2008-05-24 20:39:34 +00:00
|
|
|
AVFilterInOut *next = head->next;
|
2008-05-24 20:35:43 +00:00
|
|
|
av_free(head);
|
|
|
|
head = next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2008-05-24 20:39:29 +00:00
|
|
|
* Parse "[a1][link2] ... [etc]"
|
2008-05-24 20:35:43 +00:00
|
|
|
*/
|
2008-05-24 20:39:39 +00:00
|
|
|
static int parse_inouts(const char **buf, AVFilterInOut **inout, int pad,
|
2008-05-24 20:39:16 +00:00
|
|
|
enum LinkType type, AVFilterContext *filter)
|
2008-05-24 20:35:43 +00:00
|
|
|
{
|
2008-05-24 20:39:22 +00:00
|
|
|
while (**buf == '[') {
|
2008-05-24 20:35:43 +00:00
|
|
|
AVFilterInOut *inoutn = av_malloc(sizeof(AVFilterInOut));
|
|
|
|
parse_link_name(buf, &inoutn->name);
|
2008-05-24 20:39:42 +00:00
|
|
|
|
|
|
|
if (!inoutn->name) {
|
|
|
|
av_free(inoutn);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-05-24 20:39:48 +00:00
|
|
|
inoutn->type = type;
|
|
|
|
inoutn->filter = filter;
|
2008-05-24 20:35:43 +00:00
|
|
|
inoutn->pad_idx = pad++;
|
2008-05-24 20:39:48 +00:00
|
|
|
inoutn->next = *inout;
|
2008-05-24 20:35:43 +00:00
|
|
|
*inout = inoutn;
|
|
|
|
}
|
|
|
|
return pad;
|
|
|
|
}
|
|
|
|
|
2008-05-24 20:38:25 +00:00
|
|
|
static const char *skip_inouts(const char *buf)
|
|
|
|
{
|
2008-05-24 20:39:36 +00:00
|
|
|
while (*buf == '[')
|
|
|
|
buf += strcspn(buf, "]") + 1;
|
|
|
|
|
2008-05-24 20:38:25 +00:00
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-05-24 20:35:43 +00:00
|
|
|
/**
|
|
|
|
* Parse a string describing a filter graph.
|
|
|
|
*/
|
2008-05-24 20:39:19 +00:00
|
|
|
int avfilter_parse_graph(AVFilterGraph *graph, const char *filters,
|
|
|
|
AVFilterContext *in, int inpad,
|
|
|
|
AVFilterContext *out, int outpad)
|
2008-05-24 20:35:43 +00:00
|
|
|
{
|
|
|
|
AVFilterInOut *inout=NULL;
|
|
|
|
AVFilterInOut *head=NULL;
|
|
|
|
|
|
|
|
int index = 0;
|
|
|
|
char chr = 0;
|
|
|
|
int pad = 0;
|
|
|
|
int has_out = 0;
|
|
|
|
|
2008-05-24 20:38:25 +00:00
|
|
|
AVFilterContext *last_filt = NULL;
|
2008-05-24 20:35:43 +00:00
|
|
|
|
|
|
|
consume_whitespace(&filters);
|
|
|
|
|
|
|
|
do {
|
2008-05-24 20:38:25 +00:00
|
|
|
AVFilterContext *filter;
|
2008-05-24 20:35:43 +00:00
|
|
|
int oldpad = pad;
|
2008-05-24 20:38:25 +00:00
|
|
|
const char *inouts = filters;
|
2008-05-24 20:35:43 +00:00
|
|
|
|
2008-05-24 20:38:25 +00:00
|
|
|
// We need to parse the inputs of the filter after we create it, so
|
|
|
|
// skip it by now
|
|
|
|
filters = skip_inouts(filters);
|
2008-05-24 20:35:43 +00:00
|
|
|
|
2008-05-24 20:39:32 +00:00
|
|
|
if(!(filter = parse_filter(&filters, graph, index)))
|
2008-05-24 20:35:43 +00:00
|
|
|
goto fail;
|
|
|
|
|
2008-05-24 20:38:25 +00:00
|
|
|
pad = parse_inouts(&inouts, &inout, chr == ',', LinkTypeIn, filter);
|
|
|
|
|
2008-05-24 20:39:42 +00:00
|
|
|
if(pad < 0)
|
|
|
|
goto fail;
|
|
|
|
|
2008-05-24 20:35:43 +00:00
|
|
|
// If the first filter has an input and none was given, it is
|
|
|
|
// implicitly the input of the whole graph.
|
2008-05-24 20:39:32 +00:00
|
|
|
if(pad == 0 && filter->input_count == 1) {
|
2008-05-24 20:39:03 +00:00
|
|
|
if(link_filter(in, inpad, filter, 0))
|
2008-05-24 20:35:43 +00:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(chr == ',') {
|
2008-05-24 20:39:32 +00:00
|
|
|
if(link_filter(last_filt, oldpad, filter, 0) < 0)
|
2008-05-24 20:35:43 +00:00
|
|
|
goto fail;
|
|
|
|
}
|
2008-05-24 20:39:14 +00:00
|
|
|
|
2008-05-24 20:38:25 +00:00
|
|
|
pad = parse_inouts(&filters, &inout, 0, LinkTypeOut, filter);
|
2008-05-24 20:35:43 +00:00
|
|
|
chr = *filters++;
|
|
|
|
index++;
|
2008-05-24 20:38:25 +00:00
|
|
|
last_filt = filter;
|
2008-05-24 20:35:43 +00:00
|
|
|
} while (chr == ',' || chr == ';');
|
|
|
|
|
|
|
|
head = inout;
|
|
|
|
for (; inout != NULL; inout = inout->next) {
|
2008-05-24 20:39:32 +00:00
|
|
|
if(inout->filter == NULL)
|
2008-05-24 20:35:43 +00:00
|
|
|
continue; // Already processed
|
|
|
|
|
2008-05-24 20:39:32 +00:00
|
|
|
if(!strcmp(inout->name, "in")) {
|
2008-05-24 20:39:16 +00:00
|
|
|
if(link_filter(in, inpad, inout->filter, inout->pad_idx))
|
2008-05-24 20:35:43 +00:00
|
|
|
goto fail;
|
2008-05-24 20:39:03 +00:00
|
|
|
|
2008-05-24 20:39:32 +00:00
|
|
|
} else if(!strcmp(inout->name, "out")) {
|
2008-05-24 20:35:43 +00:00
|
|
|
has_out = 1;
|
|
|
|
|
2008-05-24 20:39:16 +00:00
|
|
|
if(link_filter(inout->filter, inout->pad_idx, out, outpad))
|
2008-05-24 20:35:43 +00:00
|
|
|
goto fail;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
AVFilterInOut *p, *src, *dst;
|
|
|
|
for (p = inout->next;
|
|
|
|
p && strcmp(p->name,inout->name); p = p->next);
|
|
|
|
|
2008-05-24 20:39:32 +00:00
|
|
|
if(!p) {
|
2008-05-24 20:35:43 +00:00
|
|
|
av_log(&log_ctx, AV_LOG_ERROR, "Unmatched link: %s.\n",
|
|
|
|
inout->name);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2008-05-24 20:39:32 +00:00
|
|
|
if(p->type == LinkTypeIn && inout->type == LinkTypeOut) {
|
2008-05-24 20:35:43 +00:00
|
|
|
src = inout;
|
|
|
|
dst = p;
|
2008-05-24 20:39:32 +00:00
|
|
|
} else if(p->type == LinkTypeOut && inout->type == LinkTypeIn) {
|
2008-05-24 20:35:43 +00:00
|
|
|
src = p;
|
|
|
|
dst = inout;
|
|
|
|
} else {
|
|
|
|
av_log(&log_ctx, AV_LOG_ERROR, "Two links named '%s' are either both input or both output\n",
|
|
|
|
inout->name);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2008-05-24 20:39:32 +00:00
|
|
|
if(link_filter(src->filter, src->pad_idx, dst->filter, dst->pad_idx) < 0)
|
2008-05-24 20:35:43 +00:00
|
|
|
goto fail;
|
|
|
|
|
2008-05-24 20:39:16 +00:00
|
|
|
src->filter = NULL;
|
|
|
|
dst->filter = NULL;
|
2008-05-24 20:35:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
free_inout(head);
|
|
|
|
|
2008-05-24 20:39:32 +00:00
|
|
|
if(!has_out) {
|
2008-05-24 20:39:03 +00:00
|
|
|
if(link_filter(last_filt, pad, out, outpad))
|
2008-05-24 20:35:43 +00:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
free_inout(head);
|
|
|
|
avfilter_destroy_graph(graph);
|
|
|
|
return -1;
|
|
|
|
}
|