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.

626 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 (**buf == '=') {
  168. (*buf)++;
  169. opts = av_get_token(buf, "[],;");
  170. }
  171. ret = create_filter(filt_ctx, graph, index, name, opts, log_ctx);
  172. av_free(name);
  173. av_free(opts);
  174. return ret;
  175. }
  176. AVFilterInOut *avfilter_inout_alloc(void)
  177. {
  178. return av_mallocz(sizeof(AVFilterInOut));
  179. }
  180. void avfilter_inout_free(AVFilterInOut **inout)
  181. {
  182. while (*inout) {
  183. AVFilterInOut *next = (*inout)->next;
  184. av_freep(&(*inout)->name);
  185. av_freep(inout);
  186. *inout = next;
  187. }
  188. }
  189. static AVFilterInOut *extract_inout(const char *label, AVFilterInOut **links)
  190. {
  191. AVFilterInOut *ret;
  192. while (*links && (!(*links)->name || strcmp((*links)->name, label)))
  193. links = &((*links)->next);
  194. ret = *links;
  195. if (ret) {
  196. *links = ret->next;
  197. ret->next = NULL;
  198. }
  199. return ret;
  200. }
  201. static void insert_inout(AVFilterInOut **inouts, AVFilterInOut *element)
  202. {
  203. element->next = *inouts;
  204. *inouts = element;
  205. }
  206. static void append_inout(AVFilterInOut **inouts, AVFilterInOut **element)
  207. {
  208. while (*inouts && (*inouts)->next)
  209. inouts = &((*inouts)->next);
  210. if (!*inouts)
  211. *inouts = *element;
  212. else
  213. (*inouts)->next = *element;
  214. *element = NULL;
  215. }
  216. static int link_filter_inouts(AVFilterContext *filt_ctx,
  217. AVFilterInOut **curr_inputs,
  218. AVFilterInOut **open_inputs, void *log_ctx)
  219. {
  220. int pad, ret;
  221. for (pad = 0; pad < filt_ctx->nb_inputs; pad++) {
  222. AVFilterInOut *p = *curr_inputs;
  223. if (p) {
  224. *curr_inputs = (*curr_inputs)->next;
  225. p->next = NULL;
  226. } else if (!(p = av_mallocz(sizeof(*p))))
  227. return AVERROR(ENOMEM);
  228. if (p->filter_ctx) {
  229. ret = link_filter(p->filter_ctx, p->pad_idx, filt_ctx, pad, log_ctx);
  230. av_freep(&p->name);
  231. av_freep(&p);
  232. if (ret < 0)
  233. return ret;
  234. } else {
  235. p->filter_ctx = filt_ctx;
  236. p->pad_idx = pad;
  237. append_inout(open_inputs, &p);
  238. }
  239. }
  240. if (*curr_inputs) {
  241. av_log(log_ctx, AV_LOG_ERROR,
  242. "Too many inputs specified for the \"%s\" filter.\n",
  243. filt_ctx->filter->name);
  244. return AVERROR(EINVAL);
  245. }
  246. pad = filt_ctx->nb_outputs;
  247. while (pad--) {
  248. AVFilterInOut *currlinkn = av_mallocz(sizeof(AVFilterInOut));
  249. if (!currlinkn)
  250. return AVERROR(ENOMEM);
  251. currlinkn->filter_ctx = filt_ctx;
  252. currlinkn->pad_idx = pad;
  253. insert_inout(curr_inputs, currlinkn);
  254. }
  255. return 0;
  256. }
  257. static int parse_inputs(const char **buf, AVFilterInOut **curr_inputs,
  258. AVFilterInOut **open_outputs, void *log_ctx)
  259. {
  260. AVFilterInOut *parsed_inputs = NULL;
  261. int pad = 0;
  262. while (**buf == '[') {
  263. char *name = parse_link_name(buf, log_ctx);
  264. AVFilterInOut *match;
  265. if (!name) {
  266. avfilter_inout_free(&parsed_inputs);
  267. return AVERROR(EINVAL);
  268. }
  269. /* First check if the label is not in the open_outputs list */
  270. match = extract_inout(name, open_outputs);
  271. if (match) {
  272. av_free(name);
  273. } else {
  274. /* Not in the list, so add it as an input */
  275. if (!(match = av_mallocz(sizeof(AVFilterInOut)))) {
  276. avfilter_inout_free(&parsed_inputs);
  277. av_free(name);
  278. return AVERROR(ENOMEM);
  279. }
  280. match->name = name;
  281. match->pad_idx = pad;
  282. }
  283. append_inout(&parsed_inputs, &match);
  284. *buf += strspn(*buf, WHITESPACES);
  285. pad++;
  286. }
  287. append_inout(&parsed_inputs, curr_inputs);
  288. *curr_inputs = parsed_inputs;
  289. return pad;
  290. }
  291. static int parse_outputs(const char **buf, AVFilterInOut **curr_inputs,
  292. AVFilterInOut **open_inputs,
  293. AVFilterInOut **open_outputs, void *log_ctx)
  294. {
  295. int ret, pad = 0;
  296. while (**buf == '[') {
  297. char *name = parse_link_name(buf, log_ctx);
  298. AVFilterInOut *match;
  299. AVFilterInOut *input = *curr_inputs;
  300. if (!name)
  301. return AVERROR(EINVAL);
  302. if (!input) {
  303. av_log(log_ctx, AV_LOG_ERROR,
  304. "No output pad can be associated to link label '%s'.\n", name);
  305. av_free(name);
  306. return AVERROR(EINVAL);
  307. }
  308. *curr_inputs = (*curr_inputs)->next;
  309. /* First check if the label is not in the open_inputs list */
  310. match = extract_inout(name, open_inputs);
  311. if (match) {
  312. if ((ret = link_filter(input->filter_ctx, input->pad_idx,
  313. match->filter_ctx, match->pad_idx, log_ctx)) < 0) {
  314. av_free(name);
  315. return ret;
  316. }
  317. av_freep(&match->name);
  318. av_freep(&name);
  319. av_freep(&match);
  320. av_freep(&input);
  321. } else {
  322. /* Not in the list, so add the first input as an open_output */
  323. input->name = name;
  324. insert_inout(open_outputs, input);
  325. }
  326. *buf += strspn(*buf, WHITESPACES);
  327. pad++;
  328. }
  329. return pad;
  330. }
  331. static int parse_sws_flags(const char **buf, AVFilterGraph *graph)
  332. {
  333. char *p = strchr(*buf, ';');
  334. if (strncmp(*buf, "sws_flags=", 10))
  335. return 0;
  336. if (!p) {
  337. av_log(graph, AV_LOG_ERROR, "sws_flags not terminated with ';'.\n");
  338. return AVERROR(EINVAL);
  339. }
  340. *buf += 4; // keep the 'flags=' part
  341. av_freep(&graph->scale_sws_opts);
  342. if (!(graph->scale_sws_opts = av_mallocz(p - *buf + 1)))
  343. return AVERROR(ENOMEM);
  344. av_strlcpy(graph->scale_sws_opts, *buf, p - *buf + 1);
  345. *buf = p + 1;
  346. return 0;
  347. }
  348. int avfilter_graph_parse2(AVFilterGraph *graph, const char *filters,
  349. AVFilterInOut **inputs,
  350. AVFilterInOut **outputs)
  351. {
  352. int index = 0, ret = 0;
  353. char chr = 0;
  354. AVFilterInOut *curr_inputs = NULL, *open_inputs = NULL, *open_outputs = NULL;
  355. filters += strspn(filters, WHITESPACES);
  356. if ((ret = parse_sws_flags(&filters, graph)) < 0)
  357. goto fail;
  358. do {
  359. AVFilterContext *filter;
  360. filters += strspn(filters, WHITESPACES);
  361. if ((ret = parse_inputs(&filters, &curr_inputs, &open_outputs, graph)) < 0)
  362. goto end;
  363. if ((ret = parse_filter(&filter, &filters, graph, index, graph)) < 0)
  364. goto end;
  365. if ((ret = link_filter_inouts(filter, &curr_inputs, &open_inputs, graph)) < 0)
  366. goto end;
  367. if ((ret = parse_outputs(&filters, &curr_inputs, &open_inputs, &open_outputs,
  368. graph)) < 0)
  369. goto end;
  370. filters += strspn(filters, WHITESPACES);
  371. chr = *filters++;
  372. if (chr == ';' && curr_inputs)
  373. append_inout(&open_outputs, &curr_inputs);
  374. index++;
  375. } while (chr == ',' || chr == ';');
  376. if (chr) {
  377. av_log(graph, AV_LOG_ERROR,
  378. "Unable to parse graph description substring: \"%s\"\n",
  379. filters - 1);
  380. ret = AVERROR(EINVAL);
  381. goto end;
  382. }
  383. append_inout(&open_outputs, &curr_inputs);
  384. *inputs = open_inputs;
  385. *outputs = open_outputs;
  386. return 0;
  387. fail:end:
  388. while (graph->nb_filters)
  389. avfilter_free(graph->filters[0]);
  390. av_freep(&graph->filters);
  391. avfilter_inout_free(&open_inputs);
  392. avfilter_inout_free(&open_outputs);
  393. avfilter_inout_free(&curr_inputs);
  394. *inputs = NULL;
  395. *outputs = NULL;
  396. return ret;
  397. }
  398. int avfilter_graph_parse(AVFilterGraph *graph, const char *filters,
  399. AVFilterInOut *open_inputs,
  400. AVFilterInOut *open_outputs, void *log_ctx)
  401. {
  402. int ret;
  403. AVFilterInOut *cur, *match, *inputs = NULL, *outputs = NULL;
  404. if ((ret = avfilter_graph_parse2(graph, filters, &inputs, &outputs)) < 0)
  405. goto fail;
  406. /* First input can be omitted if it is "[in]" */
  407. if (inputs && !inputs->name)
  408. inputs->name = av_strdup("in");
  409. for (cur = inputs; cur; cur = cur->next) {
  410. if (!cur->name) {
  411. av_log(log_ctx, AV_LOG_ERROR,
  412. "Not enough inputs specified for the \"%s\" filter.\n",
  413. cur->filter_ctx->filter->name);
  414. ret = AVERROR(EINVAL);
  415. goto fail;
  416. }
  417. if (!(match = extract_inout(cur->name, &open_outputs)))
  418. continue;
  419. ret = avfilter_link(match->filter_ctx, match->pad_idx,
  420. cur->filter_ctx, cur->pad_idx);
  421. avfilter_inout_free(&match);
  422. if (ret < 0)
  423. goto fail;
  424. }
  425. /* Last output can be omitted if it is "[out]" */
  426. if (outputs && !outputs->name)
  427. outputs->name = av_strdup("out");
  428. for (cur = outputs; cur; cur = cur->next) {
  429. if (!cur->name) {
  430. av_log(log_ctx, AV_LOG_ERROR,
  431. "Invalid filterchain containing an unlabelled output pad: \"%s\"\n",
  432. filters);
  433. ret = AVERROR(EINVAL);
  434. goto fail;
  435. }
  436. if (!(match = extract_inout(cur->name, &open_inputs)))
  437. continue;
  438. ret = avfilter_link(cur->filter_ctx, cur->pad_idx,
  439. match->filter_ctx, match->pad_idx);
  440. avfilter_inout_free(&match);
  441. if (ret < 0)
  442. goto fail;
  443. }
  444. fail:
  445. if (ret < 0) {
  446. while (graph->nb_filters)
  447. avfilter_free(graph->filters[0]);
  448. av_freep(&graph->filters);
  449. }
  450. avfilter_inout_free(&inputs);
  451. avfilter_inout_free(&outputs);
  452. avfilter_inout_free(&open_inputs);
  453. avfilter_inout_free(&open_outputs);
  454. return ret;
  455. }
  456. int avfilter_graph_parse_ptr(AVFilterGraph *graph, const char *filters,
  457. AVFilterInOut **open_inputs_ptr, AVFilterInOut **open_outputs_ptr,
  458. void *log_ctx)
  459. {
  460. int index = 0, ret = 0;
  461. char chr = 0;
  462. AVFilterInOut *curr_inputs = NULL;
  463. AVFilterInOut *open_inputs = open_inputs_ptr ? *open_inputs_ptr : NULL;
  464. AVFilterInOut *open_outputs = open_outputs_ptr ? *open_outputs_ptr : NULL;
  465. if ((ret = parse_sws_flags(&filters, graph)) < 0)
  466. goto end;
  467. do {
  468. AVFilterContext *filter;
  469. const char *filterchain = filters;
  470. filters += strspn(filters, WHITESPACES);
  471. if ((ret = parse_inputs(&filters, &curr_inputs, &open_outputs, log_ctx)) < 0)
  472. goto end;
  473. if ((ret = parse_filter(&filter, &filters, graph, index, log_ctx)) < 0)
  474. goto end;
  475. if (filter->nb_inputs == 1 && !curr_inputs && !index) {
  476. /* First input pad, assume it is "[in]" if not specified */
  477. const char *tmp = "[in]";
  478. if ((ret = parse_inputs(&tmp, &curr_inputs, &open_outputs, log_ctx)) < 0)
  479. goto end;
  480. }
  481. if ((ret = link_filter_inouts(filter, &curr_inputs, &open_inputs, log_ctx)) < 0)
  482. goto end;
  483. if ((ret = parse_outputs(&filters, &curr_inputs, &open_inputs, &open_outputs,
  484. log_ctx)) < 0)
  485. goto end;
  486. filters += strspn(filters, WHITESPACES);
  487. chr = *filters++;
  488. if (chr == ';' && curr_inputs) {
  489. av_log(log_ctx, AV_LOG_ERROR,
  490. "Invalid filterchain containing an unlabelled output pad: \"%s\"\n",
  491. filterchain);
  492. ret = AVERROR(EINVAL);
  493. goto end;
  494. }
  495. index++;
  496. } while (chr == ',' || chr == ';');
  497. if (chr) {
  498. av_log(log_ctx, AV_LOG_ERROR,
  499. "Unable to parse graph description substring: \"%s\"\n",
  500. filters - 1);
  501. ret = AVERROR(EINVAL);
  502. goto end;
  503. }
  504. if (curr_inputs) {
  505. /* Last output pad, assume it is "[out]" if not specified */
  506. const char *tmp = "[out]";
  507. if ((ret = parse_outputs(&tmp, &curr_inputs, &open_inputs, &open_outputs,
  508. log_ctx)) < 0)
  509. goto end;
  510. }
  511. end:
  512. /* clear open_in/outputs only if not passed as parameters */
  513. if (open_inputs_ptr) *open_inputs_ptr = open_inputs;
  514. else avfilter_inout_free(&open_inputs);
  515. if (open_outputs_ptr) *open_outputs_ptr = open_outputs;
  516. else avfilter_inout_free(&open_outputs);
  517. avfilter_inout_free(&curr_inputs);
  518. if (ret < 0) {
  519. while (graph->nb_filters)
  520. avfilter_free(graph->filters[0]);
  521. av_freep(&graph->filters);
  522. }
  523. return ret;
  524. }