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.

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