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.

380 lines
9.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 tmp[20];
  44. snprintf(tmp, 20, "%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, tmp))) {
  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. * Copy the first size bytes of input string to a null-terminated string,
  81. * removing any control character. Ex: "aaa'bb'c\'c\\" -> "aaabbc'c\"
  82. */
  83. static void copy_unquoted(char *out, const char *in, int size)
  84. {
  85. int i;
  86. for (i=0; i < size; i++) {
  87. if (in[i] == '\'')
  88. continue;
  89. else if (in[i] == '\\') {
  90. if (i+1 == size) {
  91. *out = 0;
  92. return;
  93. }
  94. i++;
  95. }
  96. *out++ = in[i];
  97. }
  98. *out=0;
  99. }
  100. /**
  101. * Consumes a string from *buf.
  102. * @return a copy of the consumed string, which should be free'd after use
  103. */
  104. static char *consume_string(const char **buf)
  105. {
  106. const char *start;
  107. char *ret;
  108. int size;
  109. consume_whitespace(buf);
  110. if (!(**buf))
  111. return av_mallocz(1);
  112. start = *buf;
  113. while(1) {
  114. *buf += strcspn(*buf, " ()=,'\\");
  115. if (**buf == '\\')
  116. *buf+=2;
  117. else
  118. break;
  119. }
  120. if (**buf == '\'') {
  121. const char *p = *buf;
  122. do {
  123. p++;
  124. p = strchr(p, '\'');
  125. } while (p && p[-1] == '\\');
  126. if (p)
  127. *buf = p + 1;
  128. else
  129. *buf += strlen(*buf); // Move the pointer to the null end byte
  130. }
  131. size = *buf - start + 1;
  132. ret = av_malloc(size);
  133. copy_unquoted(ret, start, size-1);
  134. return ret;
  135. }
  136. /**
  137. * Parse "(linkname)"
  138. * @arg name a pointer (that need to be free'd after use) to the name between
  139. * parenthesis
  140. */
  141. static void parse_link_name(const char **buf, char **name)
  142. {
  143. (*buf)++;
  144. *name = consume_string(buf);
  145. if (!*name[0])
  146. goto fail;
  147. if (*(*buf)++ != ')')
  148. goto fail;
  149. return;
  150. fail:
  151. av_freep(name);
  152. av_log(&log_ctx, AV_LOG_ERROR, "Could not parse link name!\n");
  153. }
  154. /**
  155. * Parse "filter=params"
  156. * @arg name a pointer (that need to be free'd after use) to the name of the
  157. * filter
  158. * @arg ars a pointer (that need to be free'd after use) to the args of the
  159. * filter
  160. */
  161. static AVFilterContext *parse_filter(const char **buf, AVFilterGraph *graph, int index)
  162. {
  163. char *name, *opts;
  164. name = consume_string(buf);
  165. if (**buf == '=') {
  166. (*buf)++;
  167. opts = consume_string(buf);
  168. } else {
  169. opts = NULL;
  170. }
  171. return create_filter(graph, index, name, opts);
  172. }
  173. enum LinkType {
  174. LinkTypeIn,
  175. LinkTypeOut,
  176. };
  177. /**
  178. * A linked-list of the inputs/outputs of the filter chain.
  179. */
  180. typedef struct AVFilterInOut {
  181. enum LinkType type;
  182. char *name;
  183. AVFilterContext *instance;
  184. int pad_idx;
  185. struct AVFilterInOut *next;
  186. } AVFilterInOut;
  187. static void free_inout(AVFilterInOut *head)
  188. {
  189. while (head) {
  190. AVFilterInOut *next;
  191. next = head->next;
  192. av_free(head);
  193. head = next;
  194. }
  195. }
  196. /**
  197. * Parse "(a1)(link2) ... (etc)"
  198. */
  199. static int parse_inouts(const char **buf, AVFilterInOut **inout, int firstpad,
  200. enum LinkType type, AVFilterContext *instance)
  201. {
  202. int pad = firstpad;
  203. while (**buf == '(') {
  204. AVFilterInOut *inoutn = av_malloc(sizeof(AVFilterInOut));
  205. parse_link_name(buf, &inoutn->name);
  206. inoutn->type = type;
  207. inoutn->instance = instance;
  208. inoutn->pad_idx = pad++;
  209. inoutn->next = *inout;
  210. *inout = inoutn;
  211. }
  212. return pad;
  213. }
  214. static const char *skip_inouts(const char *buf)
  215. {
  216. while (*buf == '(') {
  217. buf += strcspn(buf, ")");
  218. buf++;
  219. }
  220. return buf;
  221. }
  222. /**
  223. * Parse a string describing a filter graph.
  224. */
  225. int avfilter_graph_parse_chain(AVFilterGraph *graph, const char *filters, AVFilterContext *in, int inpad, AVFilterContext *out, int outpad)
  226. {
  227. AVFilterInOut *inout=NULL;
  228. AVFilterInOut *head=NULL;
  229. int index = 0;
  230. char chr = 0;
  231. int pad = 0;
  232. int has_out = 0;
  233. AVFilterContext *last_filt = NULL;
  234. consume_whitespace(&filters);
  235. do {
  236. AVFilterContext *filter;
  237. int oldpad = pad;
  238. const char *inouts = filters;
  239. // We need to parse the inputs of the filter after we create it, so
  240. // skip it by now
  241. filters = skip_inouts(filters);
  242. if (!(filter = parse_filter(&filters, graph, index)))
  243. goto fail;
  244. pad = parse_inouts(&inouts, &inout, chr == ',', LinkTypeIn, filter);
  245. // If the first filter has an input and none was given, it is
  246. // implicitly the input of the whole graph.
  247. if (pad == 0 && graph->filters[graph->filter_count-1]->input_count == 1) {
  248. if(avfilter_link(in, inpad, filter, 0)) {
  249. av_log(&log_ctx, AV_LOG_ERROR, "cannot create link between source and destination filters\n");
  250. goto fail;
  251. }
  252. }
  253. if(chr == ',') {
  254. if (link_filter(last_filt, oldpad, filter, 0) < 0)
  255. goto fail;
  256. }
  257. pad = parse_inouts(&filters, &inout, 0, LinkTypeOut, filter);
  258. chr = *filters++;
  259. index++;
  260. last_filt = filter;
  261. } while (chr == ',' || chr == ';');
  262. head = inout;
  263. for (; inout != NULL; inout = inout->next) {
  264. if (inout->instance == NULL)
  265. continue; // Already processed
  266. if (!strcmp(inout->name, "in")) {
  267. if(avfilter_link(in, inpad, inout->instance, inout->pad_idx)) {
  268. av_log(&log_ctx, AV_LOG_ERROR, "cannot create link between source and destination filters\n");
  269. goto fail;
  270. }
  271. } else if (!strcmp(inout->name, "out")) {
  272. has_out = 1;
  273. if(avfilter_link(inout->instance, inout->pad_idx, out, outpad)) {
  274. av_log(&log_ctx, AV_LOG_ERROR, "cannot create link between source and destination filters\n");
  275. goto fail;
  276. }
  277. } else {
  278. AVFilterInOut *p, *src, *dst;
  279. for (p = inout->next;
  280. p && strcmp(p->name,inout->name); p = p->next);
  281. if (!p) {
  282. av_log(&log_ctx, AV_LOG_ERROR, "Unmatched link: %s.\n",
  283. inout->name);
  284. goto fail;
  285. }
  286. if (p->type == LinkTypeIn && inout->type == LinkTypeOut) {
  287. src = inout;
  288. dst = p;
  289. } else if (p->type == LinkTypeOut && inout->type == LinkTypeIn) {
  290. src = p;
  291. dst = inout;
  292. } else {
  293. av_log(&log_ctx, AV_LOG_ERROR, "Two links named '%s' are either both input or both output\n",
  294. inout->name);
  295. goto fail;
  296. }
  297. if (link_filter(src->instance, src->pad_idx, dst->instance, dst->pad_idx) < 0)
  298. goto fail;
  299. src->instance = NULL;
  300. dst->instance = NULL;
  301. }
  302. }
  303. free_inout(head);
  304. if (!has_out) {
  305. if(avfilter_link(last_filt, pad, out, outpad)) {
  306. av_log(&log_ctx, AV_LOG_ERROR, "cannot create link between source and destination filters\n");
  307. goto fail;
  308. }
  309. }
  310. return 0;
  311. fail:
  312. free_inout(head);
  313. avfilter_destroy_graph(graph);
  314. return -1;
  315. }