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.

597 lines
17KB

  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. if ((ret = link_filter(p->filter_ctx, p->pad_idx, filt_ctx, pad, log_ctx)) < 0)
  207. return ret;
  208. av_free(p->name);
  209. av_free(p);
  210. } else {
  211. p->filter_ctx = filt_ctx;
  212. p->pad_idx = pad;
  213. append_inout(open_inputs, &p);
  214. }
  215. }
  216. if (*curr_inputs) {
  217. av_log(log_ctx, AV_LOG_ERROR,
  218. "Too many inputs specified for the \"%s\" filter.\n",
  219. filt_ctx->filter->name);
  220. return AVERROR(EINVAL);
  221. }
  222. pad = filt_ctx->nb_outputs;
  223. while (pad--) {
  224. AVFilterInOut *currlinkn = av_mallocz(sizeof(AVFilterInOut));
  225. if (!currlinkn)
  226. return AVERROR(ENOMEM);
  227. currlinkn->filter_ctx = filt_ctx;
  228. currlinkn->pad_idx = pad;
  229. insert_inout(curr_inputs, currlinkn);
  230. }
  231. return 0;
  232. }
  233. static int parse_inputs(const char **buf, AVFilterInOut **curr_inputs,
  234. AVFilterInOut **open_outputs, void *log_ctx)
  235. {
  236. AVFilterInOut *parsed_inputs = NULL;
  237. int pad = 0;
  238. while (**buf == '[') {
  239. char *name = parse_link_name(buf, log_ctx);
  240. AVFilterInOut *match;
  241. if (!name)
  242. return AVERROR(EINVAL);
  243. /* First check if the label is not in the open_outputs list */
  244. match = extract_inout(name, open_outputs);
  245. if (match) {
  246. av_free(name);
  247. } else {
  248. /* Not in the list, so add it as an input */
  249. if (!(match = av_mallocz(sizeof(AVFilterInOut))))
  250. return AVERROR(ENOMEM);
  251. match->name = name;
  252. match->pad_idx = pad;
  253. }
  254. append_inout(&parsed_inputs, &match);
  255. *buf += strspn(*buf, WHITESPACES);
  256. pad++;
  257. }
  258. append_inout(&parsed_inputs, curr_inputs);
  259. *curr_inputs = parsed_inputs;
  260. return pad;
  261. }
  262. static int parse_outputs(const char **buf, AVFilterInOut **curr_inputs,
  263. AVFilterInOut **open_inputs,
  264. AVFilterInOut **open_outputs, void *log_ctx)
  265. {
  266. int ret, pad = 0;
  267. while (**buf == '[') {
  268. char *name = parse_link_name(buf, log_ctx);
  269. AVFilterInOut *match;
  270. AVFilterInOut *input = *curr_inputs;
  271. if (!input) {
  272. av_log(log_ctx, AV_LOG_ERROR,
  273. "No output pad can be associated to link label '%s'.\n",
  274. name);
  275. return AVERROR(EINVAL);
  276. }
  277. *curr_inputs = (*curr_inputs)->next;
  278. if (!name)
  279. return AVERROR(EINVAL);
  280. /* First check if the label is not in the open_inputs list */
  281. match = extract_inout(name, open_inputs);
  282. if (match) {
  283. if ((ret = link_filter(input->filter_ctx, input->pad_idx,
  284. match->filter_ctx, match->pad_idx, log_ctx)) < 0)
  285. return ret;
  286. av_free(match->name);
  287. av_free(name);
  288. av_free(match);
  289. av_free(input);
  290. } else {
  291. /* Not in the list, so add the first input as a open_output */
  292. input->name = name;
  293. insert_inout(open_outputs, input);
  294. }
  295. *buf += strspn(*buf, WHITESPACES);
  296. pad++;
  297. }
  298. return pad;
  299. }
  300. static int parse_sws_flags(const char **buf, AVFilterGraph *graph)
  301. {
  302. char *p = strchr(*buf, ';');
  303. if (strncmp(*buf, "sws_flags=", 10))
  304. return 0;
  305. if (!p) {
  306. av_log(graph, AV_LOG_ERROR, "sws_flags not terminated with ';'.\n");
  307. return AVERROR(EINVAL);
  308. }
  309. *buf += 4; // keep the 'flags=' part
  310. av_freep(&graph->scale_sws_opts);
  311. if (!(graph->scale_sws_opts = av_mallocz(p - *buf + 1)))
  312. return AVERROR(ENOMEM);
  313. av_strlcpy(graph->scale_sws_opts, *buf, p - *buf + 1);
  314. *buf = p + 1;
  315. return 0;
  316. }
  317. int avfilter_graph_parse2(AVFilterGraph *graph, const char *filters,
  318. AVFilterInOut **inputs,
  319. AVFilterInOut **outputs)
  320. {
  321. int index = 0, ret = 0;
  322. char chr = 0;
  323. AVFilterInOut *curr_inputs = NULL, *open_inputs = NULL, *open_outputs = NULL;
  324. filters += strspn(filters, WHITESPACES);
  325. if ((ret = parse_sws_flags(&filters, graph)) < 0)
  326. goto fail;
  327. do {
  328. AVFilterContext *filter;
  329. filters += strspn(filters, WHITESPACES);
  330. if ((ret = parse_inputs(&filters, &curr_inputs, &open_outputs, graph)) < 0)
  331. goto end;
  332. if ((ret = parse_filter(&filter, &filters, graph, index, graph)) < 0)
  333. goto end;
  334. if ((ret = link_filter_inouts(filter, &curr_inputs, &open_inputs, graph)) < 0)
  335. goto end;
  336. if ((ret = parse_outputs(&filters, &curr_inputs, &open_inputs, &open_outputs,
  337. graph)) < 0)
  338. goto end;
  339. filters += strspn(filters, WHITESPACES);
  340. chr = *filters++;
  341. if (chr == ';' && curr_inputs)
  342. append_inout(&open_outputs, &curr_inputs);
  343. index++;
  344. } while (chr == ',' || chr == ';');
  345. if (chr) {
  346. av_log(graph, AV_LOG_ERROR,
  347. "Unable to parse graph description substring: \"%s\"\n",
  348. filters - 1);
  349. ret = AVERROR(EINVAL);
  350. goto end;
  351. }
  352. append_inout(&open_outputs, &curr_inputs);
  353. *inputs = open_inputs;
  354. *outputs = open_outputs;
  355. return 0;
  356. fail:end:
  357. for (; graph->filter_count > 0; graph->filter_count--)
  358. avfilter_free(graph->filters[graph->filter_count - 1]);
  359. av_freep(&graph->filters);
  360. avfilter_inout_free(&open_inputs);
  361. avfilter_inout_free(&open_outputs);
  362. avfilter_inout_free(&curr_inputs);
  363. *inputs = NULL;
  364. *outputs = NULL;
  365. return ret;
  366. }
  367. int avfilter_graph_parse(AVFilterGraph *graph, const char *filters,
  368. AVFilterInOut **open_inputs_ptr, AVFilterInOut **open_outputs_ptr,
  369. void *log_ctx)
  370. {
  371. #if 0
  372. int ret;
  373. AVFilterInOut *open_inputs = open_inputs_ptr ? *open_inputs_ptr : NULL;
  374. AVFilterInOut *open_outputs = open_outputs_ptr ? *open_outputs_ptr : NULL;
  375. AVFilterInOut *cur, *match, *inputs = NULL, *outputs = NULL;
  376. if ((ret = avfilter_graph_parse2(graph, filters, &inputs, &outputs)) < 0)
  377. goto fail;
  378. /* First input can be omitted if it is "[in]" */
  379. if (inputs && !inputs->name)
  380. inputs->name = av_strdup("in");
  381. for (cur = inputs; cur; cur = cur->next) {
  382. if (!cur->name) {
  383. av_log(log_ctx, AV_LOG_ERROR,
  384. "Not enough inputs specified for the \"%s\" filter.\n",
  385. cur->filter_ctx->filter->name);
  386. ret = AVERROR(EINVAL);
  387. goto fail;
  388. }
  389. if (!(match = extract_inout(cur->name, &open_outputs)))
  390. continue;
  391. ret = avfilter_link(match->filter_ctx, match->pad_idx,
  392. cur->filter_ctx, cur->pad_idx);
  393. avfilter_inout_free(&match);
  394. if (ret < 0)
  395. goto fail;
  396. }
  397. /* Last output can be omitted if it is "[out]" */
  398. if (outputs && !outputs->name)
  399. outputs->name = av_strdup("out");
  400. for (cur = outputs; cur; cur = cur->next) {
  401. if (!cur->name) {
  402. av_log(log_ctx, AV_LOG_ERROR,
  403. "Invalid filterchain containing an unlabelled output pad: \"%s\"\n",
  404. filters);
  405. ret = AVERROR(EINVAL);
  406. goto fail;
  407. }
  408. if (!(match = extract_inout(cur->name, &open_inputs)))
  409. continue;
  410. ret = avfilter_link(cur->filter_ctx, cur->pad_idx,
  411. match->filter_ctx, match->pad_idx);
  412. avfilter_inout_free(&match);
  413. if (ret < 0)
  414. goto fail;
  415. }
  416. fail:
  417. if (ret < 0) {
  418. for (; graph->filter_count > 0; graph->filter_count--)
  419. avfilter_free(graph->filters[graph->filter_count - 1]);
  420. av_freep(&graph->filters);
  421. }
  422. avfilter_inout_free(&inputs);
  423. avfilter_inout_free(&outputs);
  424. /* clear open_in/outputs only if not passed as parameters */
  425. if (open_inputs_ptr) *open_inputs_ptr = open_inputs;
  426. else avfilter_inout_free(&open_inputs);
  427. if (open_outputs_ptr) *open_outputs_ptr = open_outputs;
  428. else avfilter_inout_free(&open_outputs);
  429. return ret;
  430. }
  431. #else
  432. int index = 0, ret = 0;
  433. char chr = 0;
  434. AVFilterInOut *curr_inputs = NULL;
  435. AVFilterInOut *open_inputs = open_inputs_ptr ? *open_inputs_ptr : NULL;
  436. AVFilterInOut *open_outputs = open_outputs_ptr ? *open_outputs_ptr : NULL;
  437. if ((ret = parse_sws_flags(&filters, graph)) < 0)
  438. goto end;
  439. do {
  440. AVFilterContext *filter;
  441. const char *filterchain = filters;
  442. filters += strspn(filters, WHITESPACES);
  443. if ((ret = parse_inputs(&filters, &curr_inputs, &open_outputs, log_ctx)) < 0)
  444. goto end;
  445. if ((ret = parse_filter(&filter, &filters, graph, index, log_ctx)) < 0)
  446. goto end;
  447. if (filter->input_count == 1 && !curr_inputs && !index) {
  448. /* First input pad, assume it is "[in]" if not specified */
  449. const char *tmp = "[in]";
  450. if ((ret = parse_inputs(&tmp, &curr_inputs, &open_outputs, log_ctx)) < 0)
  451. goto end;
  452. }
  453. if ((ret = link_filter_inouts(filter, &curr_inputs, &open_inputs, log_ctx)) < 0)
  454. goto end;
  455. if ((ret = parse_outputs(&filters, &curr_inputs, &open_inputs, &open_outputs,
  456. log_ctx)) < 0)
  457. goto end;
  458. filters += strspn(filters, WHITESPACES);
  459. chr = *filters++;
  460. if (chr == ';' && curr_inputs) {
  461. av_log(log_ctx, AV_LOG_ERROR,
  462. "Invalid filterchain containing an unlabelled output pad: \"%s\"\n",
  463. filterchain);
  464. ret = AVERROR(EINVAL);
  465. goto end;
  466. }
  467. index++;
  468. } while (chr == ',' || chr == ';');
  469. if (chr) {
  470. av_log(log_ctx, AV_LOG_ERROR,
  471. "Unable to parse graph description substring: \"%s\"\n",
  472. filters - 1);
  473. ret = AVERROR(EINVAL);
  474. goto end;
  475. }
  476. if (curr_inputs) {
  477. /* Last output pad, assume it is "[out]" if not specified */
  478. const char *tmp = "[out]";
  479. if ((ret = parse_outputs(&tmp, &curr_inputs, &open_inputs, &open_outputs,
  480. log_ctx)) < 0)
  481. goto end;
  482. }
  483. end:
  484. /* clear open_in/outputs only if not passed as parameters */
  485. if (open_inputs_ptr) *open_inputs_ptr = open_inputs;
  486. else avfilter_inout_free(&open_inputs);
  487. if (open_outputs_ptr) *open_outputs_ptr = open_outputs;
  488. else avfilter_inout_free(&open_outputs);
  489. avfilter_inout_free(&curr_inputs);
  490. if (ret < 0) {
  491. for (; graph->filter_count > 0; graph->filter_count--)
  492. avfilter_free(graph->filters[graph->filter_count - 1]);
  493. av_freep(&graph->filters);
  494. }
  495. return ret;
  496. }
  497. #endif