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.

372 lines
9.8KB

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