graphparser.c 12 KB

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