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.

396 lines
11KB

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