You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

610 lines
18KB

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