graphparser.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  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 <string.h>
  23. #include <stdio.h>
  24. #include "libavutil/avstring.h"
  25. #include "libavutil/mem.h"
  26. #include "avfilter.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)
  59. goto fail;
  60. if (!name[0]) {
  61. av_log(log_ctx, AV_LOG_ERROR,
  62. "Bad (empty?) label found in the following: \"%s\".\n", start);
  63. goto fail;
  64. }
  65. if (*(*buf)++ != ']') {
  66. av_log(log_ctx, AV_LOG_ERROR,
  67. "Mismatched '[' found in the following: \"%s\".\n", start);
  68. fail:
  69. av_freep(&name);
  70. }
  71. return name;
  72. }
  73. #define TMP_ARGS_SIZE 256
  74. static void append_sws_flags(const char **args, const char *sws_opts, char *tmp)
  75. {
  76. int nb_opts = 0;
  77. const char *separator = ":";
  78. const char *opt = *args;
  79. if (strstr(*args, "flags"))
  80. return;
  81. if (strstr(*args, "="))
  82. separator = ":flags=";
  83. while ((opt = strstr(opt, ":")) && *opt) {
  84. av_log(NULL, AV_LOG_INFO, "opts '%s' \n", opt);
  85. if (nb_opts > 2) {
  86. return;
  87. }
  88. nb_opts++;
  89. opt++;
  90. }
  91. opt = strstr(sws_opts, "flags=");
  92. if (opt && strlen(opt) > 6)
  93. opt += 6;
  94. else
  95. opt = sws_opts;
  96. snprintf(tmp, TMP_ARGS_SIZE, "%s%s%s",
  97. *args, separator, opt);
  98. *args = tmp;
  99. }
  100. /**
  101. * Create an instance of a filter, initialize and insert it in the
  102. * filtergraph in *ctx.
  103. *
  104. * @param filt_ctx put here a filter context in case of successful creation and configuration, NULL otherwise.
  105. * @param ctx the filtergraph context
  106. * @param index an index which is supposed to be unique for each filter instance added to the filtergraph
  107. * @param filt_name the name of the filter to create
  108. * @param args the arguments provided to the filter during its initialization
  109. * @param log_ctx the log context to use
  110. * @return 0 in case of success, a negative AVERROR code otherwise
  111. */
  112. static int create_filter(AVFilterContext **filt_ctx, AVFilterGraph *ctx, int index,
  113. const char *filt_name, const char *args, void *log_ctx)
  114. {
  115. AVFilter *filt;
  116. char inst_name[30];
  117. char tmp_args[TMP_ARGS_SIZE];
  118. int ret;
  119. snprintf(inst_name, sizeof(inst_name), "Parsed filter %d %s", index, filt_name);
  120. filt = avfilter_get_by_name(filt_name);
  121. if (!filt) {
  122. av_log(log_ctx, AV_LOG_ERROR,
  123. "No such filter: '%s'\n", filt_name);
  124. return AVERROR(EINVAL);
  125. }
  126. *filt_ctx = avfilter_graph_alloc_filter(ctx, filt, inst_name);
  127. if (!*filt_ctx) {
  128. av_log(log_ctx, AV_LOG_ERROR,
  129. "Error creating filter '%s'\n", filt_name);
  130. return AVERROR(ENOMEM);
  131. }
  132. if (!strcmp(filt_name, "scale") && args &&
  133. ctx->scale_sws_opts) {
  134. append_sws_flags(&args, ctx->scale_sws_opts, tmp_args);
  135. }
  136. ret = avfilter_init_str(*filt_ctx, args);
  137. if (ret < 0) {
  138. av_log(log_ctx, AV_LOG_ERROR,
  139. "Error initializing filter '%s'", filt_name);
  140. if (args)
  141. av_log(log_ctx, AV_LOG_ERROR, " with args '%s'", args);
  142. av_log(log_ctx, AV_LOG_ERROR, "\n");
  143. avfilter_free(*filt_ctx);
  144. return ret;
  145. }
  146. return 0;
  147. }
  148. /**
  149. * Parse a string of the form FILTER_NAME[=PARAMS], and create a
  150. * corresponding filter instance which is added to graph with
  151. * create_filter().
  152. *
  153. * @param filt_ctx Pointer that is set to the created and configured filter
  154. * context on success, set to NULL on failure.
  155. * @param filt_ctx put here a pointer to the created filter context on
  156. * success, NULL otherwise
  157. * @param buf pointer to the buffer to parse, *buf will be updated to
  158. * point to the char next after the parsed string
  159. * @param index an index which is assigned to the created filter
  160. * instance, and which is supposed to be unique for each filter
  161. * instance added to the filtergraph
  162. * @return 0 in case of success, a negative AVERROR code otherwise
  163. */
  164. static int parse_filter(AVFilterContext **filt_ctx, const char **buf, AVFilterGraph *graph,
  165. int index, void *log_ctx)
  166. {
  167. char *opts = NULL;
  168. char *name = av_get_token(buf, "=,;[\n");
  169. int ret;
  170. if (**buf == '=') {
  171. (*buf)++;
  172. opts = av_get_token(buf, "[],;\n");
  173. }
  174. ret = create_filter(filt_ctx, graph, index, name, opts, log_ctx);
  175. av_free(name);
  176. av_free(opts);
  177. return ret;
  178. }
  179. AVFilterInOut *avfilter_inout_alloc(void)
  180. {
  181. return av_mallocz(sizeof(AVFilterInOut));
  182. }
  183. void avfilter_inout_free(AVFilterInOut **inout)
  184. {
  185. while (*inout) {
  186. AVFilterInOut *next = (*inout)->next;
  187. av_freep(&(*inout)->name);
  188. av_freep(inout);
  189. *inout = next;
  190. }
  191. }
  192. static AVFilterInOut *extract_inout(const char *label, AVFilterInOut **links)
  193. {
  194. AVFilterInOut *ret;
  195. while (*links && (!(*links)->name || strcmp((*links)->name, label)))
  196. links = &((*links)->next);
  197. ret = *links;
  198. if (ret) {
  199. *links = ret->next;
  200. ret->next = NULL;
  201. }
  202. return ret;
  203. }
  204. static void insert_inout(AVFilterInOut **inouts, AVFilterInOut *element)
  205. {
  206. element->next = *inouts;
  207. *inouts = element;
  208. }
  209. static void append_inout(AVFilterInOut **inouts, AVFilterInOut **element)
  210. {
  211. while (*inouts && (*inouts)->next)
  212. inouts = &((*inouts)->next);
  213. if (!*inouts)
  214. *inouts = *element;
  215. else
  216. (*inouts)->next = *element;
  217. *element = NULL;
  218. }
  219. static int link_filter_inouts(AVFilterContext *filt_ctx,
  220. AVFilterInOut **curr_inputs,
  221. AVFilterInOut **open_inputs, void *log_ctx)
  222. {
  223. int pad, ret;
  224. for (pad = 0; pad < filt_ctx->nb_inputs; pad++) {
  225. AVFilterInOut *p = *curr_inputs;
  226. if (p) {
  227. *curr_inputs = (*curr_inputs)->next;
  228. p->next = NULL;
  229. } else if (!(p = av_mallocz(sizeof(*p))))
  230. return AVERROR(ENOMEM);
  231. if (p->filter_ctx) {
  232. ret = link_filter(p->filter_ctx, p->pad_idx, filt_ctx, pad, log_ctx);
  233. av_free(p->name);
  234. av_free(p);
  235. if (ret < 0)
  236. return ret;
  237. } else {
  238. p->filter_ctx = filt_ctx;
  239. p->pad_idx = pad;
  240. append_inout(open_inputs, &p);
  241. }
  242. }
  243. if (*curr_inputs) {
  244. av_log(log_ctx, AV_LOG_ERROR,
  245. "Too many inputs specified for the \"%s\" filter.\n",
  246. filt_ctx->filter->name);
  247. return AVERROR(EINVAL);
  248. }
  249. pad = filt_ctx->nb_outputs;
  250. while (pad--) {
  251. AVFilterInOut *currlinkn = av_mallocz(sizeof(AVFilterInOut));
  252. if (!currlinkn)
  253. return AVERROR(ENOMEM);
  254. currlinkn->filter_ctx = filt_ctx;
  255. currlinkn->pad_idx = pad;
  256. insert_inout(curr_inputs, currlinkn);
  257. }
  258. return 0;
  259. }
  260. static int parse_inputs(const char **buf, AVFilterInOut **curr_inputs,
  261. AVFilterInOut **open_outputs, void *log_ctx)
  262. {
  263. AVFilterInOut *parsed_inputs = NULL;
  264. int pad = 0;
  265. while (**buf == '[') {
  266. char *name = parse_link_name(buf, log_ctx);
  267. AVFilterInOut *match;
  268. if (!name)
  269. return AVERROR(EINVAL);
  270. /* First check if the label is not in the open_outputs list */
  271. match = extract_inout(name, open_outputs);
  272. if (match) {
  273. av_free(name);
  274. } else {
  275. /* Not in the list, so add it as an input */
  276. if (!(match = av_mallocz(sizeof(AVFilterInOut)))) {
  277. av_free(name);
  278. return AVERROR(ENOMEM);
  279. }
  280. match->name = name;
  281. match->pad_idx = pad;
  282. }
  283. append_inout(&parsed_inputs, &match);
  284. *buf += strspn(*buf, WHITESPACES);
  285. pad++;
  286. }
  287. append_inout(&parsed_inputs, curr_inputs);
  288. *curr_inputs = parsed_inputs;
  289. return pad;
  290. }
  291. static int parse_outputs(const char **buf, AVFilterInOut **curr_inputs,
  292. AVFilterInOut **open_inputs,
  293. AVFilterInOut **open_outputs, void *log_ctx)
  294. {
  295. int ret, pad = 0;
  296. while (**buf == '[') {
  297. char *name = parse_link_name(buf, log_ctx);
  298. AVFilterInOut *match;
  299. AVFilterInOut *input = *curr_inputs;
  300. if (!name)
  301. return AVERROR(EINVAL);
  302. if (!input) {
  303. av_log(log_ctx, AV_LOG_ERROR,
  304. "No output pad can be associated to link label '%s'.\n", name);
  305. av_free(name);
  306. return AVERROR(EINVAL);
  307. }
  308. *curr_inputs = (*curr_inputs)->next;
  309. /* First check if the label is not in the open_inputs list */
  310. match = extract_inout(name, open_inputs);
  311. if (match) {
  312. if ((ret = link_filter(input->filter_ctx, input->pad_idx,
  313. match->filter_ctx, match->pad_idx, log_ctx)) < 0) {
  314. av_free(name);
  315. return ret;
  316. }
  317. av_free(match->name);
  318. av_free(name);
  319. av_free(match);
  320. av_free(input);
  321. } else {
  322. /* Not in the list, so add the first input as a open_output */
  323. input->name = name;
  324. insert_inout(open_outputs, input);
  325. }
  326. *buf += strspn(*buf, WHITESPACES);
  327. pad++;
  328. }
  329. return pad;
  330. }
  331. static int parse_sws_flags(const char **buf, AVFilterGraph *graph)
  332. {
  333. char *p = strchr(*buf, ';');
  334. if (strncmp(*buf, "sws_flags=", 10))
  335. return 0;
  336. if (!p) {
  337. av_log(graph, AV_LOG_ERROR, "sws_flags not terminated with ';'.\n");
  338. return AVERROR(EINVAL);
  339. }
  340. *buf += 4; // keep the 'flags=' part
  341. av_freep(&graph->scale_sws_opts);
  342. if (!(graph->scale_sws_opts = av_mallocz(p - *buf + 1)))
  343. return AVERROR(ENOMEM);
  344. av_strlcpy(graph->scale_sws_opts, *buf, p - *buf + 1);
  345. *buf = p + 1;
  346. return 0;
  347. }
  348. int avfilter_graph_parse2(AVFilterGraph *graph, const char *filters,
  349. AVFilterInOut **inputs,
  350. AVFilterInOut **outputs)
  351. {
  352. int index = 0, ret;
  353. char chr = 0;
  354. AVFilterInOut *curr_inputs = NULL, *open_inputs = NULL, *open_outputs = NULL;
  355. filters += strspn(filters, WHITESPACES);
  356. if ((ret = parse_sws_flags(&filters, graph)) < 0)
  357. goto fail;
  358. do {
  359. AVFilterContext *filter;
  360. filters += strspn(filters, WHITESPACES);
  361. if ((ret = parse_inputs(&filters, &curr_inputs, &open_outputs, graph)) < 0)
  362. goto fail;
  363. if ((ret = parse_filter(&filter, &filters, graph, index, graph)) < 0)
  364. goto fail;
  365. if ((ret = link_filter_inouts(filter, &curr_inputs, &open_inputs, graph)) < 0)
  366. goto fail;
  367. if ((ret = parse_outputs(&filters, &curr_inputs, &open_inputs, &open_outputs,
  368. graph)) < 0)
  369. goto fail;
  370. filters += strspn(filters, WHITESPACES);
  371. chr = *filters++;
  372. if (chr == ';' && curr_inputs)
  373. append_inout(&open_outputs, &curr_inputs);
  374. index++;
  375. } while (chr == ',' || chr == ';');
  376. if (chr) {
  377. av_log(graph, AV_LOG_ERROR,
  378. "Unable to parse graph description substring: \"%s\"\n",
  379. filters - 1);
  380. ret = AVERROR(EINVAL);
  381. goto fail;
  382. }
  383. append_inout(&open_outputs, &curr_inputs);
  384. *inputs = open_inputs;
  385. *outputs = open_outputs;
  386. return 0;
  387. fail:
  388. while (graph->nb_filters)
  389. avfilter_free(graph->filters[0]);
  390. av_freep(&graph->filters);
  391. avfilter_inout_free(&open_inputs);
  392. avfilter_inout_free(&open_outputs);
  393. avfilter_inout_free(&curr_inputs);
  394. *inputs = NULL;
  395. *outputs = NULL;
  396. return ret;
  397. }
  398. int avfilter_graph_parse(AVFilterGraph *graph, const char *filters,
  399. AVFilterInOut *open_inputs,
  400. AVFilterInOut *open_outputs, void *log_ctx)
  401. {
  402. int ret;
  403. AVFilterInOut *cur, *match, *inputs = NULL, *outputs = NULL;
  404. if ((ret = avfilter_graph_parse2(graph, filters, &inputs, &outputs)) < 0)
  405. goto fail;
  406. /* First input can be omitted if it is "[in]" */
  407. if (inputs && !inputs->name)
  408. inputs->name = av_strdup("in");
  409. for (cur = inputs; cur; cur = cur->next) {
  410. if (!cur->name) {
  411. av_log(log_ctx, AV_LOG_ERROR,
  412. "Not enough inputs specified for the \"%s\" filter.\n",
  413. cur->filter_ctx->filter->name);
  414. ret = AVERROR(EINVAL);
  415. goto fail;
  416. }
  417. if (!(match = extract_inout(cur->name, &open_outputs)))
  418. continue;
  419. ret = avfilter_link(match->filter_ctx, match->pad_idx,
  420. cur->filter_ctx, cur->pad_idx);
  421. avfilter_inout_free(&match);
  422. if (ret < 0)
  423. goto fail;
  424. }
  425. /* Last output can be omitted if it is "[out]" */
  426. if (outputs && !outputs->name)
  427. outputs->name = av_strdup("out");
  428. for (cur = outputs; cur; cur = cur->next) {
  429. if (!cur->name) {
  430. av_log(log_ctx, AV_LOG_ERROR,
  431. "Invalid filterchain containing an unlabelled output pad: \"%s\"\n",
  432. filters);
  433. ret = AVERROR(EINVAL);
  434. goto fail;
  435. }
  436. if (!(match = extract_inout(cur->name, &open_inputs)))
  437. continue;
  438. ret = avfilter_link(cur->filter_ctx, cur->pad_idx,
  439. match->filter_ctx, match->pad_idx);
  440. avfilter_inout_free(&match);
  441. if (ret < 0)
  442. goto fail;
  443. }
  444. fail:
  445. if (ret < 0) {
  446. while (graph->nb_filters)
  447. avfilter_free(graph->filters[0]);
  448. av_freep(&graph->filters);
  449. }
  450. avfilter_inout_free(&inputs);
  451. avfilter_inout_free(&outputs);
  452. avfilter_inout_free(&open_inputs);
  453. avfilter_inout_free(&open_outputs);
  454. return ret;
  455. }