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.

367 lines
9.7KB

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