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.

633 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. if ((ret = link_filter(input->filter_ctx, input->pad_idx,
  319. match->filter_ctx, match->pad_idx, log_ctx)) < 0) {
  320. av_free(name);
  321. return ret;
  322. }
  323. av_freep(&match->name);
  324. av_freep(&name);
  325. av_freep(&match);
  326. av_freep(&input);
  327. } else {
  328. /* Not in the list, so add the first input as an open_output */
  329. input->name = name;
  330. insert_inout(open_outputs, input);
  331. }
  332. *buf += strspn(*buf, WHITESPACES);
  333. pad++;
  334. }
  335. return pad;
  336. }
  337. static int parse_sws_flags(const char **buf, AVFilterGraph *graph)
  338. {
  339. char *p = strchr(*buf, ';');
  340. if (strncmp(*buf, "sws_flags=", 10))
  341. return 0;
  342. if (!p) {
  343. av_log(graph, AV_LOG_ERROR, "sws_flags not terminated with ';'.\n");
  344. return AVERROR(EINVAL);
  345. }
  346. *buf += 4; // keep the 'flags=' part
  347. av_freep(&graph->scale_sws_opts);
  348. if (!(graph->scale_sws_opts = av_mallocz(p - *buf + 1)))
  349. return AVERROR(ENOMEM);
  350. av_strlcpy(graph->scale_sws_opts, *buf, p - *buf + 1);
  351. *buf = p + 1;
  352. return 0;
  353. }
  354. int avfilter_graph_parse2(AVFilterGraph *graph, const char *filters,
  355. AVFilterInOut **inputs,
  356. AVFilterInOut **outputs)
  357. {
  358. int index = 0, ret = 0;
  359. char chr = 0;
  360. AVFilterInOut *curr_inputs = NULL, *open_inputs = NULL, *open_outputs = NULL;
  361. filters += strspn(filters, WHITESPACES);
  362. if ((ret = parse_sws_flags(&filters, graph)) < 0)
  363. goto fail;
  364. do {
  365. AVFilterContext *filter;
  366. filters += strspn(filters, WHITESPACES);
  367. if ((ret = parse_inputs(&filters, &curr_inputs, &open_outputs, graph)) < 0)
  368. goto end;
  369. if ((ret = parse_filter(&filter, &filters, graph, index, graph)) < 0)
  370. goto end;
  371. if ((ret = link_filter_inouts(filter, &curr_inputs, &open_inputs, graph)) < 0)
  372. goto end;
  373. if ((ret = parse_outputs(&filters, &curr_inputs, &open_inputs, &open_outputs,
  374. graph)) < 0)
  375. goto end;
  376. filters += strspn(filters, WHITESPACES);
  377. chr = *filters++;
  378. if (chr == ';' && curr_inputs)
  379. append_inout(&open_outputs, &curr_inputs);
  380. index++;
  381. } while (chr == ',' || chr == ';');
  382. if (chr) {
  383. av_log(graph, AV_LOG_ERROR,
  384. "Unable to parse graph description substring: \"%s\"\n",
  385. filters - 1);
  386. ret = AVERROR(EINVAL);
  387. goto end;
  388. }
  389. append_inout(&open_outputs, &curr_inputs);
  390. *inputs = open_inputs;
  391. *outputs = open_outputs;
  392. return 0;
  393. fail:end:
  394. while (graph->nb_filters)
  395. avfilter_free(graph->filters[0]);
  396. av_freep(&graph->filters);
  397. avfilter_inout_free(&open_inputs);
  398. avfilter_inout_free(&open_outputs);
  399. avfilter_inout_free(&curr_inputs);
  400. *inputs = NULL;
  401. *outputs = NULL;
  402. return ret;
  403. }
  404. int avfilter_graph_parse(AVFilterGraph *graph, const char *filters,
  405. AVFilterInOut *open_inputs,
  406. AVFilterInOut *open_outputs, void *log_ctx)
  407. {
  408. int ret;
  409. AVFilterInOut *cur, *match, *inputs = NULL, *outputs = NULL;
  410. if ((ret = avfilter_graph_parse2(graph, filters, &inputs, &outputs)) < 0)
  411. goto fail;
  412. /* First input can be omitted if it is "[in]" */
  413. if (inputs && !inputs->name)
  414. inputs->name = av_strdup("in");
  415. for (cur = inputs; cur; cur = cur->next) {
  416. if (!cur->name) {
  417. av_log(log_ctx, AV_LOG_ERROR,
  418. "Not enough inputs specified for the \"%s\" filter.\n",
  419. cur->filter_ctx->filter->name);
  420. ret = AVERROR(EINVAL);
  421. goto fail;
  422. }
  423. if (!(match = extract_inout(cur->name, &open_outputs)))
  424. continue;
  425. ret = avfilter_link(match->filter_ctx, match->pad_idx,
  426. cur->filter_ctx, cur->pad_idx);
  427. avfilter_inout_free(&match);
  428. if (ret < 0)
  429. goto fail;
  430. }
  431. /* Last output can be omitted if it is "[out]" */
  432. if (outputs && !outputs->name)
  433. outputs->name = av_strdup("out");
  434. for (cur = outputs; cur; cur = cur->next) {
  435. if (!cur->name) {
  436. av_log(log_ctx, AV_LOG_ERROR,
  437. "Invalid filterchain containing an unlabelled output pad: \"%s\"\n",
  438. filters);
  439. ret = AVERROR(EINVAL);
  440. goto fail;
  441. }
  442. if (!(match = extract_inout(cur->name, &open_inputs)))
  443. continue;
  444. ret = avfilter_link(cur->filter_ctx, cur->pad_idx,
  445. match->filter_ctx, match->pad_idx);
  446. avfilter_inout_free(&match);
  447. if (ret < 0)
  448. goto fail;
  449. }
  450. fail:
  451. if (ret < 0) {
  452. while (graph->nb_filters)
  453. avfilter_free(graph->filters[0]);
  454. av_freep(&graph->filters);
  455. }
  456. avfilter_inout_free(&inputs);
  457. avfilter_inout_free(&outputs);
  458. avfilter_inout_free(&open_inputs);
  459. avfilter_inout_free(&open_outputs);
  460. return ret;
  461. }
  462. int avfilter_graph_parse_ptr(AVFilterGraph *graph, const char *filters,
  463. AVFilterInOut **open_inputs_ptr, AVFilterInOut **open_outputs_ptr,
  464. void *log_ctx)
  465. {
  466. int index = 0, ret = 0;
  467. char chr = 0;
  468. AVFilterInOut *curr_inputs = NULL;
  469. AVFilterInOut *open_inputs = open_inputs_ptr ? *open_inputs_ptr : NULL;
  470. AVFilterInOut *open_outputs = open_outputs_ptr ? *open_outputs_ptr : NULL;
  471. if ((ret = parse_sws_flags(&filters, graph)) < 0)
  472. goto end;
  473. do {
  474. AVFilterContext *filter;
  475. const char *filterchain = filters;
  476. filters += strspn(filters, WHITESPACES);
  477. if ((ret = parse_inputs(&filters, &curr_inputs, &open_outputs, log_ctx)) < 0)
  478. goto end;
  479. if ((ret = parse_filter(&filter, &filters, graph, index, log_ctx)) < 0)
  480. goto end;
  481. if (filter->nb_inputs == 1 && !curr_inputs && !index) {
  482. /* First input pad, assume it is "[in]" if not specified */
  483. const char *tmp = "[in]";
  484. if ((ret = parse_inputs(&tmp, &curr_inputs, &open_outputs, log_ctx)) < 0)
  485. goto end;
  486. }
  487. if ((ret = link_filter_inouts(filter, &curr_inputs, &open_inputs, log_ctx)) < 0)
  488. goto end;
  489. if ((ret = parse_outputs(&filters, &curr_inputs, &open_inputs, &open_outputs,
  490. log_ctx)) < 0)
  491. goto end;
  492. filters += strspn(filters, WHITESPACES);
  493. chr = *filters++;
  494. if (chr == ';' && curr_inputs) {
  495. av_log(log_ctx, AV_LOG_ERROR,
  496. "Invalid filterchain containing an unlabelled output pad: \"%s\"\n",
  497. filterchain);
  498. ret = AVERROR(EINVAL);
  499. goto end;
  500. }
  501. index++;
  502. } while (chr == ',' || chr == ';');
  503. if (chr) {
  504. av_log(log_ctx, AV_LOG_ERROR,
  505. "Unable to parse graph description substring: \"%s\"\n",
  506. filters - 1);
  507. ret = AVERROR(EINVAL);
  508. goto end;
  509. }
  510. if (curr_inputs) {
  511. /* Last output pad, assume it is "[out]" if not specified */
  512. const char *tmp = "[out]";
  513. if ((ret = parse_outputs(&tmp, &curr_inputs, &open_inputs, &open_outputs,
  514. log_ctx)) < 0)
  515. goto end;
  516. }
  517. end:
  518. /* clear open_in/outputs only if not passed as parameters */
  519. if (open_inputs_ptr) *open_inputs_ptr = open_inputs;
  520. else avfilter_inout_free(&open_inputs);
  521. if (open_outputs_ptr) *open_outputs_ptr = open_outputs;
  522. else avfilter_inout_free(&open_outputs);
  523. avfilter_inout_free(&curr_inputs);
  524. if (ret < 0) {
  525. while (graph->nb_filters)
  526. avfilter_free(graph->filters[0]);
  527. av_freep(&graph->filters);
  528. }
  529. return ret;
  530. }