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.

404 lines
12KB

  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 <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. AVClass *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, AVClass *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 ctx the filtergraph context
  76. * @param put here a filter context in case of successful creation and configuration, NULL otherwise.
  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, AVClass *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") && !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 put here a pointer to the created filter context on
  125. * success, NULL otherwise
  126. * @param buf pointer to the buffer to parse, *buf will be updated to
  127. * point to the char next after the parsed string
  128. * @param index an index which is assigned to the created filter
  129. * instance, and which is supposed to be unique for each filter
  130. * instance added to the filtergraph
  131. * @return 0 in case of success, a negative AVERROR code otherwise
  132. */
  133. static int parse_filter(AVFilterContext **filt_ctx, const char **buf, AVFilterGraph *graph,
  134. int index, AVClass *log_ctx)
  135. {
  136. char *opts = NULL;
  137. char *name = av_get_token(buf, "=,;[\n");
  138. int ret;
  139. if (**buf == '=') {
  140. (*buf)++;
  141. opts = av_get_token(buf, "[],;\n");
  142. }
  143. ret = create_filter(filt_ctx, graph, index, name, opts, log_ctx);
  144. av_free(name);
  145. av_free(opts);
  146. return ret;
  147. }
  148. static void free_inout(AVFilterInOut *head)
  149. {
  150. while (head) {
  151. AVFilterInOut *next = head->next;
  152. av_free(head->name);
  153. av_free(head);
  154. head = next;
  155. }
  156. }
  157. static AVFilterInOut *extract_inout(const char *label, AVFilterInOut **links)
  158. {
  159. AVFilterInOut *ret;
  160. while (*links && strcmp((*links)->name, label))
  161. links = &((*links)->next);
  162. ret = *links;
  163. if (ret)
  164. *links = ret->next;
  165. return ret;
  166. }
  167. static void insert_inout(AVFilterInOut **inouts, AVFilterInOut *element)
  168. {
  169. element->next = *inouts;
  170. *inouts = element;
  171. }
  172. static int link_filter_inouts(AVFilterContext *filt_ctx,
  173. AVFilterInOut **curr_inputs,
  174. AVFilterInOut **open_inputs, AVClass *log_ctx)
  175. {
  176. int pad = filt_ctx->input_count, ret;
  177. while (pad--) {
  178. AVFilterInOut *p = *curr_inputs;
  179. if (!p) {
  180. av_log(log_ctx, AV_LOG_ERROR,
  181. "Not enough inputs specified for the \"%s\" filter.\n",
  182. filt_ctx->filter->name);
  183. return AVERROR(EINVAL);
  184. }
  185. *curr_inputs = (*curr_inputs)->next;
  186. if (p->filter_ctx) {
  187. if ((ret = link_filter(p->filter_ctx, p->pad_idx, filt_ctx, pad, log_ctx)) < 0)
  188. return ret;
  189. av_free(p->name);
  190. av_free(p);
  191. } else {
  192. p->filter_ctx = filt_ctx;
  193. p->pad_idx = pad;
  194. insert_inout(open_inputs, p);
  195. }
  196. }
  197. if (*curr_inputs) {
  198. av_log(log_ctx, AV_LOG_ERROR,
  199. "Too many inputs specified for the \"%s\" filter.\n",
  200. filt_ctx->filter->name);
  201. return AVERROR(EINVAL);
  202. }
  203. pad = filt_ctx->output_count;
  204. while (pad--) {
  205. AVFilterInOut *currlinkn = av_mallocz(sizeof(AVFilterInOut));
  206. if (!currlinkn)
  207. return AVERROR(ENOMEM);
  208. currlinkn->filter_ctx = filt_ctx;
  209. currlinkn->pad_idx = pad;
  210. insert_inout(curr_inputs, currlinkn);
  211. }
  212. return 0;
  213. }
  214. static int parse_inputs(const char **buf, AVFilterInOut **curr_inputs,
  215. AVFilterInOut **open_outputs, AVClass *log_ctx)
  216. {
  217. int pad = 0;
  218. while (**buf == '[') {
  219. char *name = parse_link_name(buf, log_ctx);
  220. AVFilterInOut *match;
  221. if (!name)
  222. return AVERROR(EINVAL);
  223. /* First check if the label is not in the open_outputs list */
  224. match = extract_inout(name, open_outputs);
  225. if (match) {
  226. av_free(name);
  227. } else {
  228. /* Not in the list, so add it as an input */
  229. if (!(match = av_mallocz(sizeof(AVFilterInOut))))
  230. return AVERROR(ENOMEM);
  231. match->name = name;
  232. match->pad_idx = pad;
  233. }
  234. insert_inout(curr_inputs, match);
  235. *buf += strspn(*buf, WHITESPACES);
  236. pad++;
  237. }
  238. return pad;
  239. }
  240. static int parse_outputs(const char **buf, AVFilterInOut **curr_inputs,
  241. AVFilterInOut **open_inputs,
  242. AVFilterInOut **open_outputs, AVClass *log_ctx)
  243. {
  244. int ret, pad = 0;
  245. while (**buf == '[') {
  246. char *name = parse_link_name(buf, log_ctx);
  247. AVFilterInOut *match;
  248. AVFilterInOut *input = *curr_inputs;
  249. if (!input) {
  250. av_log(log_ctx, AV_LOG_ERROR,
  251. "No output pad can be associated to link label '%s'.\n",
  252. name);
  253. return AVERROR(EINVAL);
  254. }
  255. *curr_inputs = (*curr_inputs)->next;
  256. if (!name)
  257. return AVERROR(EINVAL);
  258. /* First check if the label is not in the open_inputs list */
  259. match = extract_inout(name, open_inputs);
  260. if (match) {
  261. if ((ret = link_filter(input->filter_ctx, input->pad_idx,
  262. match->filter_ctx, match->pad_idx, log_ctx)) < 0)
  263. return ret;
  264. av_free(match->name);
  265. av_free(name);
  266. av_free(match);
  267. av_free(input);
  268. } else {
  269. /* Not in the list, so add the first input as a open_output */
  270. input->name = name;
  271. insert_inout(open_outputs, input);
  272. }
  273. *buf += strspn(*buf, WHITESPACES);
  274. pad++;
  275. }
  276. return pad;
  277. }
  278. int avfilter_graph_parse(AVFilterGraph *graph, const char *filters,
  279. AVFilterInOut *open_inputs,
  280. AVFilterInOut *open_outputs, AVClass *log_ctx)
  281. {
  282. int index = 0, ret;
  283. char chr = 0;
  284. AVFilterInOut *curr_inputs = NULL;
  285. do {
  286. AVFilterContext *filter;
  287. const char *filterchain = filters;
  288. filters += strspn(filters, WHITESPACES);
  289. if ((ret = parse_inputs(&filters, &curr_inputs, &open_outputs, log_ctx)) < 0)
  290. goto fail;
  291. if ((ret = parse_filter(&filter, &filters, graph, index, log_ctx)) < 0)
  292. goto fail;
  293. if (filter->input_count == 1 && !curr_inputs && !index) {
  294. /* First input can be omitted if it is "[in]" */
  295. const char *tmp = "[in]";
  296. if ((ret = parse_inputs(&tmp, &curr_inputs, &open_outputs, log_ctx)) < 0)
  297. goto fail;
  298. }
  299. if ((ret = link_filter_inouts(filter, &curr_inputs, &open_inputs, log_ctx)) < 0)
  300. goto fail;
  301. if ((ret = parse_outputs(&filters, &curr_inputs, &open_inputs, &open_outputs,
  302. log_ctx)) < 0)
  303. goto fail;
  304. filters += strspn(filters, WHITESPACES);
  305. chr = *filters++;
  306. if (chr == ';' && curr_inputs) {
  307. av_log(log_ctx, AV_LOG_ERROR,
  308. "Invalid filterchain containing an unlabelled output pad: \"%s\"\n",
  309. filterchain);
  310. ret = AVERROR(EINVAL);
  311. goto fail;
  312. }
  313. index++;
  314. } while (chr == ',' || chr == ';');
  315. if (chr) {
  316. av_log(log_ctx, AV_LOG_ERROR,
  317. "Unable to parse graph description substring: \"%s\"\n",
  318. filters - 1);
  319. ret = AVERROR(EINVAL);
  320. goto fail;
  321. }
  322. if (open_inputs && !strcmp(open_inputs->name, "out") && curr_inputs) {
  323. /* Last output can be omitted if it is "[out]" */
  324. const char *tmp = "[out]";
  325. if ((ret = parse_outputs(&tmp, &curr_inputs, &open_inputs, &open_outputs,
  326. log_ctx)) < 0)
  327. goto fail;
  328. }
  329. return 0;
  330. fail:
  331. for (; graph->filter_count > 0; graph->filter_count--)
  332. avfilter_free(graph->filters[graph->filter_count - 1]);
  333. av_freep(&graph->filters);
  334. free_inout(open_inputs);
  335. free_inout(open_outputs);
  336. free_inout(curr_inputs);
  337. return ret;
  338. }