graphparser.c 17 KB

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