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.

602 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. #include "avfiltergraph.h"
  28. #define WHITESPACES " \n\t"
  29. /**
  30. * Link two filters together.
  31. *
  32. * @see avfilter_link()
  33. */
  34. static int link_filter(AVFilterContext *src, int srcpad,
  35. AVFilterContext *dst, int dstpad,
  36. void *log_ctx)
  37. {
  38. int ret;
  39. if ((ret = avfilter_link(src, srcpad, dst, dstpad))) {
  40. av_log(log_ctx, AV_LOG_ERROR,
  41. "Cannot create the link %s:%d -> %s:%d\n",
  42. src->filter->name, srcpad, dst->filter->name, dstpad);
  43. return ret;
  44. }
  45. return 0;
  46. }
  47. /**
  48. * Parse the name of a link, which has the format "[linkname]".
  49. *
  50. * @return a pointer (that need to be freed after use) to the name
  51. * between parenthesis
  52. */
  53. static char *parse_link_name(const char **buf, void *log_ctx)
  54. {
  55. const char *start = *buf;
  56. char *name;
  57. (*buf)++;
  58. name = av_get_token(buf, "]");
  59. if (!name[0]) {
  60. av_log(log_ctx, AV_LOG_ERROR,
  61. "Bad (empty?) label found in the following: \"%s\".\n", start);
  62. goto fail;
  63. }
  64. if (*(*buf)++ != ']') {
  65. av_log(log_ctx, AV_LOG_ERROR,
  66. "Mismatched '[' found in the following: \"%s\".\n", start);
  67. fail:
  68. av_freep(&name);
  69. }
  70. return name;
  71. }
  72. /**
  73. * Create an instance of a filter, initialize and insert it in the
  74. * filtergraph in *ctx.
  75. *
  76. * @param filt_ctx put here a filter context in case of successful creation and configuration, NULL otherwise.
  77. * @param ctx the filtergraph context
  78. * @param index an index which is supposed to be unique for each filter instance added to the filtergraph
  79. * @param filt_name the name of the filter to create
  80. * @param args the arguments provided to the filter during its initialization
  81. * @param log_ctx the log context to use
  82. * @return 0 in case of success, a negative AVERROR code otherwise
  83. */
  84. static int create_filter(AVFilterContext **filt_ctx, AVFilterGraph *ctx, int index,
  85. const char *filt_name, const char *args, void *log_ctx)
  86. {
  87. AVFilter *filt;
  88. char inst_name[30];
  89. char tmp_args[256];
  90. int ret;
  91. snprintf(inst_name, sizeof(inst_name), "Parsed_%s_%d", filt_name, index);
  92. filt = avfilter_get_by_name(filt_name);
  93. if (!filt) {
  94. av_log(log_ctx, AV_LOG_ERROR,
  95. "No such filter: '%s'\n", filt_name);
  96. return AVERROR(EINVAL);
  97. }
  98. ret = avfilter_open(filt_ctx, filt, inst_name);
  99. if (!*filt_ctx) {
  100. av_log(log_ctx, AV_LOG_ERROR,
  101. "Error creating filter '%s'\n", filt_name);
  102. return ret;
  103. }
  104. if ((ret = avfilter_graph_add_filter(ctx, *filt_ctx)) < 0) {
  105. avfilter_free(*filt_ctx);
  106. return ret;
  107. }
  108. if (!strcmp(filt_name, "scale") && args && !strstr(args, "flags")
  109. && ctx->scale_sws_opts) {
  110. snprintf(tmp_args, sizeof(tmp_args), "%s:%s",
  111. args, ctx->scale_sws_opts);
  112. args = tmp_args;
  113. }
  114. if ((ret = avfilter_init_filter(*filt_ctx, args, NULL)) < 0) {
  115. av_log(log_ctx, AV_LOG_ERROR,
  116. "Error initializing filter '%s' with args '%s'\n", filt_name, args);
  117. return ret;
  118. }
  119. return 0;
  120. }
  121. /**
  122. * Parse a string of the form FILTER_NAME[=PARAMS], and create a
  123. * corresponding filter instance which is added to graph with
  124. * create_filter().
  125. *
  126. * @param filt_ctx Pointer that is set to the created and configured filter
  127. * context on success, set to NULL on failure.
  128. * @param filt_ctx put here a pointer to the created filter context on
  129. * success, NULL otherwise
  130. * @param buf pointer to the buffer to parse, *buf will be updated to
  131. * point to the char next after the parsed string
  132. * @param index an index which is assigned to the created filter
  133. * instance, and which is supposed to be unique for each filter
  134. * instance added to the filtergraph
  135. * @return 0 in case of success, a negative AVERROR code otherwise
  136. */
  137. static int parse_filter(AVFilterContext **filt_ctx, const char **buf, AVFilterGraph *graph,
  138. int index, void *log_ctx)
  139. {
  140. char *opts = NULL;
  141. char *name = av_get_token(buf, "=,;[\n");
  142. int ret;
  143. if (**buf == '=') {
  144. (*buf)++;
  145. opts = av_get_token(buf, "[],;\n");
  146. }
  147. ret = create_filter(filt_ctx, graph, index, name, opts, log_ctx);
  148. av_free(name);
  149. av_free(opts);
  150. return ret;
  151. }
  152. AVFilterInOut *avfilter_inout_alloc(void)
  153. {
  154. return av_mallocz(sizeof(AVFilterInOut));
  155. }
  156. void avfilter_inout_free(AVFilterInOut **inout)
  157. {
  158. while (*inout) {
  159. AVFilterInOut *next = (*inout)->next;
  160. av_freep(&(*inout)->name);
  161. av_freep(inout);
  162. *inout = next;
  163. }
  164. }
  165. static AVFilterInOut *extract_inout(const char *label, AVFilterInOut **links)
  166. {
  167. AVFilterInOut *ret;
  168. while (*links && (!(*links)->name || strcmp((*links)->name, label)))
  169. links = &((*links)->next);
  170. ret = *links;
  171. if (ret) {
  172. *links = ret->next;
  173. ret->next = NULL;
  174. }
  175. return ret;
  176. }
  177. static void insert_inout(AVFilterInOut **inouts, AVFilterInOut *element)
  178. {
  179. element->next = *inouts;
  180. *inouts = element;
  181. }
  182. static void append_inout(AVFilterInOut **inouts, AVFilterInOut **element)
  183. {
  184. while (*inouts && (*inouts)->next)
  185. inouts = &((*inouts)->next);
  186. if (!*inouts)
  187. *inouts = *element;
  188. else
  189. (*inouts)->next = *element;
  190. *element = NULL;
  191. }
  192. static int link_filter_inouts(AVFilterContext *filt_ctx,
  193. AVFilterInOut **curr_inputs,
  194. AVFilterInOut **open_inputs, void *log_ctx)
  195. {
  196. int pad, ret;
  197. for (pad = 0; pad < filt_ctx->nb_inputs; pad++) {
  198. AVFilterInOut *p = *curr_inputs;
  199. if (p) {
  200. *curr_inputs = (*curr_inputs)->next;
  201. p->next = NULL;
  202. } else if (!(p = av_mallocz(sizeof(*p))))
  203. return AVERROR(ENOMEM);
  204. if (p->filter_ctx) {
  205. ret = link_filter(p->filter_ctx, p->pad_idx, filt_ctx, pad, log_ctx);
  206. av_free(p->name);
  207. av_free(p);
  208. if (ret < 0)
  209. return ret;
  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. av_free(name);
  251. return AVERROR(ENOMEM);
  252. }
  253. match->name = name;
  254. match->pad_idx = pad;
  255. }
  256. append_inout(&parsed_inputs, &match);
  257. *buf += strspn(*buf, WHITESPACES);
  258. pad++;
  259. }
  260. append_inout(&parsed_inputs, curr_inputs);
  261. *curr_inputs = parsed_inputs;
  262. return pad;
  263. }
  264. static int parse_outputs(const char **buf, AVFilterInOut **curr_inputs,
  265. AVFilterInOut **open_inputs,
  266. AVFilterInOut **open_outputs, void *log_ctx)
  267. {
  268. int ret, pad = 0;
  269. while (**buf == '[') {
  270. char *name = parse_link_name(buf, log_ctx);
  271. AVFilterInOut *match;
  272. AVFilterInOut *input = *curr_inputs;
  273. if (!name)
  274. return AVERROR(EINVAL);
  275. if (!input) {
  276. av_log(log_ctx, AV_LOG_ERROR,
  277. "No output pad can be associated to link label '%s'.\n", name);
  278. av_free(name);
  279. return AVERROR(EINVAL);
  280. }
  281. *curr_inputs = (*curr_inputs)->next;
  282. /* First check if the label is not in the open_inputs list */
  283. match = extract_inout(name, open_inputs);
  284. if (match) {
  285. if ((ret = link_filter(input->filter_ctx, input->pad_idx,
  286. match->filter_ctx, match->pad_idx, log_ctx)) < 0) {
  287. av_free(name);
  288. return ret;
  289. }
  290. av_free(match->name);
  291. av_free(name);
  292. av_free(match);
  293. av_free(input);
  294. } else {
  295. /* Not in the list, so add the first input as a open_output */
  296. input->name = name;
  297. insert_inout(open_outputs, input);
  298. }
  299. *buf += strspn(*buf, WHITESPACES);
  300. pad++;
  301. }
  302. return pad;
  303. }
  304. static int parse_sws_flags(const char **buf, AVFilterGraph *graph)
  305. {
  306. char *p = strchr(*buf, ';');
  307. if (strncmp(*buf, "sws_flags=", 10))
  308. return 0;
  309. if (!p) {
  310. av_log(graph, AV_LOG_ERROR, "sws_flags not terminated with ';'.\n");
  311. return AVERROR(EINVAL);
  312. }
  313. *buf += 4; // keep the 'flags=' part
  314. av_freep(&graph->scale_sws_opts);
  315. if (!(graph->scale_sws_opts = av_mallocz(p - *buf + 1)))
  316. return AVERROR(ENOMEM);
  317. av_strlcpy(graph->scale_sws_opts, *buf, p - *buf + 1);
  318. *buf = p + 1;
  319. return 0;
  320. }
  321. int avfilter_graph_parse2(AVFilterGraph *graph, const char *filters,
  322. AVFilterInOut **inputs,
  323. AVFilterInOut **outputs)
  324. {
  325. int index = 0, ret = 0;
  326. char chr = 0;
  327. AVFilterInOut *curr_inputs = NULL, *open_inputs = NULL, *open_outputs = NULL;
  328. filters += strspn(filters, WHITESPACES);
  329. if ((ret = parse_sws_flags(&filters, graph)) < 0)
  330. goto fail;
  331. do {
  332. AVFilterContext *filter;
  333. filters += strspn(filters, WHITESPACES);
  334. if ((ret = parse_inputs(&filters, &curr_inputs, &open_outputs, graph)) < 0)
  335. goto end;
  336. if ((ret = parse_filter(&filter, &filters, graph, index, graph)) < 0)
  337. goto end;
  338. if ((ret = link_filter_inouts(filter, &curr_inputs, &open_inputs, graph)) < 0)
  339. goto end;
  340. if ((ret = parse_outputs(&filters, &curr_inputs, &open_inputs, &open_outputs,
  341. graph)) < 0)
  342. goto end;
  343. filters += strspn(filters, WHITESPACES);
  344. chr = *filters++;
  345. if (chr == ';' && curr_inputs)
  346. append_inout(&open_outputs, &curr_inputs);
  347. index++;
  348. } while (chr == ',' || chr == ';');
  349. if (chr) {
  350. av_log(graph, AV_LOG_ERROR,
  351. "Unable to parse graph description substring: \"%s\"\n",
  352. filters - 1);
  353. ret = AVERROR(EINVAL);
  354. goto end;
  355. }
  356. append_inout(&open_outputs, &curr_inputs);
  357. *inputs = open_inputs;
  358. *outputs = open_outputs;
  359. return 0;
  360. fail:end:
  361. for (; graph->filter_count > 0; graph->filter_count--)
  362. avfilter_free(graph->filters[graph->filter_count - 1]);
  363. av_freep(&graph->filters);
  364. avfilter_inout_free(&open_inputs);
  365. avfilter_inout_free(&open_outputs);
  366. avfilter_inout_free(&curr_inputs);
  367. *inputs = NULL;
  368. *outputs = NULL;
  369. return ret;
  370. }
  371. int avfilter_graph_parse(AVFilterGraph *graph, const char *filters,
  372. AVFilterInOut **open_inputs_ptr, AVFilterInOut **open_outputs_ptr,
  373. void *log_ctx)
  374. {
  375. #if 0
  376. int ret;
  377. AVFilterInOut *open_inputs = open_inputs_ptr ? *open_inputs_ptr : NULL;
  378. AVFilterInOut *open_outputs = open_outputs_ptr ? *open_outputs_ptr : NULL;
  379. AVFilterInOut *cur, *match, *inputs = NULL, *outputs = NULL;
  380. if ((ret = avfilter_graph_parse2(graph, filters, &inputs, &outputs)) < 0)
  381. goto fail;
  382. /* First input can be omitted if it is "[in]" */
  383. if (inputs && !inputs->name)
  384. inputs->name = av_strdup("in");
  385. for (cur = inputs; cur; cur = cur->next) {
  386. if (!cur->name) {
  387. av_log(log_ctx, AV_LOG_ERROR,
  388. "Not enough inputs specified for the \"%s\" filter.\n",
  389. cur->filter_ctx->filter->name);
  390. ret = AVERROR(EINVAL);
  391. goto fail;
  392. }
  393. if (!(match = extract_inout(cur->name, &open_outputs)))
  394. continue;
  395. ret = avfilter_link(match->filter_ctx, match->pad_idx,
  396. cur->filter_ctx, cur->pad_idx);
  397. avfilter_inout_free(&match);
  398. if (ret < 0)
  399. goto fail;
  400. }
  401. /* Last output can be omitted if it is "[out]" */
  402. if (outputs && !outputs->name)
  403. outputs->name = av_strdup("out");
  404. for (cur = outputs; cur; cur = cur->next) {
  405. if (!cur->name) {
  406. av_log(log_ctx, AV_LOG_ERROR,
  407. "Invalid filterchain containing an unlabelled output pad: \"%s\"\n",
  408. filters);
  409. ret = AVERROR(EINVAL);
  410. goto fail;
  411. }
  412. if (!(match = extract_inout(cur->name, &open_inputs)))
  413. continue;
  414. ret = avfilter_link(cur->filter_ctx, cur->pad_idx,
  415. match->filter_ctx, match->pad_idx);
  416. avfilter_inout_free(&match);
  417. if (ret < 0)
  418. goto fail;
  419. }
  420. fail:
  421. if (ret < 0) {
  422. for (; graph->filter_count > 0; graph->filter_count--)
  423. avfilter_free(graph->filters[graph->filter_count - 1]);
  424. av_freep(&graph->filters);
  425. }
  426. avfilter_inout_free(&inputs);
  427. avfilter_inout_free(&outputs);
  428. /* clear open_in/outputs only if not passed as parameters */
  429. if (open_inputs_ptr) *open_inputs_ptr = open_inputs;
  430. else avfilter_inout_free(&open_inputs);
  431. if (open_outputs_ptr) *open_outputs_ptr = open_outputs;
  432. else avfilter_inout_free(&open_outputs);
  433. return ret;
  434. }
  435. #else
  436. int index = 0, ret = 0;
  437. char chr = 0;
  438. AVFilterInOut *curr_inputs = NULL;
  439. AVFilterInOut *open_inputs = open_inputs_ptr ? *open_inputs_ptr : NULL;
  440. AVFilterInOut *open_outputs = open_outputs_ptr ? *open_outputs_ptr : NULL;
  441. if ((ret = parse_sws_flags(&filters, graph)) < 0)
  442. goto end;
  443. do {
  444. AVFilterContext *filter;
  445. const char *filterchain = filters;
  446. filters += strspn(filters, WHITESPACES);
  447. if ((ret = parse_inputs(&filters, &curr_inputs, &open_outputs, log_ctx)) < 0)
  448. goto end;
  449. if ((ret = parse_filter(&filter, &filters, graph, index, log_ctx)) < 0)
  450. goto end;
  451. if (filter->input_count == 1 && !curr_inputs && !index) {
  452. /* First input pad, assume it is "[in]" if not specified */
  453. const char *tmp = "[in]";
  454. if ((ret = parse_inputs(&tmp, &curr_inputs, &open_outputs, log_ctx)) < 0)
  455. goto end;
  456. }
  457. if ((ret = link_filter_inouts(filter, &curr_inputs, &open_inputs, log_ctx)) < 0)
  458. goto end;
  459. if ((ret = parse_outputs(&filters, &curr_inputs, &open_inputs, &open_outputs,
  460. log_ctx)) < 0)
  461. goto end;
  462. filters += strspn(filters, WHITESPACES);
  463. chr = *filters++;
  464. if (chr == ';' && curr_inputs) {
  465. av_log(log_ctx, AV_LOG_ERROR,
  466. "Invalid filterchain containing an unlabelled output pad: \"%s\"\n",
  467. filterchain);
  468. ret = AVERROR(EINVAL);
  469. goto end;
  470. }
  471. index++;
  472. } while (chr == ',' || chr == ';');
  473. if (chr) {
  474. av_log(log_ctx, AV_LOG_ERROR,
  475. "Unable to parse graph description substring: \"%s\"\n",
  476. filters - 1);
  477. ret = AVERROR(EINVAL);
  478. goto end;
  479. }
  480. if (curr_inputs) {
  481. /* Last output pad, assume it is "[out]" if not specified */
  482. const char *tmp = "[out]";
  483. if ((ret = parse_outputs(&tmp, &curr_inputs, &open_inputs, &open_outputs,
  484. log_ctx)) < 0)
  485. goto end;
  486. }
  487. end:
  488. /* clear open_in/outputs only if not passed as parameters */
  489. if (open_inputs_ptr) *open_inputs_ptr = open_inputs;
  490. else avfilter_inout_free(&open_inputs);
  491. if (open_outputs_ptr) *open_outputs_ptr = open_outputs;
  492. else avfilter_inout_free(&open_outputs);
  493. avfilter_inout_free(&curr_inputs);
  494. if (ret < 0) {
  495. for (; graph->filter_count > 0; graph->filter_count--)
  496. avfilter_free(graph->filters[graph->filter_count - 1]);
  497. av_freep(&graph->filters);
  498. }
  499. return ret;
  500. }
  501. #endif