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.

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