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.

507 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. static void free_inout(AVFilterInOut *head)
  151. {
  152. while (head) {
  153. AVFilterInOut *next = head->next;
  154. av_free(head->name);
  155. av_free(head);
  156. head = next;
  157. }
  158. }
  159. static AVFilterInOut *extract_inout(const char *label, AVFilterInOut **links)
  160. {
  161. AVFilterInOut *ret;
  162. while (*links && (!(*links)->name || strcmp((*links)->name, label)))
  163. links = &((*links)->next);
  164. ret = *links;
  165. if (ret) {
  166. *links = ret->next;
  167. ret->next = NULL;
  168. }
  169. return ret;
  170. }
  171. static void insert_inout(AVFilterInOut **inouts, AVFilterInOut *element)
  172. {
  173. element->next = *inouts;
  174. *inouts = element;
  175. }
  176. static void append_inout(AVFilterInOut **inouts, AVFilterInOut **element)
  177. {
  178. while (*inouts && (*inouts)->next)
  179. inouts = &((*inouts)->next);
  180. if (!*inouts)
  181. *inouts = *element;
  182. else
  183. (*inouts)->next = *element;
  184. *element = NULL;
  185. }
  186. static int link_filter_inouts(AVFilterContext *filt_ctx,
  187. AVFilterInOut **curr_inputs,
  188. AVFilterInOut **open_inputs, void *log_ctx)
  189. {
  190. int pad, ret;
  191. for (pad = 0; pad < filt_ctx->input_count; pad++) {
  192. AVFilterInOut *p = *curr_inputs;
  193. if (p)
  194. *curr_inputs = (*curr_inputs)->next;
  195. else if (!(p = av_mallocz(sizeof(*p))))
  196. return AVERROR(ENOMEM);
  197. if (p->filter_ctx) {
  198. if ((ret = link_filter(p->filter_ctx, p->pad_idx, filt_ctx, pad, log_ctx)) < 0)
  199. return ret;
  200. av_free(p->name);
  201. av_free(p);
  202. } else {
  203. p->filter_ctx = filt_ctx;
  204. p->pad_idx = pad;
  205. insert_inout(open_inputs, p);
  206. }
  207. }
  208. if (*curr_inputs) {
  209. av_log(log_ctx, AV_LOG_ERROR,
  210. "Too many inputs specified for the \"%s\" filter.\n",
  211. filt_ctx->filter->name);
  212. return AVERROR(EINVAL);
  213. }
  214. pad = filt_ctx->output_count;
  215. while (pad--) {
  216. AVFilterInOut *currlinkn = av_mallocz(sizeof(AVFilterInOut));
  217. if (!currlinkn)
  218. return AVERROR(ENOMEM);
  219. currlinkn->filter_ctx = filt_ctx;
  220. currlinkn->pad_idx = pad;
  221. insert_inout(curr_inputs, currlinkn);
  222. }
  223. return 0;
  224. }
  225. static int parse_inputs(const char **buf, AVFilterInOut **curr_inputs,
  226. AVFilterInOut **open_outputs, void *log_ctx)
  227. {
  228. AVFilterInOut *parsed_inputs = NULL;
  229. int pad = 0;
  230. while (**buf == '[') {
  231. char *name = parse_link_name(buf, log_ctx);
  232. AVFilterInOut *match;
  233. if (!name)
  234. return AVERROR(EINVAL);
  235. /* First check if the label is not in the open_outputs list */
  236. match = extract_inout(name, open_outputs);
  237. if (match) {
  238. av_free(name);
  239. } else {
  240. /* Not in the list, so add it as an input */
  241. if (!(match = av_mallocz(sizeof(AVFilterInOut))))
  242. return AVERROR(ENOMEM);
  243. match->name = name;
  244. match->pad_idx = pad;
  245. }
  246. append_inout(&parsed_inputs, &match);
  247. *buf += strspn(*buf, WHITESPACES);
  248. pad++;
  249. }
  250. append_inout(&parsed_inputs, curr_inputs);
  251. *curr_inputs = parsed_inputs;
  252. return pad;
  253. }
  254. static int parse_outputs(const char **buf, AVFilterInOut **curr_inputs,
  255. AVFilterInOut **open_inputs,
  256. AVFilterInOut **open_outputs, void *log_ctx)
  257. {
  258. int ret, pad = 0;
  259. while (**buf == '[') {
  260. char *name = parse_link_name(buf, log_ctx);
  261. AVFilterInOut *match;
  262. AVFilterInOut *input = *curr_inputs;
  263. if (!input) {
  264. av_log(log_ctx, AV_LOG_ERROR,
  265. "No output pad can be associated to link label '%s'.\n",
  266. name);
  267. return AVERROR(EINVAL);
  268. }
  269. *curr_inputs = (*curr_inputs)->next;
  270. if (!name)
  271. return AVERROR(EINVAL);
  272. /* First check if the label is not in the open_inputs list */
  273. match = extract_inout(name, open_inputs);
  274. if (match) {
  275. if ((ret = link_filter(input->filter_ctx, input->pad_idx,
  276. match->filter_ctx, match->pad_idx, log_ctx)) < 0)
  277. return ret;
  278. av_free(match->name);
  279. av_free(name);
  280. av_free(match);
  281. av_free(input);
  282. } else {
  283. /* Not in the list, so add the first input as a open_output */
  284. input->name = name;
  285. insert_inout(open_outputs, input);
  286. }
  287. *buf += strspn(*buf, WHITESPACES);
  288. pad++;
  289. }
  290. return pad;
  291. }
  292. #if FF_API_GRAPH_AVCLASS
  293. #define log_ctx graph
  294. #else
  295. #define log_ctx NULL
  296. #endif
  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(log_ctx, 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, log_ctx)) < 0)
  328. goto fail;
  329. if ((ret = parse_filter(&filter, &filters, graph, index, log_ctx)) < 0)
  330. goto fail;
  331. if ((ret = link_filter_inouts(filter, &curr_inputs, &open_inputs, log_ctx)) < 0)
  332. goto fail;
  333. if ((ret = parse_outputs(&filters, &curr_inputs, &open_inputs, &open_outputs,
  334. log_ctx)) < 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(log_ctx, 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. free_inout(open_inputs);
  358. free_inout(open_outputs);
  359. free_inout(curr_inputs);
  360. *inputs = NULL;
  361. *outputs = NULL;
  362. return ret;
  363. }
  364. #undef log_ctx
  365. int avfilter_graph_parse(AVFilterGraph *graph, const char *filters,
  366. AVFilterInOut *open_inputs,
  367. AVFilterInOut *open_outputs, void *log_ctx)
  368. {
  369. int ret;
  370. AVFilterInOut *cur, *match, *inputs = NULL, *outputs = NULL;
  371. if ((ret = avfilter_graph_parse2(graph, filters, &inputs, &outputs)) < 0)
  372. goto fail;
  373. /* First input can be omitted if it is "[in]" */
  374. if (inputs && !inputs->name)
  375. inputs->name = av_strdup("in");
  376. for (cur = inputs; cur; cur = cur->next) {
  377. if (!cur->name) {
  378. av_log(log_ctx, AV_LOG_ERROR,
  379. "Not enough inputs specified for the \"%s\" filter.\n",
  380. cur->filter_ctx->filter->name);
  381. ret = AVERROR(EINVAL);
  382. goto fail;
  383. }
  384. if (!(match = extract_inout(cur->name, &open_outputs)))
  385. continue;
  386. ret = avfilter_link(match->filter_ctx, match->pad_idx,
  387. cur->filter_ctx, cur->pad_idx);
  388. free_inout(match);
  389. if (ret < 0)
  390. goto fail;
  391. }
  392. /* Last output can be omitted if it is "[out]" */
  393. if (outputs && !outputs->name)
  394. outputs->name = av_strdup("out");
  395. for (cur = outputs; cur; cur = cur->next) {
  396. if (!cur->name) {
  397. av_log(log_ctx, AV_LOG_ERROR,
  398. "Invalid filterchain containing an unlabelled output pad: \"%s\"\n",
  399. filters);
  400. ret = AVERROR(EINVAL);
  401. goto fail;
  402. }
  403. if (!(match = extract_inout(cur->name, &open_inputs)))
  404. continue;
  405. ret = avfilter_link(cur->filter_ctx, cur->pad_idx,
  406. match->filter_ctx, match->pad_idx);
  407. free_inout(match);
  408. if (ret < 0)
  409. goto fail;
  410. }
  411. fail:
  412. if (ret < 0) {
  413. for (; graph->filter_count > 0; graph->filter_count--)
  414. avfilter_free(graph->filters[graph->filter_count - 1]);
  415. av_freep(&graph->filters);
  416. }
  417. free_inout(inputs);
  418. free_inout(outputs);
  419. free_inout(open_inputs);
  420. free_inout(open_outputs);
  421. return ret;
  422. }