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.

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