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.

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