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 AVFilterContext *create_filter(AVFilterGraph *ctx, int index,
  28. const char *name, const char *args,
  29. AVClass *log_ctx)
  30. {
  31. AVFilterContext *filt;
  32. AVFilter *filterdef;
  33. char inst_name[30];
  34. snprintf(inst_name, sizeof(inst_name), "Parsed filter %d", index);
  35. if(!(filterdef = avfilter_get_by_name(name))) {
  36. av_log(log_ctx, AV_LOG_ERROR,
  37. "no such filter: '%s'\n", name);
  38. return NULL;
  39. }
  40. if(!(filt = avfilter_open(filterdef, inst_name))) {
  41. av_log(log_ctx, AV_LOG_ERROR,
  42. "error creating filter '%s'\n", name);
  43. return NULL;
  44. }
  45. if(avfilter_graph_add_filter(ctx, filt) < 0)
  46. return NULL;
  47. if(avfilter_init_filter(filt, args, NULL)) {
  48. av_log(log_ctx, AV_LOG_ERROR,
  49. "error initializing filter '%s' with args '%s'\n", name, args);
  50. return NULL;
  51. }
  52. return filt;
  53. }
  54. static int link_filter(AVFilterContext *src, int srcpad,
  55. AVFilterContext *dst, int dstpad,
  56. AVClass *log_ctx)
  57. {
  58. if(avfilter_link(src, srcpad, dst, dstpad)) {
  59. av_log(log_ctx, AV_LOG_ERROR,
  60. "cannot create the link %s:%d -> %s:%d\n",
  61. src->filter->name, srcpad, dst->filter->name, dstpad);
  62. return -1;
  63. }
  64. return 0;
  65. }
  66. static void consume_whitespace(const char **buf)
  67. {
  68. *buf += strspn(*buf, " \n\t");
  69. }
  70. /**
  71. * Consumes a string from *buf.
  72. * @return a copy of the consumed string, which should be free'd after use
  73. */
  74. static char *consume_string(const char **buf)
  75. {
  76. char *out = av_malloc(strlen(*buf) + 1);
  77. char *ret = out;
  78. consume_whitespace(buf);
  79. do{
  80. char c = *(*buf)++;
  81. switch (c) {
  82. case '\\':
  83. *out++ = *(*buf)++;
  84. break;
  85. case '\'':
  86. while(**buf && **buf != '\'')
  87. *out++ = *(*buf)++;
  88. if(**buf) (*buf)++;
  89. break;
  90. case 0:
  91. case ']':
  92. case '[':
  93. case '=':
  94. case ',':
  95. case ';':
  96. case ' ':
  97. case '\n':
  98. *out++ = 0;
  99. break;
  100. default:
  101. *out++ = c;
  102. }
  103. } while(out[-1]);
  104. (*buf)--;
  105. consume_whitespace(buf);
  106. return ret;
  107. }
  108. /**
  109. * Parse "[linkname]"
  110. * @arg name a pointer (that need to be free'd after use) to the name between
  111. * parenthesis
  112. */
  113. static void parse_link_name(const char **buf, char **name, AVClass *log_ctx)
  114. {
  115. const char *start = *buf;
  116. (*buf)++;
  117. *name = consume_string(buf);
  118. if(!*name[0]) {
  119. av_log(log_ctx, AV_LOG_ERROR,
  120. "Bad (empty?) label found in the following: \"%s\".\n", start);
  121. goto fail;
  122. }
  123. if(*(*buf)++ != ']') {
  124. av_log(log_ctx, AV_LOG_ERROR,
  125. "Mismatched '[' found in the following: \"%s\".\n", start);
  126. fail:
  127. av_freep(name);
  128. }
  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. }