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.

365 lines
9.6KB

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