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.

510 lines
14KB

  1. /*
  2. * filter graphs
  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 "avfilter.h"
  25. #include "avfiltergraph.h"
  26. /**
  27. * For use in av_log
  28. */
  29. static const char *log_name(void *p)
  30. {
  31. return "Filter parser";
  32. }
  33. static const AVClass filter_parser_class = {
  34. "Filter parser",
  35. log_name
  36. };
  37. static const AVClass *log_ctx = &filter_parser_class;
  38. void avfilter_destroy_graph(AVFilterGraph *graph)
  39. {
  40. for(; graph->filter_count > 0; graph->filter_count --)
  41. avfilter_destroy(graph->filters[graph->filter_count - 1]);
  42. av_freep(&graph->filters);
  43. }
  44. /* TODO: insert in sorted order */
  45. void avfilter_graph_add_filter(AVFilterGraph *graph, AVFilterContext *filter)
  46. {
  47. graph->filters = av_realloc(graph->filters,
  48. sizeof(AVFilterContext*) * ++graph->filter_count);
  49. graph->filters[graph->filter_count - 1] = filter;
  50. }
  51. /* search intelligently, once we insert in order */
  52. AVFilterContext *avfilter_graph_get_filter(AVFilterGraph *graph, char *name)
  53. {
  54. int i;
  55. if(!name)
  56. return NULL;
  57. for(i = 0; i < graph->filter_count; i ++)
  58. if(graph->filters[i]->name && !strcmp(name, graph->filters[i]->name))
  59. return graph->filters[i];
  60. return NULL;
  61. }
  62. static int query_formats(AVFilterGraph *graph)
  63. {
  64. int i, j;
  65. /* ask all the sub-filters for their supported colorspaces */
  66. for(i = 0; i < graph->filter_count; i ++) {
  67. if(graph->filters[i]->filter->query_formats)
  68. graph->filters[i]->filter->query_formats(graph->filters[i]);
  69. else
  70. avfilter_default_query_formats(graph->filters[i]);
  71. }
  72. /* go through and merge as many format lists as possible */
  73. for(i = 0; i < graph->filter_count; i ++) {
  74. AVFilterContext *filter = graph->filters[i];
  75. for(j = 0; j < filter->input_count; j ++) {
  76. AVFilterLink *link;
  77. if(!(link = filter->inputs[j]))
  78. continue;
  79. if(link->in_formats != link->out_formats) {
  80. if(!avfilter_merge_formats(link->in_formats,
  81. link->out_formats)) {
  82. /* couldn't merge format lists. auto-insert scale filter */
  83. AVFilterContext *scale;
  84. if(!(scale =
  85. avfilter_open(avfilter_get_by_name("scale"), NULL)))
  86. return -1;
  87. if(scale->filter->init(scale, NULL, NULL) ||
  88. avfilter_insert_filter(link, scale, 0, 0)) {
  89. avfilter_destroy(scale);
  90. return -1;
  91. }
  92. avfilter_graph_add_filter(graph, scale);
  93. scale->filter->query_formats(scale);
  94. if(!avfilter_merge_formats(scale-> inputs[0]->in_formats,
  95. scale-> inputs[0]->out_formats) ||
  96. !avfilter_merge_formats(scale->outputs[0]->in_formats,
  97. scale->outputs[0]->out_formats))
  98. return -1;
  99. }
  100. }
  101. }
  102. }
  103. return 0;
  104. }
  105. static void pick_format(AVFilterLink *link)
  106. {
  107. if(!link || !link->in_formats)
  108. return;
  109. link->in_formats->format_count = 1;
  110. link->format = link->in_formats->formats[0];
  111. avfilter_formats_unref(&link->in_formats);
  112. avfilter_formats_unref(&link->out_formats);
  113. }
  114. static void pick_formats(AVFilterGraph *graph)
  115. {
  116. int i, j;
  117. for(i = 0; i < graph->filter_count; i ++) {
  118. AVFilterContext *filter = graph->filters[i];
  119. for(j = 0; j < filter->input_count; j ++)
  120. pick_format(filter->inputs[j]);
  121. for(j = 0; j < filter->output_count; j ++)
  122. pick_format(filter->outputs[j]);
  123. }
  124. }
  125. int avfilter_graph_config_formats(AVFilterGraph *graph)
  126. {
  127. /* find supported formats from sub-filters, and merge along links */
  128. if(query_formats(graph))
  129. return -1;
  130. /* Once everything is merged, it's possible that we'll still have
  131. * multiple valid colorspace choices. We pick the first one. */
  132. pick_formats(graph);
  133. return 0;
  134. }
  135. static int create_filter(AVFilterGraph *ctx, int index, char *name,
  136. char *args)
  137. {
  138. AVFilterContext *filt;
  139. AVFilter *filterdef;
  140. char tmp[20];
  141. snprintf(tmp, 20, "%d", index);
  142. if(!(filterdef = avfilter_get_by_name(name)) ||
  143. !(filt = avfilter_open(filterdef, tmp))) {
  144. av_log(&log_ctx, AV_LOG_ERROR,
  145. "error creating filter '%s'\n", name);
  146. return -1;
  147. }
  148. avfilter_graph_add_filter(ctx, filt);
  149. if(avfilter_init_filter(filt, args, NULL)) {
  150. av_log(&log_ctx, AV_LOG_ERROR,
  151. "error initializing filter '%s'\n", name);
  152. return -1;
  153. }
  154. return 0;
  155. }
  156. static int link_filter(AVFilterGraph *ctx, int src, int srcpad,
  157. int dst, int dstpad)
  158. {
  159. AVFilterContext *filt, *filtb;
  160. char tmp[20];
  161. snprintf(tmp, 20, "%d", src);
  162. if(!(filt = avfilter_graph_get_filter(ctx, tmp))) {
  163. av_log(&log_ctx, AV_LOG_ERROR, "link source does not exist in graph\n");
  164. return -1;
  165. }
  166. snprintf(tmp, 20, "%d", dst);
  167. if(!(filtb = avfilter_graph_get_filter(ctx, tmp))) {
  168. av_log(&log_ctx, AV_LOG_ERROR, "link destination does not exist in graph\n");
  169. return -1;
  170. }
  171. if(avfilter_link(filt, srcpad, filtb, dstpad)) {
  172. av_log(&log_ctx, AV_LOG_ERROR, "cannot create link between source and destination filters\n");
  173. return -1;
  174. }
  175. return 0;
  176. }
  177. static void consume_whitespace(const char **buf)
  178. {
  179. *buf += strspn(*buf, " \n\t");
  180. }
  181. /**
  182. * get the next non-whitespace char
  183. */
  184. static char consume_char(const char **buf)
  185. {
  186. char out;
  187. consume_whitespace(buf);
  188. out = **buf;
  189. if (out)
  190. (*buf)++;
  191. return out;
  192. }
  193. /**
  194. * remove the quotation marks from a string. Ex: "aaa'bb'cc" -> "aaabbcc"
  195. */
  196. static void unquote(char *str)
  197. {
  198. char *p1, *p2;
  199. p1=p2=str;
  200. while (*p1 != 0) {
  201. if (*p1 != '\'')
  202. *p2++ = *p1;
  203. p1++;
  204. }
  205. *p2 = 0;
  206. }
  207. /**
  208. * Consumes a string from *buf.
  209. * @return a copy of the consumed string, which should be free'd after use
  210. */
  211. static char *consume_string(const char **buf)
  212. {
  213. const char *start;
  214. char *ret;
  215. int size;
  216. consume_whitespace(buf);
  217. if (!(**buf))
  218. return av_mallocz(1);
  219. start = *buf;
  220. *buf += strcspn(*buf, " ()=,'");
  221. if (**buf == '\'') {
  222. char *p = strchr(*buf + 1, '\'');
  223. if (p)
  224. *buf = p + 1;
  225. else
  226. *buf += strlen(*buf); // Move the pointer to the null end byte
  227. }
  228. size = *buf - start + 1;
  229. ret = av_malloc(size);
  230. memcpy(ret, start, size - 1);
  231. ret[size-1] = 0;
  232. unquote(ret);
  233. return ret;
  234. }
  235. /**
  236. * Parse "(linkname)"
  237. * @arg name a pointer (that need to be free'd after use) to the name between
  238. * parenthesis
  239. */
  240. static void parse_link_name(const char **buf, char **name)
  241. {
  242. consume_char(buf);
  243. *name = consume_string(buf);
  244. if (!*name[0])
  245. goto fail;
  246. if (consume_char(buf) != ')')
  247. goto fail;
  248. return;
  249. fail:
  250. av_freep(name);
  251. av_log(&log_ctx, AV_LOG_ERROR, "Could not parse link name!\n");
  252. }
  253. /**
  254. * Parse "filter=params"
  255. * @arg name a pointer (that need to be free'd after use) to the name of the
  256. * filter
  257. * @arg ars a pointer (that need to be free'd after use) to the args of the
  258. * filter
  259. */
  260. static int parse_filter(const char **buf, AVFilterGraph *graph, int index)
  261. {
  262. char *name, *opts;
  263. name = consume_string(buf);
  264. if (**buf == '=') {
  265. consume_char(buf);
  266. opts = consume_string(buf);
  267. } else {
  268. opts = NULL;
  269. }
  270. return create_filter(graph, index, name, opts);
  271. }
  272. enum LinkType {
  273. LinkTypeIn,
  274. LinkTypeOut,
  275. };
  276. /**
  277. * A linked-list of the inputs/outputs of the filter chain.
  278. */
  279. typedef struct AVFilterInOut {
  280. enum LinkType type;
  281. char *name;
  282. int instance;
  283. int pad_idx;
  284. struct AVFilterInOut *next;
  285. } AVFilterInOut;
  286. static void free_inout(AVFilterInOut *head)
  287. {
  288. while (head) {
  289. AVFilterInOut *next;
  290. next = head->next;
  291. av_free(head);
  292. head = next;
  293. }
  294. }
  295. /**
  296. * Parse "(a1)(link2) ... (etc)"
  297. */
  298. static int parse_inouts(const char **buf, AVFilterInOut **inout, int firstpad,
  299. enum LinkType type, int instance)
  300. {
  301. int pad = firstpad;
  302. while (**buf == '(') {
  303. AVFilterInOut *inoutn = av_malloc(sizeof(AVFilterInOut));
  304. parse_link_name(buf, &inoutn->name);
  305. inoutn->type = type;
  306. inoutn->instance = instance;
  307. inoutn->pad_idx = pad++;
  308. inoutn->next = *inout;
  309. *inout = inoutn;
  310. }
  311. return pad;
  312. }
  313. /**
  314. * Parse a string describing a filter graph.
  315. */
  316. int avfilter_graph_parse_chain(AVFilterGraph *graph, const char *filters, AVFilterContext *in, int inpad, AVFilterContext *out, int outpad)
  317. {
  318. AVFilterInOut *inout=NULL;
  319. AVFilterInOut *head=NULL;
  320. int index = 0;
  321. char chr = 0;
  322. int pad = 0;
  323. int has_out = 0;
  324. char tmp[20];
  325. AVFilterContext *filt;
  326. consume_whitespace(&filters);
  327. do {
  328. int oldpad = pad;
  329. pad = parse_inouts(&filters, &inout, chr == ',', LinkTypeIn, index);
  330. if (parse_filter(&filters, graph, index) < 0)
  331. goto fail;
  332. // If the first filter has an input and none was given, it is
  333. // implicitly the input of the whole graph.
  334. if (pad == 0 && graph->filters[graph->filter_count-1]->input_count == 1) {
  335. snprintf(tmp, 20, "%d", index);
  336. if(!(filt = avfilter_graph_get_filter(graph, tmp))) {
  337. av_log(&log_ctx, AV_LOG_ERROR, "filter owning exported pad does not exist\n");
  338. goto fail;
  339. }
  340. if(avfilter_link(in, inpad, filt, 0)) {
  341. av_log(&log_ctx, AV_LOG_ERROR, "cannot create link between source and destination filters\n");
  342. goto fail;
  343. }
  344. }
  345. if(chr == ',') {
  346. if (link_filter(graph, index-1, oldpad, index, 0) < 0)
  347. goto fail;
  348. }
  349. pad = parse_inouts(&filters, &inout, 0, LinkTypeOut, index);
  350. chr = consume_char(&filters);
  351. index++;
  352. } while (chr == ',' || chr == ';');
  353. head = inout;
  354. for (; inout != NULL; inout = inout->next) {
  355. if (inout->instance == -1)
  356. continue; // Already processed
  357. if (!strcmp(inout->name, "in")) {
  358. snprintf(tmp, 20, "%d", inout->instance);
  359. if(!(filt = avfilter_graph_get_filter(graph, tmp))) {
  360. av_log(&log_ctx, AV_LOG_ERROR, "filter owning exported pad does not exist\n");
  361. goto fail;
  362. }
  363. if(avfilter_link(in, inpad, filt, inout->pad_idx)) {
  364. av_log(&log_ctx, AV_LOG_ERROR, "cannot create link between source and destination filters\n");
  365. goto fail;
  366. }
  367. } else if (!strcmp(inout->name, "out")) {
  368. has_out = 1;
  369. snprintf(tmp, 20, "%d", inout->instance);
  370. if(!(filt = avfilter_graph_get_filter(graph, tmp))) {
  371. av_log(&log_ctx, AV_LOG_ERROR, "filter owning exported pad does not exist\n");
  372. goto fail;
  373. }
  374. if(avfilter_link(filt, inout->pad_idx, out, outpad)) {
  375. av_log(&log_ctx, AV_LOG_ERROR, "cannot create link between source and destination filters\n");
  376. goto fail;
  377. }
  378. } else {
  379. AVFilterInOut *p, *src, *dst;
  380. for (p = inout->next;
  381. p && strcmp(p->name,inout->name); p = p->next);
  382. if (!p) {
  383. av_log(&log_ctx, AV_LOG_ERROR, "Unmatched link: %s.\n",
  384. inout->name);
  385. goto fail;
  386. }
  387. if (p->type == LinkTypeIn && inout->type == LinkTypeOut) {
  388. src = inout;
  389. dst = p;
  390. } else if (p->type == LinkTypeOut && inout->type == LinkTypeIn) {
  391. src = p;
  392. dst = inout;
  393. } else {
  394. av_log(&log_ctx, AV_LOG_ERROR, "Two links named '%s' are either both input or both output\n",
  395. inout->name);
  396. goto fail;
  397. }
  398. if (link_filter(graph, src->instance, src->pad_idx, dst->instance, dst->pad_idx) < 0)
  399. goto fail;
  400. src->instance = -1;
  401. dst->instance = -1;
  402. }
  403. }
  404. free_inout(head);
  405. if (!has_out) {
  406. snprintf(tmp, 20, "%d", index-1);
  407. if(!(filt = avfilter_graph_get_filter(graph, tmp))) {
  408. av_log(&log_ctx, AV_LOG_ERROR, "filter owning exported pad does not exist\n");
  409. goto fail;
  410. }
  411. if(avfilter_link(filt, pad, out, outpad)) {
  412. av_log(&log_ctx, AV_LOG_ERROR, "cannot create link between source and destination filters\n");
  413. goto fail;
  414. }
  415. }
  416. return 0;
  417. fail:
  418. free_inout(head);
  419. avfilter_destroy_graph(graph);
  420. return -1;
  421. }