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. static AVFilterContext *create_filter(AVFilterGraph *ctx, int index,
  27. const char *name, const char *args,
  28. AVClass *log_ctx)
  29. {
  30. AVFilterContext *filt;
  31. AVFilter *filterdef;
  32. char inst_name[30];
  33. snprintf(inst_name, sizeof(inst_name), "Parsed filter %d", index);
  34. if(!(filterdef = avfilter_get_by_name(name))) {
  35. av_log(&log_ctx, AV_LOG_ERROR,
  36. "no such filter: '%s'\n", name);
  37. return NULL;
  38. }
  39. if(!(filt = avfilter_open(filterdef, inst_name))) {
  40. av_log(log_ctx, AV_LOG_ERROR,
  41. "error creating filter '%s'\n", name);
  42. return NULL;
  43. }
  44. if(avfilter_graph_add_filter(ctx, filt) < 0)
  45. return NULL;
  46. if(avfilter_init_filter(filt, args, NULL)) {
  47. av_log(log_ctx, AV_LOG_ERROR,
  48. "error initializing filter '%s' with args '%s'\n", name, args);
  49. return NULL;
  50. }
  51. return filt;
  52. }
  53. static int link_filter(AVFilterContext *src, int srcpad,
  54. AVFilterContext *dst, int dstpad,
  55. AVClass *log_ctx)
  56. {
  57. if(avfilter_link(src, srcpad, dst, dstpad)) {
  58. av_log(log_ctx, AV_LOG_ERROR,
  59. "cannot create the link %s:%d -> %s:%d\n",
  60. src->filter->name, srcpad, dst->filter->name, dstpad);
  61. return -1;
  62. }
  63. return 0;
  64. }
  65. static void consume_whitespace(const char **buf)
  66. {
  67. *buf += strspn(*buf, " \n\t");
  68. }
  69. /**
  70. * Consumes a string from *buf.
  71. * @return a copy of the consumed string, which should be free'd after use
  72. */
  73. static char *consume_string(const char **buf)
  74. {
  75. char *out = av_malloc(strlen(*buf) + 1);
  76. char *ret = out;
  77. consume_whitespace(buf);
  78. do{
  79. char c = *(*buf)++;
  80. switch (c) {
  81. case '\\':
  82. *out++= *(*buf)++;
  83. break;
  84. case '\'':
  85. while(**buf && **buf != '\'')
  86. *out++= *(*buf)++;
  87. if(**buf) (*buf)++;
  88. break;
  89. case 0:
  90. case ']':
  91. case '[':
  92. case '=':
  93. case ',':
  94. case ' ':
  95. case '\n':
  96. *out++= 0;
  97. break;
  98. default:
  99. *out++= c;
  100. }
  101. } while(out[-1]);
  102. (*buf)--;
  103. consume_whitespace(buf);
  104. return ret;
  105. }
  106. /**
  107. * Parse "[linkname]"
  108. * @arg name a pointer (that need to be free'd after use) to the name between
  109. * parenthesis
  110. */
  111. static void parse_link_name(const char **buf, char **name, AVClass *log_ctx)
  112. {
  113. const char *start = *buf;
  114. (*buf)++;
  115. *name = consume_string(buf);
  116. if(!*name[0]) {
  117. av_log(log_ctx, AV_LOG_ERROR,
  118. "Bad (empty?) label found in the following: \"%s\".\n", start);
  119. goto fail;
  120. }
  121. if(*(*buf)++ != ']') {
  122. av_log(log_ctx, AV_LOG_ERROR,
  123. "Mismatched '[' found in the following: \"%s\".\n", start);
  124. fail:
  125. av_freep(name);
  126. }
  127. }
  128. /**
  129. * Parse "filter=params"
  130. * @arg name a pointer (that need to be free'd after use) to the name of the
  131. * filter
  132. * @arg ars a pointer (that need to be free'd after use) to the args of the
  133. * filter
  134. */
  135. static AVFilterContext *parse_filter(const char **buf,
  136. AVFilterGraph *graph, int index,
  137. AVClass *log_ctx)
  138. {
  139. char *name, *opts;
  140. name = consume_string(buf);
  141. if(**buf == '=') {
  142. (*buf)++;
  143. opts = consume_string(buf);
  144. } else {
  145. opts = NULL;
  146. }
  147. return create_filter(graph, index, name, opts, log_ctx);
  148. }
  149. enum LinkType {
  150. LinkTypeIn,
  151. LinkTypeOut,
  152. };
  153. /**
  154. * A linked-list of the inputs/outputs of the filter chain.
  155. */
  156. typedef struct AVFilterInOut {
  157. enum LinkType type;
  158. char *name;
  159. AVFilterContext *filter;
  160. int pad_idx;
  161. struct AVFilterInOut *next;
  162. } AVFilterInOut;
  163. static void free_inout(AVFilterInOut *head)
  164. {
  165. while (head) {
  166. AVFilterInOut *next = head->next;
  167. av_free(head);
  168. head = next;
  169. }
  170. }
  171. /**
  172. * Process a link. This funcion looks for a matching label in the *inout
  173. * linked list. If none is found, it adds this link to the list.
  174. */
  175. static int handle_link(char *name, AVFilterInOut **inout, int pad,
  176. enum LinkType type, AVFilterContext *filter,
  177. AVClass *log_ctx)
  178. {
  179. AVFilterInOut *p = *inout;
  180. for (; p && strcmp(p->name, name); p = p->next);
  181. if(!p) {
  182. // First label apearence, add it to the linked list
  183. AVFilterInOut *inoutn = av_malloc(sizeof(AVFilterInOut));
  184. inoutn->name = name;
  185. inoutn->type = type;
  186. inoutn->filter = filter;
  187. inoutn->pad_idx = pad;
  188. inoutn->next = *inout;
  189. *inout = inoutn;
  190. return 0;
  191. }
  192. if(p->type == LinkTypeIn && type == LinkTypeOut) {
  193. if(link_filter(filter, pad, p->filter, p->pad_idx, log_ctx) < 0)
  194. goto fail;
  195. } else if(p->type == LinkTypeOut && type == LinkTypeIn) {
  196. if(link_filter(p->filter, p->pad_idx, filter, pad, log_ctx) < 0)
  197. goto fail;
  198. } else {
  199. av_log(log_ctx, AV_LOG_ERROR,
  200. "Two links named '%s' are either both input or both output\n",
  201. name);
  202. goto fail;
  203. }
  204. p->filter = NULL;
  205. return 0;
  206. fail:
  207. return -1;
  208. }
  209. /**
  210. * Parse "[a1][link2] ... [etc]"
  211. */
  212. static int parse_inouts(const char **buf, AVFilterInOut **inout, int pad,
  213. enum LinkType type, AVFilterContext *filter,
  214. AVClass *log_ctx)
  215. {
  216. while (**buf == '[') {
  217. char *name;
  218. parse_link_name(buf, &name, log_ctx);
  219. if(!name)
  220. return -1;
  221. if(handle_link(name, inout, pad++, type, filter, log_ctx) < 0)
  222. return -1;
  223. consume_whitespace(buf);
  224. }
  225. return pad;
  226. }
  227. static const char *skip_inouts(const char *buf)
  228. {
  229. while (*buf == '[') {
  230. buf += strcspn(buf, "]") + 1;
  231. consume_whitespace(&buf);
  232. }
  233. return buf;
  234. }
  235. /**
  236. * Parse a string describing a filter graph.
  237. */
  238. int avfilter_parse_graph(AVFilterGraph *graph, const char *filters,
  239. AVFilterContext *in, int inpad,
  240. AVFilterContext *out, int outpad,
  241. AVClass *log_ctx)
  242. {
  243. AVFilterInOut *inout=NULL;
  244. AVFilterInOut *head=NULL;
  245. int index = 0;
  246. char chr = 0;
  247. int pad = 0;
  248. int has_out = 0;
  249. AVFilterContext *last_filt = NULL;
  250. do {
  251. AVFilterContext *filter;
  252. int oldpad = pad;
  253. const char *inouts;
  254. consume_whitespace(&filters);
  255. inouts = filters;
  256. // We need to parse the inputs of the filter after we create it, so
  257. // skip it by now
  258. filters = skip_inouts(filters);
  259. if(!(filter = parse_filter(&filters, graph, index, log_ctx)))
  260. goto fail;
  261. pad = parse_inouts(&inouts, &inout, chr == ',', LinkTypeIn, filter,
  262. log_ctx);
  263. if(pad < 0)
  264. goto fail;
  265. // If the first filter has an input and none was given, it is
  266. // implicitly the input of the whole graph.
  267. if(pad == 0 && filter->input_count == 1) {
  268. if(link_filter(in, inpad, filter, 0, log_ctx))
  269. goto fail;
  270. }
  271. if(chr == ',') {
  272. if(link_filter(last_filt, oldpad, filter, 0, log_ctx) < 0)
  273. goto fail;
  274. }
  275. pad = parse_inouts(&filters, &inout, 0, LinkTypeOut, filter, log_ctx);
  276. if (pad < 0)
  277. goto fail;
  278. consume_whitespace(&filters);
  279. chr = *filters++;
  280. index++;
  281. last_filt = filter;
  282. } while (chr == ',' || chr == ';');
  283. head = inout;
  284. // Process remaining labels. Only inputs and outputs should be left.
  285. for (; inout; inout = inout->next) {
  286. if(!inout->filter)
  287. continue; // Already processed
  288. if(!strcmp(inout->name, "in")) {
  289. if(link_filter(in, inpad, inout->filter, inout->pad_idx, log_ctx))
  290. goto fail;
  291. } else if(!strcmp(inout->name, "out")) {
  292. has_out = 1;
  293. if(link_filter(inout->filter, inout->pad_idx, out, outpad, log_ctx))
  294. goto fail;
  295. } else {
  296. av_log(log_ctx, AV_LOG_ERROR, "Unmatched link: %s.\n",
  297. inout->name);
  298. goto fail;
  299. }
  300. }
  301. free_inout(head);
  302. if(!has_out) {
  303. if(link_filter(last_filt, pad, out, outpad, log_ctx))
  304. goto fail;
  305. }
  306. return 0;
  307. fail:
  308. free_inout(head);
  309. avfilter_destroy_graph(graph);
  310. return -1;
  311. }