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.

506 lines
14KB

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