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.

412 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 void consume_whitespace(const char **buf)
  40. {
  41. *buf += 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. 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. 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 void parse_link_name(const char **buf, char **name, AVClass *log_ctx)
  87. {
  88. const char *start = *buf;
  89. (*buf)++;
  90. *name = consume_string(buf);
  91. if(!*name[0]) {
  92. av_log(log_ctx, AV_LOG_ERROR,
  93. "Bad (empty?) label found in the following: \"%s\".\n", start);
  94. goto fail;
  95. }
  96. if(*(*buf)++ != ']') {
  97. av_log(log_ctx, AV_LOG_ERROR,
  98. "Mismatched '[' found in the following: \"%s\".\n", start);
  99. fail:
  100. av_freep(name);
  101. }
  102. }
  103. static AVFilterContext *create_filter(AVFilterGraph *ctx, int index,
  104. const char *name, const char *args,
  105. AVClass *log_ctx)
  106. {
  107. AVFilterContext *filt;
  108. AVFilter *filterdef;
  109. char inst_name[30];
  110. snprintf(inst_name, sizeof(inst_name), "Parsed filter %d", index);
  111. if(!(filterdef = avfilter_get_by_name(name))) {
  112. av_log(log_ctx, AV_LOG_ERROR,
  113. "no such filter: '%s'\n", name);
  114. return NULL;
  115. }
  116. if(!(filt = avfilter_open(filterdef, inst_name))) {
  117. av_log(log_ctx, AV_LOG_ERROR,
  118. "error creating filter '%s'\n", name);
  119. return NULL;
  120. }
  121. if(avfilter_graph_add_filter(ctx, filt) < 0)
  122. return NULL;
  123. if(avfilter_init_filter(filt, args, NULL)) {
  124. av_log(log_ctx, AV_LOG_ERROR,
  125. "error initializing filter '%s' with args '%s'\n", name, args);
  126. return NULL;
  127. }
  128. return filt;
  129. }
  130. static void free_inout(AVFilterInOut *head)
  131. {
  132. while(head) {
  133. AVFilterInOut *next = head->next;
  134. av_free(head);
  135. head = next;
  136. }
  137. }
  138. static AVFilterInOut *extract_inout(const char *label, AVFilterInOut **links)
  139. {
  140. AVFilterInOut *ret;
  141. while(*links && strcmp((*links)->name, label))
  142. links = &((*links)->next);
  143. ret = *links;
  144. if(ret)
  145. *links = ret->next;
  146. return ret;
  147. }
  148. static int link_filter_inouts(AVFilterContext *filter,
  149. AVFilterInOut **currInputs,
  150. AVFilterInOut **openLinks, AVClass *log_ctx)
  151. {
  152. int pad = 0;
  153. pad = filter->input_count;
  154. while(pad--) {
  155. AVFilterInOut *p = *currInputs;
  156. *currInputs = (*currInputs)->next;
  157. if(!p) {
  158. av_log(log_ctx, AV_LOG_ERROR,
  159. "Not enough inputs specified for the \"%s\" filter.\n",
  160. filter->name);
  161. return -1;
  162. }
  163. if(p->filter) {
  164. if(link_filter(p->filter, p->pad_idx, filter, pad, log_ctx))
  165. return -1;
  166. av_free(p);
  167. } else {
  168. p->filter = filter;
  169. p->pad_idx = pad;
  170. p->next = *openLinks;
  171. *openLinks = p;
  172. }
  173. }
  174. if(*currInputs) {
  175. av_log(log_ctx, AV_LOG_ERROR,
  176. "Too many inputs specified for the \"%s\" filter.\n",
  177. filter->name);
  178. return -1;
  179. }
  180. pad = filter->output_count;
  181. while(pad--) {
  182. AVFilterInOut *currlinkn = av_malloc(sizeof(AVFilterInOut));
  183. currlinkn->name = NULL;
  184. currlinkn->type = LinkTypeOut;
  185. currlinkn->filter = filter;
  186. currlinkn->pad_idx = pad;
  187. currlinkn->next = *currInputs;
  188. *currInputs = currlinkn;
  189. }
  190. return 0;
  191. }
  192. /**
  193. * Parse "filter=params"
  194. * @arg name a pointer (that need to be free'd after use) to the name of the
  195. * filter
  196. * @arg ars a pointer (that need to be free'd after use) to the args of the
  197. * filter
  198. */
  199. static AVFilterContext *parse_filter(const char **buf, AVFilterGraph *graph,
  200. int index, AVClass *log_ctx)
  201. {
  202. char *opts;
  203. char *name = consume_string(buf);
  204. if(**buf == '=') {
  205. (*buf)++;
  206. opts = consume_string(buf);
  207. } else {
  208. opts = NULL;
  209. }
  210. return create_filter(graph, index, name, opts, log_ctx);
  211. }
  212. static int parse_inputs(const char **buf, AVFilterInOut **currInputs,
  213. AVFilterInOut **openLinks, AVClass *log_ctx)
  214. {
  215. int pad = 0;
  216. while(**buf == '[') {
  217. char *name;
  218. AVFilterInOut *link_to_add;
  219. AVFilterInOut *match;
  220. parse_link_name(buf, &name, log_ctx);
  221. if(!name)
  222. return -1;
  223. /* First check if the label is not in the openLinks list */
  224. match = extract_inout(name, openLinks);
  225. if(match) {
  226. /* A label of a open link. Make it one of the inputs of the next
  227. filter */
  228. if(match->type != LinkTypeOut) {
  229. av_log(log_ctx, AV_LOG_ERROR,
  230. "Label \"%s\" appears twice as input!\n", match->name);
  231. return -1;
  232. }
  233. link_to_add = match;
  234. } else {
  235. /* Not in the list, so add it as an input */
  236. link_to_add = av_malloc(sizeof(AVFilterInOut));
  237. link_to_add->name = name;
  238. link_to_add->type = LinkTypeIn;
  239. link_to_add->filter = NULL;
  240. link_to_add->pad_idx = pad;
  241. }
  242. link_to_add->next = *currInputs;
  243. *currInputs = link_to_add;
  244. consume_whitespace(buf);
  245. pad++;
  246. }
  247. return pad;
  248. }
  249. static int parse_outputs(const char **buf, AVFilterInOut **currInputs,
  250. AVFilterInOut **openLinks, AVClass *log_ctx)
  251. {
  252. int pad = 0;
  253. while(**buf == '[') {
  254. char *name;
  255. AVFilterInOut *match;
  256. AVFilterInOut *input = *currInputs;
  257. *currInputs = (*currInputs)->next;
  258. parse_link_name(buf, &name, log_ctx);
  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. 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. 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. 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. }