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.

407 lines
10KB

  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 "graphparser.h"
  25. #include "avfilter.h"
  26. #include "avfiltergraph.h"
  27. static int link_filter(AVFilterContext *src, int srcpad,
  28. AVFilterContext *dst, int dstpad,
  29. AVClass *log_ctx)
  30. {
  31. if(avfilter_link(src, srcpad, dst, dstpad)) {
  32. av_log(log_ctx, AV_LOG_ERROR,
  33. "cannot create the link %s:%d -> %s:%d\n",
  34. src->filter->name, srcpad, dst->filter->name, dstpad);
  35. return -1;
  36. }
  37. return 0;
  38. }
  39. static int consume_whitespace(const char *buf)
  40. {
  41. return strspn(buf, " \n\t");
  42. }
  43. /**
  44. * Consumes a string from *buf.
  45. * @return a copy of the consumed string, which should be free'd after use
  46. */
  47. static char *consume_string(const char **buf)
  48. {
  49. char *out = av_malloc(strlen(*buf) + 1);
  50. char *ret = out;
  51. *buf += consume_whitespace(*buf);
  52. do{
  53. char c = *(*buf)++;
  54. switch (c) {
  55. case '\\':
  56. *out++ = *(*buf)++;
  57. break;
  58. case '\'':
  59. while(**buf && **buf != '\'')
  60. *out++ = *(*buf)++;
  61. if(**buf) (*buf)++;
  62. break;
  63. case 0:
  64. case ']':
  65. case '[':
  66. case '=':
  67. case ',':
  68. case ';':
  69. case ' ':
  70. case '\n':
  71. *out++ = 0;
  72. break;
  73. default:
  74. *out++ = c;
  75. }
  76. } while(out[-1]);
  77. (*buf)--;
  78. *buf += consume_whitespace(*buf);
  79. return ret;
  80. }
  81. /**
  82. * Parse "[linkname]"
  83. * @param name a pointer (that need to be free'd after use) to the name between
  84. * parenthesis
  85. */
  86. static char *parse_link_name(const char **buf, AVClass *log_ctx)
  87. {
  88. const char *start = *buf;
  89. char *name;
  90. (*buf)++;
  91. name = consume_string(buf);
  92. if(!name[0]) {
  93. av_log(log_ctx, AV_LOG_ERROR,
  94. "Bad (empty?) label found in the following: \"%s\".\n", start);
  95. goto fail;
  96. }
  97. if(*(*buf)++ != ']') {
  98. av_log(log_ctx, AV_LOG_ERROR,
  99. "Mismatched '[' found in the following: \"%s\".\n", start);
  100. fail:
  101. av_freep(&name);
  102. }
  103. return name;
  104. }
  105. static AVFilterContext *create_filter(AVFilterGraph *ctx, int index,
  106. const char *name, const char *args,
  107. AVClass *log_ctx)
  108. {
  109. AVFilterContext *filt;
  110. AVFilter *filterdef;
  111. char inst_name[30];
  112. snprintf(inst_name, sizeof(inst_name), "Parsed filter %d", index);
  113. filterdef = avfilter_get_by_name(name);
  114. if(!filterdef) {
  115. av_log(log_ctx, AV_LOG_ERROR,
  116. "no such filter: '%s'\n", name);
  117. return NULL;
  118. }
  119. filt = avfilter_open(filterdef, inst_name);
  120. if(!filt) {
  121. av_log(log_ctx, AV_LOG_ERROR,
  122. "error creating filter '%s'\n", name);
  123. return NULL;
  124. }
  125. if(avfilter_graph_add_filter(ctx, filt) < 0)
  126. return NULL;
  127. if(avfilter_init_filter(filt, args, NULL)) {
  128. av_log(log_ctx, AV_LOG_ERROR,
  129. "error initializing filter '%s' with args '%s'\n", name, args);
  130. return NULL;
  131. }
  132. return filt;
  133. }
  134. /**
  135. * Parse "filter=params"
  136. */
  137. static AVFilterContext *parse_filter(const char **buf, AVFilterGraph *graph,
  138. int index, AVClass *log_ctx)
  139. {
  140. char *opts = NULL;
  141. char *name = consume_string(buf);
  142. if(**buf == '=') {
  143. (*buf)++;
  144. opts = consume_string(buf);
  145. }
  146. return create_filter(graph, index, name, opts, log_ctx);
  147. }
  148. static void free_inout(AVFilterInOut *head)
  149. {
  150. while(head) {
  151. AVFilterInOut *next = head->next;
  152. av_free(head);
  153. head = next;
  154. }
  155. }
  156. static AVFilterInOut *extract_inout(const char *label, AVFilterInOut **links)
  157. {
  158. AVFilterInOut *ret;
  159. while(*links && strcmp((*links)->name, label))
  160. links = &((*links)->next);
  161. ret = *links;
  162. if(ret)
  163. *links = ret->next;
  164. return ret;
  165. }
  166. static int link_filter_inouts(AVFilterContext *filter,
  167. AVFilterInOut **currInputs,
  168. AVFilterInOut **openLinks, AVClass *log_ctx)
  169. {
  170. int pad = 0;
  171. pad = filter->input_count;
  172. while(pad--) {
  173. AVFilterInOut *p = *currInputs;
  174. *currInputs = (*currInputs)->next;
  175. if(!p) {
  176. av_log(log_ctx, AV_LOG_ERROR,
  177. "Not enough inputs specified for the \"%s\" filter.\n",
  178. filter->filter->name);
  179. return -1;
  180. }
  181. if(p->filter) {
  182. if(link_filter(p->filter, p->pad_idx, filter, pad, log_ctx))
  183. return -1;
  184. av_free(p);
  185. } else {
  186. p->filter = filter;
  187. p->pad_idx = pad;
  188. p->next = *openLinks;
  189. *openLinks = p;
  190. }
  191. }
  192. if(*currInputs) {
  193. av_log(log_ctx, AV_LOG_ERROR,
  194. "Too many inputs specified for the \"%s\" filter.\n",
  195. filter->filter->name);
  196. return -1;
  197. }
  198. pad = filter->output_count;
  199. while(pad--) {
  200. AVFilterInOut *currlinkn = av_malloc(sizeof(AVFilterInOut));
  201. currlinkn->name = NULL;
  202. currlinkn->type = LinkTypeOut;
  203. currlinkn->filter = filter;
  204. currlinkn->pad_idx = pad;
  205. currlinkn->next = *currInputs;
  206. *currInputs = currlinkn;
  207. }
  208. return 0;
  209. }
  210. static int parse_inputs(const char **buf, AVFilterInOut **currInputs,
  211. AVFilterInOut **openLinks, AVClass *log_ctx)
  212. {
  213. int pad = 0;
  214. while(**buf == '[') {
  215. char *name = parse_link_name(buf, log_ctx);
  216. AVFilterInOut *link_to_add;
  217. AVFilterInOut *match;
  218. if(!name)
  219. return -1;
  220. /* First check if the label is not in the openLinks list */
  221. match = extract_inout(name, openLinks);
  222. if(match) {
  223. /* A label of a open link. Make it one of the inputs of the next
  224. filter */
  225. if(match->type != LinkTypeOut) {
  226. av_log(log_ctx, AV_LOG_ERROR,
  227. "Label \"%s\" appears twice as input!\n", match->name);
  228. return -1;
  229. }
  230. link_to_add = match;
  231. } else {
  232. /* Not in the list, so add it as an input */
  233. link_to_add = av_malloc(sizeof(AVFilterInOut));
  234. link_to_add->name = name;
  235. link_to_add->type = LinkTypeIn;
  236. link_to_add->filter = NULL;
  237. link_to_add->pad_idx = pad;
  238. }
  239. link_to_add->next = *currInputs;
  240. *currInputs = link_to_add;
  241. *buf += consume_whitespace(*buf);
  242. pad++;
  243. }
  244. return pad;
  245. }
  246. static int parse_outputs(const char **buf, AVFilterInOut **currInputs,
  247. AVFilterInOut **openLinks, AVClass *log_ctx)
  248. {
  249. int pad = 0;
  250. while(**buf == '[') {
  251. char *name = parse_link_name(buf, log_ctx);
  252. AVFilterInOut *match;
  253. AVFilterInOut *input = *currInputs;
  254. *currInputs = (*currInputs)->next;
  255. if(!name)
  256. return -1;
  257. /* First check if the label is not in the openLinks list */
  258. match = extract_inout(name, openLinks);
  259. if(match) {
  260. /* A label of a open link. Link it. */
  261. if(match->type != LinkTypeIn) {
  262. av_log(log_ctx, AV_LOG_ERROR,
  263. "Label \"%s\" appears twice as output!\n", match->name);
  264. return -1;
  265. }
  266. if(link_filter(input->filter, input->pad_idx,
  267. match->filter, match->pad_idx, log_ctx) < 0)
  268. return -1;
  269. av_free(match);
  270. av_free(input);
  271. } else {
  272. /* Not in the list, so add the first input as a openLink */
  273. input->next = *openLinks;
  274. input->type = LinkTypeOut;
  275. input->name = name;
  276. *openLinks = input;
  277. }
  278. *buf += consume_whitespace(*buf);
  279. pad++;
  280. }
  281. return pad;
  282. }
  283. int avfilter_parse_graph(AVFilterGraph *graph, const char *filters,
  284. AVFilterInOut *openLinks, AVClass *log_ctx)
  285. {
  286. int index = 0;
  287. char chr = 0;
  288. int pad = 0;
  289. AVFilterInOut *currInputs = NULL;
  290. do {
  291. AVFilterContext *filter;
  292. filters += consume_whitespace(filters);
  293. pad = parse_inputs(&filters, &currInputs, &openLinks, log_ctx);
  294. if(pad < 0)
  295. goto fail;
  296. filter = parse_filter(&filters, graph, index, log_ctx);
  297. if(!filter)
  298. goto fail;
  299. if(filter->input_count == 1 && !currInputs && !index) {
  300. // First input can be ommitted if it is "[in]"
  301. const char *tmp = "[in]";
  302. pad = parse_inputs(&tmp, &currInputs, &openLinks, log_ctx);
  303. if(pad < 0)
  304. goto fail;
  305. }
  306. if(link_filter_inouts(filter, &currInputs, &openLinks, log_ctx) < 0)
  307. goto fail;
  308. pad = parse_outputs(&filters, &currInputs, &openLinks, log_ctx);
  309. if(pad < 0)
  310. goto fail;
  311. filters += consume_whitespace(filters);
  312. chr = *filters++;
  313. if(chr == ';' && currInputs) {
  314. av_log(log_ctx, AV_LOG_ERROR,
  315. "Could not find a output to link when parsing \"%s\"\n",
  316. filters - 1);
  317. goto fail;
  318. }
  319. index++;
  320. } while(chr == ',' || chr == ';');
  321. if(openLinks && !strcmp(openLinks->name, "out") && currInputs) {
  322. // Last output can be ommitted if it is "[out]"
  323. const char *tmp = "[out]";
  324. if(parse_outputs(&tmp, &currInputs, &openLinks, log_ctx) < 0)
  325. goto fail;
  326. }
  327. return 0;
  328. fail:
  329. avfilter_destroy_graph(graph);
  330. free_inout(openLinks);
  331. free_inout(currInputs);
  332. return -1;
  333. }