graphparser.c 12 KB

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