graphparser.c 10 KB

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