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.

606 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[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. }
  119. av_free(tmp_args);
  120. return ret;
  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. while (graph->nb_filters)
  363. avfilter_free(graph->filters[0]);
  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. #if HAVE_INCOMPATIBLE_LIBAV_ABI || !FF_API_OLD_GRAPH_PARSE
  373. int avfilter_graph_parse(AVFilterGraph *graph, const char *filters,
  374. AVFilterInOut *open_inputs,
  375. AVFilterInOut *open_outputs, void *log_ctx)
  376. {
  377. int ret;
  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. while (graph->nb_filters)
  422. avfilter_free(graph->filters[0]);
  423. av_freep(&graph->filters);
  424. }
  425. avfilter_inout_free(&inputs);
  426. avfilter_inout_free(&outputs);
  427. avfilter_inout_free(&open_inputs);
  428. avfilter_inout_free(&open_outputs);
  429. return ret;
  430. #else
  431. int avfilter_graph_parse(AVFilterGraph *graph, const char *filters,
  432. AVFilterInOut **inputs, AVFilterInOut **outputs,
  433. void *log_ctx)
  434. {
  435. return avfilter_graph_parse_ptr(graph, filters, inputs, outputs, log_ctx);
  436. #endif
  437. }
  438. int avfilter_graph_parse_ptr(AVFilterGraph *graph, const char *filters,
  439. AVFilterInOut **open_inputs_ptr, AVFilterInOut **open_outputs_ptr,
  440. void *log_ctx)
  441. {
  442. int index = 0, ret = 0;
  443. char chr = 0;
  444. AVFilterInOut *curr_inputs = NULL;
  445. AVFilterInOut *open_inputs = open_inputs_ptr ? *open_inputs_ptr : NULL;
  446. AVFilterInOut *open_outputs = open_outputs_ptr ? *open_outputs_ptr : NULL;
  447. if ((ret = parse_sws_flags(&filters, graph)) < 0)
  448. goto end;
  449. do {
  450. AVFilterContext *filter;
  451. const char *filterchain = filters;
  452. filters += strspn(filters, WHITESPACES);
  453. if ((ret = parse_inputs(&filters, &curr_inputs, &open_outputs, log_ctx)) < 0)
  454. goto end;
  455. if ((ret = parse_filter(&filter, &filters, graph, index, log_ctx)) < 0)
  456. goto end;
  457. if (filter->nb_inputs == 1 && !curr_inputs && !index) {
  458. /* First input pad, assume it is "[in]" if not specified */
  459. const char *tmp = "[in]";
  460. if ((ret = parse_inputs(&tmp, &curr_inputs, &open_outputs, log_ctx)) < 0)
  461. goto end;
  462. }
  463. if ((ret = link_filter_inouts(filter, &curr_inputs, &open_inputs, log_ctx)) < 0)
  464. goto end;
  465. if ((ret = parse_outputs(&filters, &curr_inputs, &open_inputs, &open_outputs,
  466. log_ctx)) < 0)
  467. goto end;
  468. filters += strspn(filters, WHITESPACES);
  469. chr = *filters++;
  470. if (chr == ';' && curr_inputs) {
  471. av_log(log_ctx, AV_LOG_ERROR,
  472. "Invalid filterchain containing an unlabelled output pad: \"%s\"\n",
  473. filterchain);
  474. ret = AVERROR(EINVAL);
  475. goto end;
  476. }
  477. index++;
  478. } while (chr == ',' || chr == ';');
  479. if (chr) {
  480. av_log(log_ctx, AV_LOG_ERROR,
  481. "Unable to parse graph description substring: \"%s\"\n",
  482. filters - 1);
  483. ret = AVERROR(EINVAL);
  484. goto end;
  485. }
  486. if (curr_inputs) {
  487. /* Last output pad, assume it is "[out]" if not specified */
  488. const char *tmp = "[out]";
  489. if ((ret = parse_outputs(&tmp, &curr_inputs, &open_inputs, &open_outputs,
  490. log_ctx)) < 0)
  491. goto end;
  492. }
  493. end:
  494. /* clear open_in/outputs only if not passed as parameters */
  495. if (open_inputs_ptr) *open_inputs_ptr = open_inputs;
  496. else avfilter_inout_free(&open_inputs);
  497. if (open_outputs_ptr) *open_outputs_ptr = open_outputs;
  498. else avfilter_inout_free(&open_outputs);
  499. avfilter_inout_free(&curr_inputs);
  500. if (ret < 0) {
  501. while (graph->nb_filters)
  502. avfilter_free(graph->filters[0]);
  503. av_freep(&graph->filters);
  504. }
  505. return ret;
  506. }