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.

384 lines
10KB

  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 "graphparser.h"
  26. #include "avfilter.h"
  27. #include "avfiltergraph.h"
  28. #include "parseutils.h"
  29. #define WHITESPACES " \n\t"
  30. /**
  31. * Link two filters together.
  32. *
  33. * @see avfilter_link()
  34. */
  35. static int link_filter(AVFilterContext *src, int srcpad,
  36. AVFilterContext *dst, int dstpad,
  37. AVClass *log_ctx)
  38. {
  39. int ret;
  40. if ((ret = avfilter_link(src, srcpad, dst, dstpad))) {
  41. av_log(log_ctx, AV_LOG_ERROR,
  42. "Cannot create the link %s:%d -> %s:%d\n",
  43. src->filter->name, srcpad, dst->filter->name, dstpad);
  44. return ret;
  45. }
  46. return 0;
  47. }
  48. /**
  49. * Parse the name of a link, which has the format "[linkname]".
  50. *
  51. * @return a pointer (that need to be freed after use) to the name
  52. * between parenthesis
  53. */
  54. static char *parse_link_name(const char **buf, AVClass *log_ctx)
  55. {
  56. const char *start = *buf;
  57. char *name;
  58. (*buf)++;
  59. name = av_get_token(buf, "]");
  60. if (!name[0]) {
  61. av_log(log_ctx, AV_LOG_ERROR,
  62. "Bad (empty?) label found in the following: \"%s\".\n", start);
  63. goto fail;
  64. }
  65. if (*(*buf)++ != ']') {
  66. av_log(log_ctx, AV_LOG_ERROR,
  67. "Mismatched '[' found in the following: \"%s\".\n", start);
  68. fail:
  69. av_freep(&name);
  70. }
  71. return name;
  72. }
  73. /**
  74. * Create an instance of a filter, initialize and insert it in the
  75. * filtergraph in *ctx.
  76. *
  77. * @param ctx the filtergraph context
  78. * @param put here a filter context in case of successful creation and configuration, NULL otherwise.
  79. * @param index an index which is supposed to be unique for each filter instance added to the filtergraph
  80. * @param filt_name the name of the filter to create
  81. * @param args the arguments provided to the filter during its initialization
  82. * @param log_ctx the log context to use
  83. * @return 0 in case of success, a negative AVERROR code otherwise
  84. */
  85. static int create_filter(AVFilterContext **filt_ctx, AVFilterGraph *ctx, int index,
  86. const char *filt_name, const char *args, AVClass *log_ctx)
  87. {
  88. AVFilter *filt;
  89. char inst_name[30];
  90. char tmp_args[256];
  91. int ret;
  92. snprintf(inst_name, sizeof(inst_name), "Filter %d %s", index, filt_name);
  93. filt = avfilter_get_by_name(filt_name);
  94. if (!filt) {
  95. av_log(log_ctx, AV_LOG_ERROR,
  96. "No such filter: '%s'\n", filt_name);
  97. return AVERROR(EINVAL);
  98. }
  99. ret = avfilter_open(filt_ctx, filt, inst_name);
  100. if (!*filt_ctx) {
  101. av_log(log_ctx, AV_LOG_ERROR,
  102. "Error creating filter '%s'\n", filt_name);
  103. return ret;
  104. }
  105. if ((ret = avfilter_graph_add_filter(ctx, *filt_ctx)) < 0) {
  106. avfilter_destroy(*filt_ctx);
  107. return ret;
  108. }
  109. if (!strcmp(filt_name, "scale") && !strstr(args, "flags")) {
  110. snprintf(tmp_args, sizeof(tmp_args), "%s:%s",
  111. args, ctx->scale_sws_opts);
  112. args = tmp_args;
  113. }
  114. if ((ret = avfilter_init_filter(*filt_ctx, args, NULL)) < 0) {
  115. av_log(log_ctx, AV_LOG_ERROR,
  116. "Error initializing filter '%s' with args '%s'\n", filt_name, args);
  117. return ret;
  118. }
  119. return 0;
  120. }
  121. /**
  122. * Parse "filter=params"
  123. */
  124. static AVFilterContext *parse_filter(const char **buf, AVFilterGraph *graph,
  125. int index, AVClass *log_ctx)
  126. {
  127. char *opts = NULL;
  128. char *name = av_get_token(buf, "=,;[\n");
  129. AVFilterContext *filt_ctx;
  130. int ret;
  131. if (**buf == '=') {
  132. (*buf)++;
  133. opts = av_get_token(buf, "[],;\n");
  134. }
  135. ret = create_filter(&filt_ctx, graph, index, name, opts, log_ctx);
  136. av_free(name);
  137. av_free(opts);
  138. return filt_ctx;
  139. }
  140. static void free_inout(AVFilterInOut *head)
  141. {
  142. while (head) {
  143. AVFilterInOut *next = head->next;
  144. av_free(head->name);
  145. av_free(head);
  146. head = next;
  147. }
  148. }
  149. static AVFilterInOut *extract_inout(const char *label, AVFilterInOut **links)
  150. {
  151. AVFilterInOut *ret;
  152. while (*links && strcmp((*links)->name, label))
  153. links = &((*links)->next);
  154. ret = *links;
  155. if (ret)
  156. *links = ret->next;
  157. return ret;
  158. }
  159. static void insert_inout(AVFilterInOut **inouts, AVFilterInOut *element)
  160. {
  161. element->next = *inouts;
  162. *inouts = element;
  163. }
  164. static int link_filter_inouts(AVFilterContext *filter,
  165. AVFilterInOut **curr_inputs,
  166. AVFilterInOut **open_inputs, AVClass *log_ctx)
  167. {
  168. int pad = filter->input_count;
  169. while (pad--) {
  170. AVFilterInOut *p = *curr_inputs;
  171. if (!p) {
  172. av_log(log_ctx, AV_LOG_ERROR,
  173. "Not enough inputs specified for the \"%s\" filter.\n",
  174. filter->filter->name);
  175. return -1;
  176. }
  177. *curr_inputs = (*curr_inputs)->next;
  178. if (p->filter) {
  179. if (link_filter(p->filter, p->pad_idx, filter, pad, log_ctx))
  180. return -1;
  181. av_free(p->name);
  182. av_free(p);
  183. } else {
  184. p->filter = filter;
  185. p->pad_idx = pad;
  186. insert_inout(open_inputs, p);
  187. }
  188. }
  189. if (*curr_inputs) {
  190. av_log(log_ctx, AV_LOG_ERROR,
  191. "Too many inputs specified for the \"%s\" filter.\n",
  192. filter->filter->name);
  193. return -1;
  194. }
  195. pad = filter->output_count;
  196. while (pad--) {
  197. AVFilterInOut *currlinkn = av_mallocz(sizeof(AVFilterInOut));
  198. currlinkn->filter = filter;
  199. currlinkn->pad_idx = pad;
  200. insert_inout(curr_inputs, currlinkn);
  201. }
  202. return 0;
  203. }
  204. static int parse_inputs(const char **buf, AVFilterInOut **curr_inputs,
  205. AVFilterInOut **open_outputs, AVClass *log_ctx)
  206. {
  207. int pad = 0;
  208. while (**buf == '[') {
  209. char *name = parse_link_name(buf, log_ctx);
  210. AVFilterInOut *match;
  211. if (!name)
  212. return -1;
  213. /* First check if the label is not in the open_outputs list */
  214. match = extract_inout(name, open_outputs);
  215. if (match) {
  216. av_free(name);
  217. } else {
  218. /* Not in the list, so add it as an input */
  219. match = av_mallocz(sizeof(AVFilterInOut));
  220. match->name = name;
  221. match->pad_idx = pad;
  222. }
  223. insert_inout(curr_inputs, match);
  224. *buf += strspn(*buf, WHITESPACES);
  225. pad++;
  226. }
  227. return pad;
  228. }
  229. static int parse_outputs(const char **buf, AVFilterInOut **curr_inputs,
  230. AVFilterInOut **open_inputs,
  231. AVFilterInOut **open_outputs, AVClass *log_ctx)
  232. {
  233. int pad = 0;
  234. while (**buf == '[') {
  235. char *name = parse_link_name(buf, log_ctx);
  236. AVFilterInOut *match;
  237. AVFilterInOut *input = *curr_inputs;
  238. *curr_inputs = (*curr_inputs)->next;
  239. if (!name)
  240. return -1;
  241. /* First check if the label is not in the open_inputs list */
  242. match = extract_inout(name, open_inputs);
  243. if (match) {
  244. if (link_filter(input->filter, input->pad_idx,
  245. match->filter, match->pad_idx, log_ctx) < 0)
  246. return -1;
  247. av_free(match->name);
  248. av_free(name);
  249. av_free(match);
  250. av_free(input);
  251. } else {
  252. /* Not in the list, so add the first input as a open_output */
  253. input->name = name;
  254. insert_inout(open_outputs, input);
  255. }
  256. *buf += strspn(*buf, WHITESPACES);
  257. pad++;
  258. }
  259. return pad;
  260. }
  261. int avfilter_graph_parse(AVFilterGraph *graph, const char *filters,
  262. AVFilterInOut *open_inputs,
  263. AVFilterInOut *open_outputs, AVClass *log_ctx)
  264. {
  265. int index = 0;
  266. char chr = 0;
  267. AVFilterInOut *curr_inputs = NULL;
  268. do {
  269. AVFilterContext *filter;
  270. filters += strspn(filters, WHITESPACES);
  271. if (parse_inputs(&filters, &curr_inputs, &open_outputs, log_ctx) < 0)
  272. goto fail;
  273. filter = parse_filter(&filters, graph, index, log_ctx);
  274. if (!filter)
  275. goto fail;
  276. if (filter->input_count == 1 && !curr_inputs && !index) {
  277. /* First input can be omitted if it is "[in]" */
  278. const char *tmp = "[in]";
  279. if(parse_inputs(&tmp, &curr_inputs, &open_outputs, log_ctx) < 0)
  280. goto fail;
  281. }
  282. if (link_filter_inouts(filter, &curr_inputs, &open_inputs, log_ctx) < 0)
  283. goto fail;
  284. if (parse_outputs(&filters, &curr_inputs, &open_inputs, &open_outputs,
  285. log_ctx) < 0)
  286. goto fail;
  287. filters += strspn(filters, WHITESPACES);
  288. chr = *filters++;
  289. if (chr == ';' && curr_inputs) {
  290. av_log(log_ctx, AV_LOG_ERROR,
  291. "Could not find a output to link when parsing \"%s\"\n",
  292. filters - 1);
  293. goto fail;
  294. }
  295. index++;
  296. } while (chr == ',' || chr == ';');
  297. if (chr) {
  298. av_log(log_ctx, AV_LOG_ERROR,
  299. "Unable to parse graph description substring: \"%s\"\n",
  300. filters - 1);
  301. goto fail;
  302. }
  303. if (open_inputs && !strcmp(open_inputs->name, "out") && curr_inputs) {
  304. /* Last output can be omitted if it is "[out]" */
  305. const char *tmp = "[out]";
  306. if (parse_outputs(&tmp, &curr_inputs, &open_inputs,
  307. &open_outputs, log_ctx) < 0)
  308. goto fail;
  309. }
  310. return 0;
  311. fail:
  312. avfilter_graph_free(graph);
  313. free_inout(open_inputs);
  314. free_inout(open_outputs);
  315. free_inout(curr_inputs);
  316. return -1;
  317. }