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.

523 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. * Copy the first size bytes of input string to a null-terminated string,
  195. * removing any control character. Ex: "aaa'bb'c\'c\\" -> "aaabbc'c\"
  196. */
  197. static void copy_unquoted(char *out, const char *in, int size)
  198. {
  199. int i;
  200. for (i=0; i < size; i++) {
  201. if (in[i] == '\'')
  202. continue;
  203. else if (in[i] == '\\') {
  204. if (i+1 == size) {
  205. *out = 0;
  206. return;
  207. }
  208. i++;
  209. }
  210. *out++ = in[i];
  211. }
  212. *out=0;
  213. }
  214. /**
  215. * Consumes a string from *buf.
  216. * @return a copy of the consumed string, which should be free'd after use
  217. */
  218. static char *consume_string(const char **buf)
  219. {
  220. const char *start;
  221. char *ret;
  222. int size;
  223. consume_whitespace(buf);
  224. if (!(**buf))
  225. return av_mallocz(1);
  226. start = *buf;
  227. while(1) {
  228. *buf += strcspn(*buf, " ()=,'\\");
  229. if (**buf == '\\')
  230. *buf+=2;
  231. else
  232. break;
  233. }
  234. if (**buf == '\'') {
  235. const char *p = *buf;
  236. do {
  237. p++;
  238. p = strchr(p, '\'');
  239. } while (p && p[-1] == '\\');
  240. if (p)
  241. *buf = p + 1;
  242. else
  243. *buf += strlen(*buf); // Move the pointer to the null end byte
  244. }
  245. size = *buf - start + 1;
  246. ret = av_malloc(size);
  247. copy_unquoted(ret, start, size-1);
  248. return ret;
  249. }
  250. /**
  251. * Parse "(linkname)"
  252. * @arg name a pointer (that need to be free'd after use) to the name between
  253. * parenthesis
  254. */
  255. static void parse_link_name(const char **buf, char **name)
  256. {
  257. consume_char(buf);
  258. *name = consume_string(buf);
  259. if (!*name[0])
  260. goto fail;
  261. if (consume_char(buf) != ')')
  262. goto fail;
  263. return;
  264. fail:
  265. av_freep(name);
  266. av_log(&log_ctx, AV_LOG_ERROR, "Could not parse link name!\n");
  267. }
  268. /**
  269. * Parse "filter=params"
  270. * @arg name a pointer (that need to be free'd after use) to the name of the
  271. * filter
  272. * @arg ars a pointer (that need to be free'd after use) to the args of the
  273. * filter
  274. */
  275. static int parse_filter(const char **buf, AVFilterGraph *graph, int index)
  276. {
  277. char *name, *opts;
  278. name = consume_string(buf);
  279. if (**buf == '=') {
  280. consume_char(buf);
  281. opts = consume_string(buf);
  282. } else {
  283. opts = NULL;
  284. }
  285. return create_filter(graph, index, name, opts);
  286. }
  287. enum LinkType {
  288. LinkTypeIn,
  289. LinkTypeOut,
  290. };
  291. /**
  292. * A linked-list of the inputs/outputs of the filter chain.
  293. */
  294. typedef struct AVFilterInOut {
  295. enum LinkType type;
  296. char *name;
  297. int instance;
  298. int pad_idx;
  299. struct AVFilterInOut *next;
  300. } AVFilterInOut;
  301. static void free_inout(AVFilterInOut *head)
  302. {
  303. while (head) {
  304. AVFilterInOut *next;
  305. next = head->next;
  306. av_free(head);
  307. head = next;
  308. }
  309. }
  310. /**
  311. * Parse "(a1)(link2) ... (etc)"
  312. */
  313. static int parse_inouts(const char **buf, AVFilterInOut **inout, int firstpad,
  314. enum LinkType type, int instance)
  315. {
  316. int pad = firstpad;
  317. while (**buf == '(') {
  318. AVFilterInOut *inoutn = av_malloc(sizeof(AVFilterInOut));
  319. parse_link_name(buf, &inoutn->name);
  320. inoutn->type = type;
  321. inoutn->instance = instance;
  322. inoutn->pad_idx = pad++;
  323. inoutn->next = *inout;
  324. *inout = inoutn;
  325. }
  326. return pad;
  327. }
  328. /**
  329. * Parse a string describing a filter graph.
  330. */
  331. int avfilter_graph_parse_chain(AVFilterGraph *graph, const char *filters, AVFilterContext *in, int inpad, AVFilterContext *out, int outpad)
  332. {
  333. AVFilterInOut *inout=NULL;
  334. AVFilterInOut *head=NULL;
  335. int index = 0;
  336. char chr = 0;
  337. int pad = 0;
  338. int has_out = 0;
  339. char tmp[20];
  340. AVFilterContext *filt;
  341. consume_whitespace(&filters);
  342. do {
  343. int oldpad = pad;
  344. pad = parse_inouts(&filters, &inout, chr == ',', LinkTypeIn, index);
  345. if (parse_filter(&filters, graph, index) < 0)
  346. goto fail;
  347. // If the first filter has an input and none was given, it is
  348. // implicitly the input of the whole graph.
  349. if (pad == 0 && graph->filters[graph->filter_count-1]->input_count == 1) {
  350. snprintf(tmp, 20, "%d", index);
  351. if(!(filt = avfilter_graph_get_filter(graph, tmp))) {
  352. av_log(&log_ctx, AV_LOG_ERROR, "filter owning exported pad does not exist\n");
  353. goto fail;
  354. }
  355. if(avfilter_link(in, inpad, filt, 0)) {
  356. av_log(&log_ctx, AV_LOG_ERROR, "cannot create link between source and destination filters\n");
  357. goto fail;
  358. }
  359. }
  360. if(chr == ',') {
  361. if (link_filter(graph, index-1, oldpad, index, 0) < 0)
  362. goto fail;
  363. }
  364. pad = parse_inouts(&filters, &inout, 0, LinkTypeOut, index);
  365. chr = consume_char(&filters);
  366. index++;
  367. } while (chr == ',' || chr == ';');
  368. head = inout;
  369. for (; inout != NULL; inout = inout->next) {
  370. if (inout->instance == -1)
  371. continue; // Already processed
  372. if (!strcmp(inout->name, "in")) {
  373. snprintf(tmp, 20, "%d", inout->instance);
  374. if(!(filt = avfilter_graph_get_filter(graph, tmp))) {
  375. av_log(&log_ctx, AV_LOG_ERROR, "filter owning exported pad does not exist\n");
  376. goto fail;
  377. }
  378. if(avfilter_link(in, inpad, filt, inout->pad_idx)) {
  379. av_log(&log_ctx, AV_LOG_ERROR, "cannot create link between source and destination filters\n");
  380. goto fail;
  381. }
  382. } else if (!strcmp(inout->name, "out")) {
  383. has_out = 1;
  384. snprintf(tmp, 20, "%d", inout->instance);
  385. if(!(filt = avfilter_graph_get_filter(graph, tmp))) {
  386. av_log(&log_ctx, AV_LOG_ERROR, "filter owning exported pad does not exist\n");
  387. goto fail;
  388. }
  389. if(avfilter_link(filt, inout->pad_idx, out, outpad)) {
  390. av_log(&log_ctx, AV_LOG_ERROR, "cannot create link between source and destination filters\n");
  391. goto fail;
  392. }
  393. } else {
  394. AVFilterInOut *p, *src, *dst;
  395. for (p = inout->next;
  396. p && strcmp(p->name,inout->name); p = p->next);
  397. if (!p) {
  398. av_log(&log_ctx, AV_LOG_ERROR, "Unmatched link: %s.\n",
  399. inout->name);
  400. goto fail;
  401. }
  402. if (p->type == LinkTypeIn && inout->type == LinkTypeOut) {
  403. src = inout;
  404. dst = p;
  405. } else if (p->type == LinkTypeOut && inout->type == LinkTypeIn) {
  406. src = p;
  407. dst = inout;
  408. } else {
  409. av_log(&log_ctx, AV_LOG_ERROR, "Two links named '%s' are either both input or both output\n",
  410. inout->name);
  411. goto fail;
  412. }
  413. if (link_filter(graph, src->instance, src->pad_idx, dst->instance, dst->pad_idx) < 0)
  414. goto fail;
  415. src->instance = -1;
  416. dst->instance = -1;
  417. }
  418. }
  419. free_inout(head);
  420. if (!has_out) {
  421. snprintf(tmp, 20, "%d", index-1);
  422. if(!(filt = avfilter_graph_get_filter(graph, tmp))) {
  423. av_log(&log_ctx, AV_LOG_ERROR, "filter owning exported pad does not exist\n");
  424. goto fail;
  425. }
  426. if(avfilter_link(filt, pad, out, outpad)) {
  427. av_log(&log_ctx, AV_LOG_ERROR, "cannot create link between source and destination filters\n");
  428. goto fail;
  429. }
  430. }
  431. return 0;
  432. fail:
  433. free_inout(head);
  434. avfilter_destroy_graph(graph);
  435. return -1;
  436. }