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.

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