graphparser.c 17 KB

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