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.

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