graphparser.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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 "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. void *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, void *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 ctx the filtergraph context
  76. * @param put here a filter context in case of successful creation and configuration, NULL otherwise.
  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, void *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. ctx->scale_sws_opts) {
  109. snprintf(tmp_args, sizeof(tmp_args), "%s:%s",
  110. args, ctx->scale_sws_opts);
  111. args = tmp_args;
  112. }
  113. if ((ret = avfilter_init_filter(*filt_ctx, args, NULL)) < 0) {
  114. av_log(log_ctx, AV_LOG_ERROR,
  115. "Error initializing filter '%s' with args '%s'\n", filt_name, args);
  116. return ret;
  117. }
  118. return 0;
  119. }
  120. /**
  121. * Parse a string of the form FILTER_NAME[=PARAMS], and create a
  122. * corresponding filter instance which is added to graph with
  123. * create_filter().
  124. *
  125. * @param filt_ctx put here a pointer to the created filter context on
  126. * success, NULL otherwise
  127. * @param buf pointer to the buffer to parse, *buf will be updated to
  128. * point to the char next after the parsed string
  129. * @param index an index which is assigned to the created filter
  130. * instance, and which is supposed to be unique for each filter
  131. * instance added to the filtergraph
  132. * @return 0 in case of success, a negative AVERROR code otherwise
  133. */
  134. static int parse_filter(AVFilterContext **filt_ctx, const char **buf, AVFilterGraph *graph,
  135. int index, void *log_ctx)
  136. {
  137. char *opts = NULL;
  138. char *name = av_get_token(buf, "=,;[\n");
  139. int ret;
  140. if (**buf == '=') {
  141. (*buf)++;
  142. opts = av_get_token(buf, "[],;\n");
  143. }
  144. ret = create_filter(filt_ctx, graph, index, name, opts, log_ctx);
  145. av_free(name);
  146. av_free(opts);
  147. return ret;
  148. }
  149. AVFilterInOut *avfilter_inout_alloc(void)
  150. {
  151. return av_mallocz(sizeof(AVFilterInOut));
  152. }
  153. void avfilter_inout_free(AVFilterInOut **inout)
  154. {
  155. while (*inout) {
  156. AVFilterInOut *next = (*inout)->next;
  157. av_freep(&(*inout)->name);
  158. av_freep(inout);
  159. *inout = next;
  160. }
  161. }
  162. static AVFilterInOut *extract_inout(const char *label, AVFilterInOut **links)
  163. {
  164. AVFilterInOut *ret;
  165. while (*links && strcmp((*links)->name, label))
  166. links = &((*links)->next);
  167. ret = *links;
  168. if (ret)
  169. *links = ret->next;
  170. return ret;
  171. }
  172. static void insert_inout(AVFilterInOut **inouts, AVFilterInOut *element)
  173. {
  174. element->next = *inouts;
  175. *inouts = element;
  176. }
  177. static int link_filter_inouts(AVFilterContext *filt_ctx,
  178. AVFilterInOut **curr_inputs,
  179. AVFilterInOut **open_inputs, void *log_ctx)
  180. {
  181. int pad = filt_ctx->input_count, ret;
  182. while (pad--) {
  183. AVFilterInOut *p = *curr_inputs;
  184. if (!p) {
  185. av_log(log_ctx, AV_LOG_ERROR,
  186. "Not enough inputs specified for the \"%s\" filter.\n",
  187. filt_ctx->filter->name);
  188. return AVERROR(EINVAL);
  189. }
  190. *curr_inputs = (*curr_inputs)->next;
  191. if (p->filter_ctx) {
  192. if ((ret = link_filter(p->filter_ctx, p->pad_idx, filt_ctx, pad, log_ctx)) < 0)
  193. return ret;
  194. av_free(p->name);
  195. av_free(p);
  196. } else {
  197. p->filter_ctx = filt_ctx;
  198. p->pad_idx = pad;
  199. insert_inout(open_inputs, p);
  200. }
  201. }
  202. if (*curr_inputs) {
  203. av_log(log_ctx, AV_LOG_ERROR,
  204. "Too many inputs specified for the \"%s\" filter.\n",
  205. filt_ctx->filter->name);
  206. return AVERROR(EINVAL);
  207. }
  208. pad = filt_ctx->output_count;
  209. while (pad--) {
  210. AVFilterInOut *currlinkn = av_mallocz(sizeof(AVFilterInOut));
  211. if (!currlinkn)
  212. return AVERROR(ENOMEM);
  213. currlinkn->filter_ctx = filt_ctx;
  214. currlinkn->pad_idx = pad;
  215. insert_inout(curr_inputs, currlinkn);
  216. }
  217. return 0;
  218. }
  219. static int parse_inputs(const char **buf, AVFilterInOut **curr_inputs,
  220. AVFilterInOut **open_outputs, void *log_ctx)
  221. {
  222. int pad = 0;
  223. while (**buf == '[') {
  224. char *name = parse_link_name(buf, log_ctx);
  225. AVFilterInOut *match;
  226. if (!name)
  227. return AVERROR(EINVAL);
  228. /* First check if the label is not in the open_outputs list */
  229. match = extract_inout(name, open_outputs);
  230. if (match) {
  231. av_free(name);
  232. } else {
  233. /* Not in the list, so add it as an input */
  234. if (!(match = av_mallocz(sizeof(AVFilterInOut))))
  235. return AVERROR(ENOMEM);
  236. match->name = name;
  237. match->pad_idx = pad;
  238. }
  239. insert_inout(curr_inputs, match);
  240. *buf += strspn(*buf, WHITESPACES);
  241. pad++;
  242. }
  243. return pad;
  244. }
  245. static int parse_outputs(const char **buf, AVFilterInOut **curr_inputs,
  246. AVFilterInOut **open_inputs,
  247. AVFilterInOut **open_outputs, void *log_ctx)
  248. {
  249. int ret, pad = 0;
  250. while (**buf == '[') {
  251. char *name = parse_link_name(buf, log_ctx);
  252. AVFilterInOut *match;
  253. AVFilterInOut *input = *curr_inputs;
  254. if (!input) {
  255. av_log(log_ctx, AV_LOG_ERROR,
  256. "No output pad can be associated to link label '%s'.\n",
  257. name);
  258. return AVERROR(EINVAL);
  259. }
  260. *curr_inputs = (*curr_inputs)->next;
  261. if (!name)
  262. return AVERROR(EINVAL);
  263. /* First check if the label is not in the open_inputs list */
  264. match = extract_inout(name, open_inputs);
  265. if (match) {
  266. if ((ret = link_filter(input->filter_ctx, input->pad_idx,
  267. match->filter_ctx, match->pad_idx, log_ctx)) < 0)
  268. return ret;
  269. av_free(match->name);
  270. av_free(name);
  271. av_free(match);
  272. av_free(input);
  273. } else {
  274. /* Not in the list, so add the first input as a open_output */
  275. input->name = name;
  276. insert_inout(open_outputs, input);
  277. }
  278. *buf += strspn(*buf, WHITESPACES);
  279. pad++;
  280. }
  281. return pad;
  282. }
  283. int avfilter_graph_parse(AVFilterGraph *graph, const char *filters,
  284. AVFilterInOut **open_inputs, AVFilterInOut **open_outputs,
  285. void *log_ctx)
  286. {
  287. int index = 0, ret;
  288. char chr = 0;
  289. AVFilterInOut *curr_inputs = NULL;
  290. do {
  291. AVFilterContext *filter;
  292. const char *filterchain = filters;
  293. filters += strspn(filters, WHITESPACES);
  294. if ((ret = parse_inputs(&filters, &curr_inputs, open_outputs, log_ctx)) < 0)
  295. goto fail;
  296. if ((ret = parse_filter(&filter, &filters, graph, index, log_ctx)) < 0)
  297. goto fail;
  298. if (filter->input_count == 1 && !curr_inputs && !index) {
  299. /* First input can be omitted if it is "[in]" */
  300. const char *tmp = "[in]";
  301. if ((ret = parse_inputs(&tmp, &curr_inputs, open_outputs, log_ctx)) < 0)
  302. goto fail;
  303. }
  304. if ((ret = link_filter_inouts(filter, &curr_inputs, open_inputs, log_ctx)) < 0)
  305. goto fail;
  306. if ((ret = parse_outputs(&filters, &curr_inputs, open_inputs, open_outputs,
  307. log_ctx)) < 0)
  308. goto fail;
  309. filters += strspn(filters, WHITESPACES);
  310. chr = *filters++;
  311. if (chr == ';' && curr_inputs) {
  312. av_log(log_ctx, AV_LOG_ERROR,
  313. "Invalid filterchain containing an unlabelled output pad: \"%s\"\n",
  314. filterchain);
  315. ret = AVERROR(EINVAL);
  316. goto fail;
  317. }
  318. index++;
  319. } while (chr == ',' || chr == ';');
  320. if (chr) {
  321. av_log(log_ctx, AV_LOG_ERROR,
  322. "Unable to parse graph description substring: \"%s\"\n",
  323. filters - 1);
  324. ret = AVERROR(EINVAL);
  325. goto fail;
  326. }
  327. if (open_inputs && *open_inputs && !strcmp((*open_inputs)->name, "out") && curr_inputs) {
  328. /* Last output can be omitted if it is "[out]" */
  329. const char *tmp = "[out]";
  330. if ((ret = parse_outputs(&tmp, &curr_inputs, open_inputs, open_outputs,
  331. log_ctx)) < 0)
  332. goto fail;
  333. }
  334. return 0;
  335. fail:
  336. for (; graph->filter_count > 0; graph->filter_count--)
  337. avfilter_free(graph->filters[graph->filter_count - 1]);
  338. av_freep(&graph->filters);
  339. avfilter_inout_free(open_inputs);
  340. avfilter_inout_free(open_outputs);
  341. avfilter_inout_free(&curr_inputs);
  342. return ret;
  343. }