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.

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