graphparser.c 18 KB

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