graphparser.c 10 KB

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