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.

515 lines
15KB

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