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.

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