You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

345 lines
8.3KB

  1. /*
  2. * filter graph parser
  3. * copyright (c) 2008 Vitor Sessak
  4. * copyright (c) 2007 Bobby Bingham
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #include <ctype.h>
  23. #include <string.h>
  24. #include "avfilter.h"
  25. #include "avfiltergraph.h"
  26. /**
  27. * For use in av_log
  28. */
  29. static const char *log_name(void *p)
  30. {
  31. return "Filter parser";
  32. }
  33. static const AVClass filter_parser_class = {
  34. "Filter parser",
  35. log_name
  36. };
  37. static const AVClass *log_ctx = &filter_parser_class;
  38. static AVFilterContext *create_filter(AVFilterGraph *ctx, int index,
  39. char *name, char *args)
  40. {
  41. AVFilterContext *filt;
  42. AVFilter *filterdef;
  43. char inst_name[30];
  44. snprintf(inst_name, sizeof(inst_name), "Parsed filter %d", index);
  45. if(!(filterdef = avfilter_get_by_name(name))) {
  46. av_log(&log_ctx, AV_LOG_ERROR,
  47. "no such filter: '%s'\n", name);
  48. return NULL;
  49. }
  50. if(!(filt = avfilter_open(filterdef, inst_name))) {
  51. av_log(&log_ctx, AV_LOG_ERROR,
  52. "error creating filter '%s'\n", name);
  53. return NULL;
  54. }
  55. if(avfilter_graph_add_filter(ctx, filt) < 0)
  56. return NULL;
  57. if(avfilter_init_filter(filt, args, NULL)) {
  58. av_log(&log_ctx, AV_LOG_ERROR,
  59. "error initializing filter '%s' with args '%s'\n", name, args);
  60. return NULL;
  61. }
  62. return filt;
  63. }
  64. static int link_filter(AVFilterContext *src, int srcpad,
  65. AVFilterContext *dst, int dstpad)
  66. {
  67. if(avfilter_link(src, srcpad, dst, dstpad)) {
  68. av_log(&log_ctx, AV_LOG_ERROR,
  69. "cannot create the link %s:%d -> %s:%d\n",
  70. src->filter->name, srcpad, dst->filter->name, dstpad);
  71. return -1;
  72. }
  73. return 0;
  74. }
  75. static void consume_whitespace(const char **buf)
  76. {
  77. *buf += strspn(*buf, " \n\t");
  78. }
  79. /**
  80. * Consumes a string from *buf.
  81. * @return a copy of the consumed string, which should be free'd after use
  82. */
  83. static char *consume_string(const char **buf)
  84. {
  85. char *out = av_malloc(strlen(*buf) + 1);
  86. const char *in = *buf;
  87. char *ret = out;
  88. consume_whitespace(buf);
  89. do{
  90. char c = *in++;
  91. switch (c) {
  92. case '\\':
  93. *out++= *in++;
  94. break;
  95. case '\'':
  96. while(*in && *in != '\'')
  97. *out++= *in++;
  98. if(*in) in++;
  99. break;
  100. case 0:
  101. case ']':
  102. case '[':
  103. case '=':
  104. case ',':
  105. *out++= 0;
  106. break;
  107. default:
  108. *out++= c;
  109. }
  110. } while(out[-1]);
  111. *buf = in-1;
  112. return ret;
  113. }
  114. /**
  115. * Parse "[linkname]"
  116. * @arg name a pointer (that need to be free'd after use) to the name between
  117. * parenthesis
  118. */
  119. static void parse_link_name(const char **buf, char **name)
  120. {
  121. (*buf)++;
  122. *name = consume_string(buf);
  123. if(!*name[0])
  124. goto fail;
  125. if(*(*buf)++ != ']')
  126. goto fail;
  127. return;
  128. fail:
  129. av_freep(name);
  130. av_log(&log_ctx, AV_LOG_ERROR, "Could not parse link name!\n");
  131. }
  132. /**
  133. * Parse "filter=params"
  134. * @arg name a pointer (that need to be free'd after use) to the name of the
  135. * filter
  136. * @arg ars a pointer (that need to be free'd after use) to the args of the
  137. * filter
  138. */
  139. static AVFilterContext *parse_filter(const char **buf, AVFilterGraph *graph, int index)
  140. {
  141. char *name, *opts;
  142. name = consume_string(buf);
  143. if(**buf == '=') {
  144. (*buf)++;
  145. opts = consume_string(buf);
  146. } else {
  147. opts = NULL;
  148. }
  149. return create_filter(graph, index, name, opts);
  150. }
  151. enum LinkType {
  152. LinkTypeIn,
  153. LinkTypeOut,
  154. };
  155. /**
  156. * A linked-list of the inputs/outputs of the filter chain.
  157. */
  158. typedef struct AVFilterInOut {
  159. enum LinkType type;
  160. char *name;
  161. AVFilterContext *filter;
  162. int pad_idx;
  163. struct AVFilterInOut *next;
  164. } AVFilterInOut;
  165. static void free_inout(AVFilterInOut *head)
  166. {
  167. while (head) {
  168. AVFilterInOut *next = head->next;
  169. av_free(head);
  170. head = next;
  171. }
  172. }
  173. /**
  174. * Parse "[a1][link2] ... [etc]"
  175. */
  176. static int parse_inouts(const char **buf, AVFilterInOut **inout, int firstpad,
  177. enum LinkType type, AVFilterContext *filter)
  178. {
  179. int pad = firstpad;
  180. while (**buf == '[') {
  181. AVFilterInOut *inoutn = av_malloc(sizeof(AVFilterInOut));
  182. parse_link_name(buf, &inoutn->name);
  183. inoutn->type = type;
  184. inoutn->filter = filter;
  185. inoutn->pad_idx = pad++;
  186. inoutn->next = *inout;
  187. *inout = inoutn;
  188. }
  189. return pad;
  190. }
  191. static const char *skip_inouts(const char *buf)
  192. {
  193. while (*buf == '[')
  194. buf += strcspn(buf, "]") + 1;
  195. return buf;
  196. }
  197. /**
  198. * Parse a string describing a filter graph.
  199. */
  200. int avfilter_parse_graph(AVFilterGraph *graph, const char *filters,
  201. AVFilterContext *in, int inpad,
  202. AVFilterContext *out, int outpad)
  203. {
  204. AVFilterInOut *inout=NULL;
  205. AVFilterInOut *head=NULL;
  206. int index = 0;
  207. char chr = 0;
  208. int pad = 0;
  209. int has_out = 0;
  210. AVFilterContext *last_filt = NULL;
  211. consume_whitespace(&filters);
  212. do {
  213. AVFilterContext *filter;
  214. int oldpad = pad;
  215. const char *inouts = filters;
  216. // We need to parse the inputs of the filter after we create it, so
  217. // skip it by now
  218. filters = skip_inouts(filters);
  219. if(!(filter = parse_filter(&filters, graph, index)))
  220. goto fail;
  221. pad = parse_inouts(&inouts, &inout, chr == ',', LinkTypeIn, filter);
  222. // If the first filter has an input and none was given, it is
  223. // implicitly the input of the whole graph.
  224. if(pad == 0 && filter->input_count == 1) {
  225. if(link_filter(in, inpad, filter, 0))
  226. goto fail;
  227. }
  228. if(chr == ',') {
  229. if(link_filter(last_filt, oldpad, filter, 0) < 0)
  230. goto fail;
  231. }
  232. pad = parse_inouts(&filters, &inout, 0, LinkTypeOut, filter);
  233. chr = *filters++;
  234. index++;
  235. last_filt = filter;
  236. } while (chr == ',' || chr == ';');
  237. head = inout;
  238. for (; inout != NULL; inout = inout->next) {
  239. if(inout->filter == NULL)
  240. continue; // Already processed
  241. if(!strcmp(inout->name, "in")) {
  242. if(link_filter(in, inpad, inout->filter, inout->pad_idx))
  243. goto fail;
  244. } else if(!strcmp(inout->name, "out")) {
  245. has_out = 1;
  246. if(link_filter(inout->filter, inout->pad_idx, out, outpad))
  247. goto fail;
  248. } else {
  249. AVFilterInOut *p, *src, *dst;
  250. for (p = inout->next;
  251. p && strcmp(p->name,inout->name); p = p->next);
  252. if(!p) {
  253. av_log(&log_ctx, AV_LOG_ERROR, "Unmatched link: %s.\n",
  254. inout->name);
  255. goto fail;
  256. }
  257. if(p->type == LinkTypeIn && inout->type == LinkTypeOut) {
  258. src = inout;
  259. dst = p;
  260. } else if(p->type == LinkTypeOut && inout->type == LinkTypeIn) {
  261. src = p;
  262. dst = inout;
  263. } else {
  264. av_log(&log_ctx, AV_LOG_ERROR, "Two links named '%s' are either both input or both output\n",
  265. inout->name);
  266. goto fail;
  267. }
  268. if(link_filter(src->filter, src->pad_idx, dst->filter, dst->pad_idx) < 0)
  269. goto fail;
  270. src->filter = NULL;
  271. dst->filter = NULL;
  272. }
  273. }
  274. free_inout(head);
  275. if(!has_out) {
  276. if(link_filter(last_filt, pad, out, outpad))
  277. goto fail;
  278. }
  279. return 0;
  280. fail:
  281. free_inout(head);
  282. avfilter_destroy_graph(graph);
  283. return -1;
  284. }