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.

411 lines
11KB

  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. * @arg 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. if(!(filterdef = avfilter_get_by_name(name))) {
  114. av_log(log_ctx, AV_LOG_ERROR,
  115. "no such filter: '%s'\n", name);
  116. return NULL;
  117. }
  118. if(!(filt = avfilter_open(filterdef, inst_name))) {
  119. av_log(log_ctx, AV_LOG_ERROR,
  120. "error creating filter '%s'\n", name);
  121. return NULL;
  122. }
  123. if(avfilter_graph_add_filter(ctx, filt) < 0)
  124. return NULL;
  125. if(avfilter_init_filter(filt, args, NULL)) {
  126. av_log(log_ctx, AV_LOG_ERROR,
  127. "error initializing filter '%s' with args '%s'\n", name, args);
  128. return NULL;
  129. }
  130. return filt;
  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,
  140. int index, AVClass *log_ctx)
  141. {
  142. char *opts;
  143. char *name = consume_string(buf);
  144. if(**buf == '=') {
  145. (*buf)++;
  146. opts = consume_string(buf);
  147. } else {
  148. opts = NULL;
  149. }
  150. return create_filter(graph, index, name, opts, log_ctx);
  151. }
  152. static void free_inout(AVFilterInOut *head)
  153. {
  154. while(head) {
  155. AVFilterInOut *next = head->next;
  156. av_free(head);
  157. head = next;
  158. }
  159. }
  160. static AVFilterInOut *extract_inout(const char *label, AVFilterInOut **links)
  161. {
  162. AVFilterInOut *ret;
  163. while(*links && strcmp((*links)->name, label))
  164. links = &((*links)->next);
  165. ret = *links;
  166. if(ret)
  167. *links = ret->next;
  168. return ret;
  169. }
  170. static int link_filter_inouts(AVFilterContext *filter,
  171. AVFilterInOut **currInputs,
  172. AVFilterInOut **openLinks, AVClass *log_ctx)
  173. {
  174. int pad = 0;
  175. pad = filter->input_count;
  176. while(pad--) {
  177. AVFilterInOut *p = *currInputs;
  178. *currInputs = (*currInputs)->next;
  179. if(!p) {
  180. av_log(log_ctx, AV_LOG_ERROR,
  181. "Not enough inputs specified for the \"%s\" filter.\n",
  182. filter->name);
  183. return -1;
  184. }
  185. if(p->filter) {
  186. if(link_filter(p->filter, p->pad_idx, filter, pad, log_ctx))
  187. return -1;
  188. av_free(p);
  189. } else {
  190. p->filter = filter;
  191. p->pad_idx = pad;
  192. p->next = *openLinks;
  193. *openLinks = p;
  194. }
  195. }
  196. if(*currInputs) {
  197. av_log(log_ctx, AV_LOG_ERROR,
  198. "Too many inputs specified for the \"%s\" filter.\n",
  199. filter->name);
  200. return -1;
  201. }
  202. pad = filter->output_count;
  203. while(pad--) {
  204. AVFilterInOut *currlinkn = av_malloc(sizeof(AVFilterInOut));
  205. currlinkn->name = NULL;
  206. currlinkn->type = LinkTypeOut;
  207. currlinkn->filter = filter;
  208. currlinkn->pad_idx = pad;
  209. currlinkn->next = *currInputs;
  210. *currInputs = currlinkn;
  211. }
  212. return 0;
  213. }
  214. static int parse_inputs(const char **buf, AVFilterInOut **currInputs,
  215. AVFilterInOut **openLinks, AVClass *log_ctx)
  216. {
  217. int pad = 0;
  218. while(**buf == '[') {
  219. char *name = parse_link_name(buf, log_ctx);
  220. AVFilterInOut *link_to_add;
  221. AVFilterInOut *match;
  222. if(!name)
  223. return -1;
  224. /* First check if the label is not in the openLinks list */
  225. match = extract_inout(name, openLinks);
  226. if(match) {
  227. /* A label of a open link. Make it one of the inputs of the next
  228. filter */
  229. if(match->type != LinkTypeOut) {
  230. av_log(log_ctx, AV_LOG_ERROR,
  231. "Label \"%s\" appears twice as input!\n", match->name);
  232. return -1;
  233. }
  234. link_to_add = match;
  235. } else {
  236. /* Not in the list, so add it as an input */
  237. link_to_add = av_malloc(sizeof(AVFilterInOut));
  238. link_to_add->name = name;
  239. link_to_add->type = LinkTypeIn;
  240. link_to_add->filter = NULL;
  241. link_to_add->pad_idx = pad;
  242. }
  243. link_to_add->next = *currInputs;
  244. *currInputs = link_to_add;
  245. *buf += consume_whitespace(*buf);
  246. pad++;
  247. }
  248. return pad;
  249. }
  250. static int parse_outputs(const char **buf, AVFilterInOut **currInputs,
  251. AVFilterInOut **openLinks, AVClass *log_ctx)
  252. {
  253. int pad = 0;
  254. while(**buf == '[') {
  255. char *name = parse_link_name(buf, log_ctx);
  256. AVFilterInOut *match;
  257. AVFilterInOut *input = *currInputs;
  258. *currInputs = (*currInputs)->next;
  259. if(!name)
  260. return -1;
  261. /* First check if the label is not in the openLinks list */
  262. match = extract_inout(name, openLinks);
  263. if(match) {
  264. /* A label of a open link. Link it. */
  265. if(match->type != LinkTypeIn) {
  266. av_log(log_ctx, AV_LOG_ERROR,
  267. "Label \"%s\" appears twice as output!\n", match->name);
  268. return -1;
  269. }
  270. if(link_filter(input->filter, input->pad_idx,
  271. match->filter, match->pad_idx, log_ctx) < 0)
  272. return -1;
  273. av_free(match);
  274. av_free(input);
  275. } else {
  276. /* Not in the list, so add the first input as a openLink */
  277. input->next = *openLinks;
  278. input->type = LinkTypeOut;
  279. input->name = name;
  280. *openLinks = input;
  281. }
  282. *buf += consume_whitespace(*buf);
  283. pad++;
  284. }
  285. return pad;
  286. }
  287. /**
  288. * Parse a string describing a filter graph.
  289. */
  290. int avfilter_parse_graph(AVFilterGraph *graph, const char *filters,
  291. AVFilterInOut *openLinks, AVClass *log_ctx)
  292. {
  293. int index = 0;
  294. char chr = 0;
  295. int pad = 0;
  296. AVFilterInOut *currInputs = NULL;
  297. do {
  298. AVFilterContext *filter;
  299. filters += consume_whitespace(filters);
  300. pad = parse_inputs(&filters, &currInputs, &openLinks, log_ctx);
  301. if(pad < 0)
  302. goto fail;
  303. if(!(filter = parse_filter(&filters, graph, index, log_ctx)))
  304. goto fail;
  305. if(filter->input_count == 1 && !currInputs && !index) {
  306. // First input can be ommitted if it is "[in]"
  307. const char *tmp = "[in]";
  308. pad = parse_inputs(&tmp, &currInputs, &openLinks, log_ctx);
  309. if(pad < 0)
  310. goto fail;
  311. }
  312. if(link_filter_inouts(filter, &currInputs, &openLinks, log_ctx) < 0)
  313. goto fail;
  314. pad = parse_outputs(&filters, &currInputs, &openLinks, log_ctx);
  315. if(pad < 0)
  316. goto fail;
  317. filters += consume_whitespace(filters);
  318. chr = *filters++;
  319. if(chr == ';' && currInputs) {
  320. av_log(log_ctx, AV_LOG_ERROR,
  321. "Could not find a output to link when parsing \"%s\"\n",
  322. filters - 1);
  323. goto fail;
  324. }
  325. index++;
  326. } while(chr == ',' || chr == ';');
  327. if(openLinks && !strcmp(openLinks->name, "out") && currInputs) {
  328. // Last output can be ommitted if it is "[out]"
  329. const char *tmp = "[out]";
  330. if(parse_outputs(&tmp, &currInputs, &openLinks, log_ctx) < 0)
  331. goto fail;
  332. }
  333. return 0;
  334. fail:
  335. avfilter_destroy_graph(graph);
  336. free_inout(openLinks);
  337. free_inout(currInputs);
  338. return -1;
  339. }