2008-05-24 23:35:43 +03: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"
|
|
|
|
|
2008-05-24 23:38:25 +03:00
|
|
|
static AVFilterContext *create_filter(AVFilterGraph *ctx, int index,
|
2008-05-24 23:40:22 +03:00
|
|
|
const char *name, const char *args,
|
|
|
|
AVClass *log_ctx)
|
2008-05-24 23:35:43 +03:00
|
|
|
{
|
|
|
|
AVFilterContext *filt;
|
|
|
|
|
|
|
|
AVFilter *filterdef;
|
2008-05-24 23:39:05 +03:00
|
|
|
char inst_name[30];
|
2008-05-24 23:35:43 +03:00
|
|
|
|
2008-05-24 23:39:05 +03:00
|
|
|
snprintf(inst_name, sizeof(inst_name), "Parsed filter %d", index);
|
2008-05-24 23:38:23 +03:00
|
|
|
|
|
|
|
if(!(filterdef = avfilter_get_by_name(name))) {
|
2008-05-24 23:40:32 +03:00
|
|
|
av_log(log_ctx, AV_LOG_ERROR,
|
2008-05-24 23:38:23 +03:00
|
|
|
"no such filter: '%s'\n", name);
|
2008-05-24 23:38:25 +03:00
|
|
|
return NULL;
|
2008-05-24 23:38:23 +03:00
|
|
|
}
|
|
|
|
|
2008-05-24 23:39:05 +03:00
|
|
|
if(!(filt = avfilter_open(filterdef, inst_name))) {
|
2008-05-24 23:40:22 +03:00
|
|
|
av_log(log_ctx, AV_LOG_ERROR,
|
2008-05-24 23:35:43 +03:00
|
|
|
"error creating filter '%s'\n", name);
|
2008-05-24 23:38:25 +03:00
|
|
|
return NULL;
|
2008-05-24 23:35:43 +03:00
|
|
|
}
|
2008-05-24 23:38:15 +03:00
|
|
|
|
2008-05-24 23:39:32 +03:00
|
|
|
if(avfilter_graph_add_filter(ctx, filt) < 0)
|
2008-05-24 23:38:25 +03:00
|
|
|
return NULL;
|
2008-05-24 23:38:15 +03:00
|
|
|
|
2008-05-24 23:35:43 +03:00
|
|
|
if(avfilter_init_filter(filt, args, NULL)) {
|
2008-05-24 23:40:22 +03:00
|
|
|
av_log(log_ctx, AV_LOG_ERROR,
|
2008-05-24 23:38:23 +03:00
|
|
|
"error initializing filter '%s' with args '%s'\n", name, args);
|
2008-05-24 23:38:25 +03:00
|
|
|
return NULL;
|
2008-05-24 23:35:43 +03:00
|
|
|
}
|
|
|
|
|
2008-05-24 23:38:25 +03:00
|
|
|
return filt;
|
2008-05-24 23:35:43 +03:00
|
|
|
}
|
|
|
|
|
2008-05-24 23:38:25 +03:00
|
|
|
static int link_filter(AVFilterContext *src, int srcpad,
|
2008-05-24 23:40:22 +03:00
|
|
|
AVFilterContext *dst, int dstpad,
|
|
|
|
AVClass *log_ctx)
|
2008-05-24 23:35:43 +03:00
|
|
|
{
|
2008-05-24 23:38:25 +03:00
|
|
|
if(avfilter_link(src, srcpad, dst, dstpad)) {
|
2008-05-24 23:40:22 +03:00
|
|
|
av_log(log_ctx, AV_LOG_ERROR,
|
2008-05-24 23:38:25 +03:00
|
|
|
"cannot create the link %s:%d -> %s:%d\n",
|
|
|
|
src->filter->name, srcpad, dst->filter->name, dstpad);
|
2008-05-24 23:35:43 +03: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 23:39:26 +03:00
|
|
|
char *out = av_malloc(strlen(*buf) + 1);
|
2008-05-24 23:39:12 +03:00
|
|
|
char *ret = out;
|
2008-05-24 23:35:43 +03:00
|
|
|
|
|
|
|
consume_whitespace(buf);
|
|
|
|
|
2008-05-24 23:39:12 +03:00
|
|
|
do{
|
2008-05-24 23:39:51 +03:00
|
|
|
char c = *(*buf)++;
|
2008-05-24 23:39:12 +03:00
|
|
|
switch (c) {
|
|
|
|
case '\\':
|
2008-05-24 23:39:51 +03:00
|
|
|
*out++= *(*buf)++;
|
2008-05-24 23:35:43 +03:00
|
|
|
break;
|
2008-05-24 23:39:12 +03:00
|
|
|
case '\'':
|
2008-05-24 23:39:51 +03:00
|
|
|
while(**buf && **buf != '\'')
|
|
|
|
*out++= *(*buf)++;
|
|
|
|
if(**buf) (*buf)++;
|
2008-05-24 23:39:12 +03:00
|
|
|
break;
|
|
|
|
case 0:
|
2008-05-24 23:39:22 +03:00
|
|
|
case ']':
|
|
|
|
case '[':
|
2008-05-24 23:39:12 +03:00
|
|
|
case '=':
|
|
|
|
case ',':
|
2008-05-24 23:40:35 +03:00
|
|
|
case ';':
|
2008-05-24 23:39:55 +03:00
|
|
|
case ' ':
|
|
|
|
case '\n':
|
2008-05-24 23:39:12 +03:00
|
|
|
*out++= 0;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
*out++= c;
|
|
|
|
}
|
|
|
|
} while(out[-1]);
|
2008-05-24 23:35:43 +03:00
|
|
|
|
2008-05-24 23:39:51 +03:00
|
|
|
(*buf)--;
|
2008-05-24 23:39:55 +03:00
|
|
|
consume_whitespace(buf);
|
|
|
|
|
2008-05-24 23:35:43 +03:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2008-05-24 23:39:29 +03:00
|
|
|
* Parse "[linkname]"
|
2008-05-24 23:35:43 +03:00
|
|
|
* @arg name a pointer (that need to be free'd after use) to the name between
|
|
|
|
* parenthesis
|
|
|
|
*/
|
2008-05-24 23:40:22 +03:00
|
|
|
static void parse_link_name(const char **buf, char **name, AVClass *log_ctx)
|
2008-05-24 23:35:43 +03:00
|
|
|
{
|
2008-05-24 23:39:42 +03:00
|
|
|
const char *start = *buf;
|
2008-05-24 23:35:43 +03:00
|
|
|
(*buf)++;
|
|
|
|
|
|
|
|
*name = consume_string(buf);
|
|
|
|
|
2008-05-24 23:39:42 +03:00
|
|
|
if(!*name[0]) {
|
2008-05-24 23:40:22 +03:00
|
|
|
av_log(log_ctx, AV_LOG_ERROR,
|
2008-05-24 23:39:42 +03:00
|
|
|
"Bad (empty?) label found in the following: \"%s\".\n", start);
|
2008-05-24 23:35:43 +03:00
|
|
|
goto fail;
|
2008-05-24 23:39:42 +03:00
|
|
|
}
|
2008-05-24 23:35:43 +03:00
|
|
|
|
2008-05-24 23:39:42 +03:00
|
|
|
if(*(*buf)++ != ']') {
|
2008-05-24 23:40:22 +03:00
|
|
|
av_log(log_ctx, AV_LOG_ERROR,
|
2008-05-24 23:39:42 +03:00
|
|
|
"Mismatched '[' found in the following: \"%s\".\n", start);
|
2008-05-24 23:40:10 +03:00
|
|
|
fail:
|
|
|
|
av_freep(name);
|
2008-05-24 23:39:42 +03:00
|
|
|
}
|
2008-05-24 23:35:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 23:40:22 +03:00
|
|
|
static AVFilterContext *parse_filter(const char **buf,
|
|
|
|
AVFilterGraph *graph, int index,
|
|
|
|
AVClass *log_ctx)
|
2008-05-24 23:35:43 +03:00
|
|
|
{
|
|
|
|
char *name, *opts;
|
|
|
|
name = consume_string(buf);
|
|
|
|
|
2008-05-24 23:39:32 +03:00
|
|
|
if(**buf == '=') {
|
2008-05-24 23:35:43 +03:00
|
|
|
(*buf)++;
|
|
|
|
opts = consume_string(buf);
|
|
|
|
} else {
|
|
|
|
opts = NULL;
|
|
|
|
}
|
|
|
|
|
2008-05-24 23:40:22 +03:00
|
|
|
return create_filter(graph, index, name, opts, log_ctx);
|
2008-05-24 23:35:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
enum LinkType {
|
|
|
|
LinkTypeIn,
|
|
|
|
LinkTypeOut,
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A linked-list of the inputs/outputs of the filter chain.
|
|
|
|
*/
|
|
|
|
typedef struct AVFilterInOut {
|
|
|
|
enum LinkType type;
|
2008-05-24 23:40:37 +03:00
|
|
|
const char *name;
|
2008-05-24 23:39:16 +03:00
|
|
|
AVFilterContext *filter;
|
2008-05-24 23:35:43 +03:00
|
|
|
int pad_idx;
|
|
|
|
|
|
|
|
struct AVFilterInOut *next;
|
|
|
|
} AVFilterInOut;
|
|
|
|
|
|
|
|
static void free_inout(AVFilterInOut *head)
|
|
|
|
{
|
|
|
|
while (head) {
|
2008-05-24 23:39:34 +03:00
|
|
|
AVFilterInOut *next = head->next;
|
2008-05-24 23:35:43 +03:00
|
|
|
av_free(head);
|
|
|
|
head = next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2008-05-24 23:39:29 +03:00
|
|
|
* Parse "[a1][link2] ... [etc]"
|
2008-05-24 23:35:43 +03:00
|
|
|
*/
|
2008-05-24 23:39:39 +03:00
|
|
|
static int parse_inouts(const char **buf, AVFilterInOut **inout, int pad,
|
2008-05-24 23:40:22 +03:00
|
|
|
enum LinkType type, AVFilterContext *filter,
|
|
|
|
AVClass *log_ctx)
|
2008-05-24 23:35:43 +03:00
|
|
|
{
|
2008-05-24 23:39:22 +03:00
|
|
|
while (**buf == '[') {
|
2008-05-24 23:40:08 +03:00
|
|
|
char *name;
|
2008-05-24 23:40:30 +03:00
|
|
|
AVFilterInOut *p = *inout;
|
2008-05-24 23:39:42 +03:00
|
|
|
|
2008-05-24 23:40:22 +03:00
|
|
|
parse_link_name(buf, &name, log_ctx);
|
2008-05-24 23:40:08 +03:00
|
|
|
|
2008-05-24 23:40:17 +03:00
|
|
|
if(!name)
|
2008-05-24 23:39:42 +03:00
|
|
|
return -1;
|
|
|
|
|
2008-05-24 23:40:30 +03:00
|
|
|
for (; p && strcmp(p->name, name); p = p->next);
|
|
|
|
|
|
|
|
if(!p) {
|
|
|
|
// First label apearence, add it to the linked list
|
|
|
|
AVFilterInOut *inoutn = av_malloc(sizeof(AVFilterInOut));
|
|
|
|
|
|
|
|
inoutn->name = name;
|
|
|
|
inoutn->type = type;
|
|
|
|
inoutn->filter = filter;
|
|
|
|
inoutn->pad_idx = pad;
|
|
|
|
inoutn->next = *inout;
|
|
|
|
*inout = inoutn;
|
|
|
|
} else {
|
|
|
|
|
|
|
|
if(p->type == LinkTypeIn && type == LinkTypeOut) {
|
|
|
|
if(link_filter(filter, pad, p->filter, p->pad_idx, log_ctx) < 0)
|
|
|
|
return -1;
|
|
|
|
} else if(p->type == LinkTypeOut && type == LinkTypeIn) {
|
|
|
|
if(link_filter(p->filter, p->pad_idx, filter, pad, log_ctx) < 0)
|
|
|
|
return -1;
|
|
|
|
} else {
|
|
|
|
av_log(log_ctx, AV_LOG_ERROR,
|
|
|
|
"Two links named '%s' are either both input or both output\n",
|
|
|
|
name);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
p->filter = NULL;
|
|
|
|
}
|
2008-05-24 23:40:20 +03:00
|
|
|
|
2008-05-24 23:40:30 +03:00
|
|
|
pad++;
|
2008-05-24 23:40:01 +03:00
|
|
|
consume_whitespace(buf);
|
2008-05-24 23:35:43 +03:00
|
|
|
}
|
2008-05-24 23:40:30 +03:00
|
|
|
|
2008-05-24 23:35:43 +03:00
|
|
|
return pad;
|
|
|
|
}
|
|
|
|
|
2008-05-24 23:38:25 +03:00
|
|
|
static const char *skip_inouts(const char *buf)
|
|
|
|
{
|
2008-05-24 23:40:01 +03:00
|
|
|
while (*buf == '[') {
|
2008-05-24 23:39:36 +03:00
|
|
|
buf += strcspn(buf, "]") + 1;
|
2008-05-24 23:40:01 +03:00
|
|
|
consume_whitespace(&buf);
|
|
|
|
}
|
2008-05-24 23:38:25 +03:00
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-05-24 23:35:43 +03:00
|
|
|
/**
|
|
|
|
* Parse a string describing a filter graph.
|
|
|
|
*/
|
2008-05-24 23:39:19 +03:00
|
|
|
int avfilter_parse_graph(AVFilterGraph *graph, const char *filters,
|
|
|
|
AVFilterContext *in, int inpad,
|
2008-05-24 23:40:22 +03:00
|
|
|
AVFilterContext *out, int outpad,
|
|
|
|
AVClass *log_ctx)
|
2008-05-24 23:35:43 +03:00
|
|
|
{
|
|
|
|
AVFilterInOut *inout=NULL;
|
|
|
|
AVFilterInOut *head=NULL;
|
|
|
|
|
|
|
|
int index = 0;
|
|
|
|
char chr = 0;
|
|
|
|
int pad = 0;
|
|
|
|
int has_out = 0;
|
|
|
|
|
2008-05-24 23:38:25 +03:00
|
|
|
AVFilterContext *last_filt = NULL;
|
2008-05-24 23:35:43 +03:00
|
|
|
|
|
|
|
do {
|
2008-05-24 23:38:25 +03:00
|
|
|
AVFilterContext *filter;
|
2008-05-24 23:35:43 +03:00
|
|
|
int oldpad = pad;
|
2008-05-24 23:39:57 +03:00
|
|
|
const char *inouts;
|
|
|
|
|
|
|
|
consume_whitespace(&filters);
|
|
|
|
inouts = filters;
|
2008-05-24 23:35:43 +03:00
|
|
|
|
2008-05-24 23:38:25 +03: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 23:35:43 +03:00
|
|
|
|
2008-05-24 23:40:22 +03:00
|
|
|
if(!(filter = parse_filter(&filters, graph, index, log_ctx)))
|
2008-05-24 23:35:43 +03:00
|
|
|
goto fail;
|
|
|
|
|
2008-05-24 23:40:22 +03:00
|
|
|
pad = parse_inouts(&inouts, &inout, chr == ',', LinkTypeIn, filter,
|
|
|
|
log_ctx);
|
2008-05-24 23:38:25 +03:00
|
|
|
|
2008-05-24 23:39:42 +03:00
|
|
|
if(pad < 0)
|
|
|
|
goto fail;
|
|
|
|
|
2008-05-24 23:35:43 +03:00
|
|
|
// If the first filter has an input and none was given, it is
|
|
|
|
// implicitly the input of the whole graph.
|
2008-05-24 23:39:32 +03:00
|
|
|
if(pad == 0 && filter->input_count == 1) {
|
2008-05-24 23:40:22 +03:00
|
|
|
if(link_filter(in, inpad, filter, 0, log_ctx))
|
2008-05-24 23:35:43 +03:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(chr == ',') {
|
2008-05-24 23:40:22 +03:00
|
|
|
if(link_filter(last_filt, oldpad, filter, 0, log_ctx) < 0)
|
2008-05-24 23:35:43 +03:00
|
|
|
goto fail;
|
|
|
|
}
|
2008-05-24 23:39:14 +03:00
|
|
|
|
2008-05-24 23:40:22 +03:00
|
|
|
pad = parse_inouts(&filters, &inout, 0, LinkTypeOut, filter, log_ctx);
|
2008-05-24 23:39:57 +03:00
|
|
|
|
2008-05-24 23:40:22 +03:00
|
|
|
if (pad < 0)
|
2008-05-24 23:40:14 +03:00
|
|
|
goto fail;
|
|
|
|
|
2008-05-24 23:39:57 +03:00
|
|
|
consume_whitespace(&filters);
|
|
|
|
|
2008-05-24 23:35:43 +03:00
|
|
|
chr = *filters++;
|
|
|
|
index++;
|
2008-05-24 23:38:25 +03:00
|
|
|
last_filt = filter;
|
2008-05-24 23:35:43 +03:00
|
|
|
} while (chr == ',' || chr == ';');
|
|
|
|
|
|
|
|
head = inout;
|
2008-05-24 23:40:08 +03:00
|
|
|
// Process remaining labels. Only inputs and outputs should be left.
|
2008-05-24 23:40:03 +03:00
|
|
|
for (; inout; inout = inout->next) {
|
|
|
|
if(!inout->filter)
|
2008-05-24 23:35:43 +03:00
|
|
|
continue; // Already processed
|
|
|
|
|
2008-05-24 23:39:32 +03:00
|
|
|
if(!strcmp(inout->name, "in")) {
|
2008-05-24 23:40:22 +03:00
|
|
|
if(link_filter(in, inpad, inout->filter, inout->pad_idx, log_ctx))
|
2008-05-24 23:35:43 +03:00
|
|
|
goto fail;
|
2008-05-24 23:39:03 +03:00
|
|
|
|
2008-05-24 23:39:32 +03:00
|
|
|
} else if(!strcmp(inout->name, "out")) {
|
2008-05-24 23:35:43 +03:00
|
|
|
has_out = 1;
|
|
|
|
|
2008-05-24 23:40:22 +03:00
|
|
|
if(link_filter(inout->filter, inout->pad_idx, out, outpad, log_ctx))
|
2008-05-24 23:35:43 +03:00
|
|
|
goto fail;
|
|
|
|
|
|
|
|
} else {
|
2008-05-24 23:40:22 +03:00
|
|
|
av_log(log_ctx, AV_LOG_ERROR, "Unmatched link: %s.\n",
|
2008-05-24 23:40:08 +03:00
|
|
|
inout->name);
|
2008-05-24 23:35:43 +03:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
free_inout(head);
|
|
|
|
|
2008-05-24 23:39:32 +03:00
|
|
|
if(!has_out) {
|
2008-05-24 23:40:22 +03:00
|
|
|
if(link_filter(last_filt, pad, out, outpad, log_ctx))
|
2008-05-24 23:35:43 +03:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
free_inout(head);
|
|
|
|
avfilter_destroy_graph(graph);
|
|
|
|
return -1;
|
|
|
|
}
|