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.

347 lines
8.4KB

  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));
  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;
  169. next = head->next;
  170. av_free(head);
  171. head = next;
  172. }
  173. }
  174. /**
  175. * Parse "(a1)(link2) ... (etc)"
  176. */
  177. static int parse_inouts(const char **buf, AVFilterInOut **inout, int firstpad,
  178. enum LinkType type, AVFilterContext *filter)
  179. {
  180. int pad = firstpad;
  181. while (**buf == '[') {
  182. AVFilterInOut *inoutn = av_malloc(sizeof(AVFilterInOut));
  183. parse_link_name(buf, &inoutn->name);
  184. inoutn->type = type;
  185. inoutn->filter = filter;
  186. inoutn->pad_idx = pad++;
  187. inoutn->next = *inout;
  188. *inout = inoutn;
  189. }
  190. return pad;
  191. }
  192. static const char *skip_inouts(const char *buf)
  193. {
  194. while (*buf == '[') {
  195. buf += strcspn(buf, "]");
  196. buf++;
  197. }
  198. return buf;
  199. }
  200. /**
  201. * Parse a string describing a filter graph.
  202. */
  203. int avfilter_parse_graph(AVFilterGraph *graph, const char *filters,
  204. AVFilterContext *in, int inpad,
  205. AVFilterContext *out, int outpad)
  206. {
  207. AVFilterInOut *inout=NULL;
  208. AVFilterInOut *head=NULL;
  209. int index = 0;
  210. char chr = 0;
  211. int pad = 0;
  212. int has_out = 0;
  213. AVFilterContext *last_filt = NULL;
  214. consume_whitespace(&filters);
  215. do {
  216. AVFilterContext *filter;
  217. int oldpad = pad;
  218. const char *inouts = filters;
  219. // We need to parse the inputs of the filter after we create it, so
  220. // skip it by now
  221. filters = skip_inouts(filters);
  222. if (!(filter = parse_filter(&filters, graph, index)))
  223. goto fail;
  224. pad = parse_inouts(&inouts, &inout, chr == ',', LinkTypeIn, filter);
  225. // If the first filter has an input and none was given, it is
  226. // implicitly the input of the whole graph.
  227. if (pad == 0 && filter->input_count == 1) {
  228. if(link_filter(in, inpad, filter, 0))
  229. goto fail;
  230. }
  231. if(chr == ',') {
  232. if (link_filter(last_filt, oldpad, filter, 0) < 0)
  233. goto fail;
  234. }
  235. pad = parse_inouts(&filters, &inout, 0, LinkTypeOut, filter);
  236. chr = *filters++;
  237. index++;
  238. last_filt = filter;
  239. } while (chr == ',' || chr == ';');
  240. head = inout;
  241. for (; inout != NULL; inout = inout->next) {
  242. if (inout->filter == NULL)
  243. continue; // Already processed
  244. if (!strcmp(inout->name, "in")) {
  245. if(link_filter(in, inpad, inout->filter, inout->pad_idx))
  246. goto fail;
  247. } else if (!strcmp(inout->name, "out")) {
  248. has_out = 1;
  249. if(link_filter(inout->filter, inout->pad_idx, out, outpad))
  250. goto fail;
  251. } else {
  252. AVFilterInOut *p, *src, *dst;
  253. for (p = inout->next;
  254. p && strcmp(p->name,inout->name); p = p->next);
  255. if (!p) {
  256. av_log(&log_ctx, AV_LOG_ERROR, "Unmatched link: %s.\n",
  257. inout->name);
  258. goto fail;
  259. }
  260. if (p->type == LinkTypeIn && inout->type == LinkTypeOut) {
  261. src = inout;
  262. dst = p;
  263. } else if (p->type == LinkTypeOut && inout->type == LinkTypeIn) {
  264. src = p;
  265. dst = inout;
  266. } else {
  267. av_log(&log_ctx, AV_LOG_ERROR, "Two links named '%s' are either both input or both output\n",
  268. inout->name);
  269. goto fail;
  270. }
  271. if (link_filter(src->filter, src->pad_idx, dst->filter, dst->pad_idx) < 0)
  272. goto fail;
  273. src->filter = NULL;
  274. dst->filter = NULL;
  275. }
  276. }
  277. free_inout(head);
  278. if (!has_out) {
  279. if(link_filter(last_filt, pad, out, outpad))
  280. goto fail;
  281. }
  282. return 0;
  283. fail:
  284. free_inout(head);
  285. avfilter_destroy_graph(graph);
  286. return -1;
  287. }