graphparser.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. /*
  2. * filter graph parser
  3. * Copyright (c) 2008 Vitor Sessak
  4. * Copyright (c) 2007 Bobby Bingham
  5. *
  6. * This file is part of Libav.
  7. *
  8. * Libav 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. * Libav 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 Libav; 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 "libavutil/avstring.h"
  25. #include "avfilter.h"
  26. #include "avfiltergraph.h"
  27. #define WHITESPACES " \n\t"
  28. /**
  29. * Link two filters together.
  30. *
  31. * @see avfilter_link()
  32. */
  33. static int link_filter(AVFilterContext *src, int srcpad,
  34. AVFilterContext *dst, int dstpad,
  35. AVClass *log_ctx)
  36. {
  37. int ret;
  38. if ((ret = avfilter_link(src, srcpad, dst, dstpad))) {
  39. av_log(log_ctx, AV_LOG_ERROR,
  40. "Cannot create the link %s:%d -> %s:%d\n",
  41. src->filter->name, srcpad, dst->filter->name, dstpad);
  42. return ret;
  43. }
  44. return 0;
  45. }
  46. /**
  47. * Parse the name of a link, which has the format "[linkname]".
  48. *
  49. * @return a pointer (that need to be freed after use) to the name
  50. * between parenthesis
  51. */
  52. static char *parse_link_name(const char **buf, AVClass *log_ctx)
  53. {
  54. const char *start = *buf;
  55. char *name;
  56. (*buf)++;
  57. name = av_get_token(buf, "]");
  58. if (!name[0]) {
  59. av_log(log_ctx, AV_LOG_ERROR,
  60. "Bad (empty?) label found in the following: \"%s\".\n", start);
  61. goto fail;
  62. }
  63. if (*(*buf)++ != ']') {
  64. av_log(log_ctx, AV_LOG_ERROR,
  65. "Mismatched '[' found in the following: \"%s\".\n", start);
  66. fail:
  67. av_freep(&name);
  68. }
  69. return name;
  70. }
  71. /**
  72. * Create an instance of a filter, initialize and insert it in the
  73. * filtergraph in *ctx.
  74. *
  75. * @param filt_ctx put here a filter context in case of successful creation and configuration, NULL otherwise.
  76. * @param ctx the filtergraph context
  77. * @param index an index which is supposed to be unique for each filter instance added to the filtergraph
  78. * @param filt_name the name of the filter to create
  79. * @param args the arguments provided to the filter during its initialization
  80. * @param log_ctx the log context to use
  81. * @return 0 in case of success, a negative AVERROR code otherwise
  82. */
  83. static int create_filter(AVFilterContext **filt_ctx, AVFilterGraph *ctx, int index,
  84. const char *filt_name, const char *args, AVClass *log_ctx)
  85. {
  86. AVFilter *filt;
  87. char inst_name[30];
  88. char tmp_args[256];
  89. int ret;
  90. snprintf(inst_name, sizeof(inst_name), "Parsed filter %d %s", index, filt_name);
  91. filt = avfilter_get_by_name(filt_name);
  92. if (!filt) {
  93. av_log(log_ctx, AV_LOG_ERROR,
  94. "No such filter: '%s'\n", filt_name);
  95. return AVERROR(EINVAL);
  96. }
  97. ret = avfilter_open(filt_ctx, filt, inst_name);
  98. if (!*filt_ctx) {
  99. av_log(log_ctx, AV_LOG_ERROR,
  100. "Error creating filter '%s'\n", filt_name);
  101. return ret;
  102. }
  103. if ((ret = avfilter_graph_add_filter(ctx, *filt_ctx)) < 0) {
  104. avfilter_free(*filt_ctx);
  105. return ret;
  106. }
  107. if (!strcmp(filt_name, "scale") && args && !strstr(args, "flags")) {
  108. snprintf(tmp_args, sizeof(tmp_args), "%s:%s",
  109. args, ctx->scale_sws_opts);
  110. args = tmp_args;
  111. }
  112. if ((ret = avfilter_init_filter(*filt_ctx, args, NULL)) < 0) {
  113. av_log(log_ctx, AV_LOG_ERROR,
  114. "Error initializing filter '%s' with args '%s'\n", filt_name, args);
  115. return ret;
  116. }
  117. return 0;
  118. }
  119. /**
  120. * Parse a string of the form FILTER_NAME[=PARAMS], and create a
  121. * corresponding filter instance which is added to graph with
  122. * create_filter().
  123. *
  124. * @param filt_ctx Pointer that is set to the created and configured filter
  125. * context on success, set to NULL on failure.
  126. * @param filt_ctx put here a pointer to the created filter context on
  127. * success, NULL otherwise
  128. * @param buf pointer to the buffer to parse, *buf will be updated to
  129. * point to the char next after the parsed string
  130. * @param index an index which is assigned to the created filter
  131. * instance, and which is supposed to be unique for each filter
  132. * instance added to the filtergraph
  133. * @return 0 in case of success, a negative AVERROR code otherwise
  134. */
  135. static int parse_filter(AVFilterContext **filt_ctx, const char **buf, AVFilterGraph *graph,
  136. int index, AVClass *log_ctx)
  137. {
  138. char *opts = NULL;
  139. char *name = av_get_token(buf, "=,;[\n");
  140. int ret;
  141. if (**buf == '=') {
  142. (*buf)++;
  143. opts = av_get_token(buf, "[],;\n");
  144. }
  145. ret = create_filter(filt_ctx, graph, index, name, opts, log_ctx);
  146. av_free(name);
  147. av_free(opts);
  148. return ret;
  149. }
  150. static void free_inout(AVFilterInOut *head)
  151. {
  152. while (head) {
  153. AVFilterInOut *next = head->next;
  154. av_free(head->name);
  155. av_free(head);
  156. head = next;
  157. }
  158. }
  159. static AVFilterInOut *extract_inout(const char *label, AVFilterInOut **links)
  160. {
  161. AVFilterInOut *ret;
  162. while (*links && strcmp((*links)->name, label))
  163. links = &((*links)->next);
  164. ret = *links;
  165. if (ret)
  166. *links = ret->next;
  167. return ret;
  168. }
  169. static void insert_inout(AVFilterInOut **inouts, AVFilterInOut *element)
  170. {
  171. element->next = *inouts;
  172. *inouts = element;
  173. }
  174. static int link_filter_inouts(AVFilterContext *filt_ctx,
  175. AVFilterInOut **curr_inputs,
  176. AVFilterInOut **open_inputs, AVClass *log_ctx)
  177. {
  178. int pad = filt_ctx->input_count, ret;
  179. while (pad--) {
  180. AVFilterInOut *p = *curr_inputs;
  181. if (!p) {
  182. av_log(log_ctx, AV_LOG_ERROR,
  183. "Not enough inputs specified for the \"%s\" filter.\n",
  184. filt_ctx->filter->name);
  185. return AVERROR(EINVAL);
  186. }
  187. *curr_inputs = (*curr_inputs)->next;
  188. if (p->filter_ctx) {
  189. if ((ret = link_filter(p->filter_ctx, p->pad_idx, filt_ctx, pad, log_ctx)) < 0)
  190. return ret;
  191. av_free(p->name);
  192. av_free(p);
  193. } else {
  194. p->filter_ctx = filt_ctx;
  195. p->pad_idx = pad;
  196. insert_inout(open_inputs, p);
  197. }
  198. }
  199. if (*curr_inputs) {
  200. av_log(log_ctx, AV_LOG_ERROR,
  201. "Too many inputs specified for the \"%s\" filter.\n",
  202. filt_ctx->filter->name);
  203. return AVERROR(EINVAL);
  204. }
  205. pad = filt_ctx->output_count;
  206. while (pad--) {
  207. AVFilterInOut *currlinkn = av_mallocz(sizeof(AVFilterInOut));
  208. if (!currlinkn)
  209. return AVERROR(ENOMEM);
  210. currlinkn->filter_ctx = filt_ctx;
  211. currlinkn->pad_idx = pad;
  212. insert_inout(curr_inputs, currlinkn);
  213. }
  214. return 0;
  215. }
  216. static int parse_inputs(const char **buf, AVFilterInOut **curr_inputs,
  217. AVFilterInOut **open_outputs, AVClass *log_ctx)
  218. {
  219. int pad = 0;
  220. while (**buf == '[') {
  221. char *name = parse_link_name(buf, log_ctx);
  222. AVFilterInOut *match;
  223. if (!name)
  224. return AVERROR(EINVAL);
  225. /* First check if the label is not in the open_outputs list */
  226. match = extract_inout(name, open_outputs);
  227. if (match) {
  228. av_free(name);
  229. } else {
  230. /* Not in the list, so add it as an input */
  231. if (!(match = av_mallocz(sizeof(AVFilterInOut))))
  232. return AVERROR(ENOMEM);
  233. match->name = name;
  234. match->pad_idx = pad;
  235. }
  236. insert_inout(curr_inputs, match);
  237. *buf += strspn(*buf, WHITESPACES);
  238. pad++;
  239. }
  240. return pad;
  241. }
  242. static int parse_outputs(const char **buf, AVFilterInOut **curr_inputs,
  243. AVFilterInOut **open_inputs,
  244. AVFilterInOut **open_outputs, AVClass *log_ctx)
  245. {
  246. int ret, pad = 0;
  247. while (**buf == '[') {
  248. char *name = parse_link_name(buf, log_ctx);
  249. AVFilterInOut *match;
  250. AVFilterInOut *input = *curr_inputs;
  251. if (!input) {
  252. av_log(log_ctx, AV_LOG_ERROR,
  253. "No output pad can be associated to link label '%s'.\n",
  254. name);
  255. return AVERROR(EINVAL);
  256. }
  257. *curr_inputs = (*curr_inputs)->next;
  258. if (!name)
  259. return AVERROR(EINVAL);
  260. /* First check if the label is not in the open_inputs list */
  261. match = extract_inout(name, open_inputs);
  262. if (match) {
  263. if ((ret = link_filter(input->filter_ctx, input->pad_idx,
  264. match->filter_ctx, match->pad_idx, log_ctx)) < 0)
  265. return ret;
  266. av_free(match->name);
  267. av_free(name);
  268. av_free(match);
  269. av_free(input);
  270. } else {
  271. /* Not in the list, so add the first input as a open_output */
  272. input->name = name;
  273. insert_inout(open_outputs, input);
  274. }
  275. *buf += strspn(*buf, WHITESPACES);
  276. pad++;
  277. }
  278. return pad;
  279. }
  280. int avfilter_graph_parse(AVFilterGraph *graph, const char *filters,
  281. AVFilterInOut *open_inputs,
  282. AVFilterInOut *open_outputs, AVClass *log_ctx)
  283. {
  284. int index = 0, ret;
  285. char chr = 0;
  286. AVFilterInOut *curr_inputs = NULL;
  287. do {
  288. AVFilterContext *filter;
  289. const char *filterchain = filters;
  290. filters += strspn(filters, WHITESPACES);
  291. if ((ret = parse_inputs(&filters, &curr_inputs, &open_outputs, log_ctx)) < 0)
  292. goto fail;
  293. if ((ret = parse_filter(&filter, &filters, graph, index, log_ctx)) < 0)
  294. goto fail;
  295. if (filter->input_count == 1 && !curr_inputs && !index) {
  296. /* First input can be omitted if it is "[in]" */
  297. const char *tmp = "[in]";
  298. if ((ret = parse_inputs(&tmp, &curr_inputs, &open_outputs, log_ctx)) < 0)
  299. goto fail;
  300. }
  301. if ((ret = link_filter_inouts(filter, &curr_inputs, &open_inputs, log_ctx)) < 0)
  302. goto fail;
  303. if ((ret = parse_outputs(&filters, &curr_inputs, &open_inputs, &open_outputs,
  304. log_ctx)) < 0)
  305. goto fail;
  306. filters += strspn(filters, WHITESPACES);
  307. chr = *filters++;
  308. if (chr == ';' && curr_inputs) {
  309. av_log(log_ctx, AV_LOG_ERROR,
  310. "Invalid filterchain containing an unlabelled output pad: \"%s\"\n",
  311. filterchain);
  312. ret = AVERROR(EINVAL);
  313. goto fail;
  314. }
  315. index++;
  316. } while (chr == ',' || chr == ';');
  317. if (chr) {
  318. av_log(log_ctx, AV_LOG_ERROR,
  319. "Unable to parse graph description substring: \"%s\"\n",
  320. filters - 1);
  321. ret = AVERROR(EINVAL);
  322. goto fail;
  323. }
  324. if (open_inputs && !strcmp(open_inputs->name, "out") && curr_inputs) {
  325. /* Last output can be omitted if it is "[out]" */
  326. const char *tmp = "[out]";
  327. if ((ret = parse_outputs(&tmp, &curr_inputs, &open_inputs, &open_outputs,
  328. log_ctx)) < 0)
  329. goto fail;
  330. }
  331. return 0;
  332. fail:
  333. for (; graph->filter_count > 0; graph->filter_count--)
  334. avfilter_free(graph->filters[graph->filter_count - 1]);
  335. av_freep(&graph->filters);
  336. free_inout(open_inputs);
  337. free_inout(open_outputs);
  338. free_inout(curr_inputs);
  339. return ret;
  340. }