graphparser.c 18 KB

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