graphparser.c 17 KB

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