graphparser.c 18 KB

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