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.

590 lines
16KB

  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. static void uninit(GraphContext *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(GraphContext *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(GraphContext *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(GraphContext *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(GraphContext *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(GraphContext *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 graph_load_from_desc2(GraphContext *ctx, AVFilterGraphDesc *desc)
  136. {
  137. AVFilterGraphDescFilter *curfilt;
  138. AVFilterGraphDescLink *curlink;
  139. AVFilterContext *filt, *filtb;
  140. AVFilter *filterdef;
  141. char tmp[20];
  142. /* create all filters */
  143. for(curfilt = desc->filters; curfilt; curfilt = curfilt->next) {
  144. snprintf(tmp, 20, "%d", curfilt->index);
  145. if(!(filterdef = avfilter_get_by_name(curfilt->filter)) ||
  146. !(filt = avfilter_open(filterdef, tmp))) {
  147. av_log(&log_ctx, AV_LOG_ERROR,
  148. "error creating filter '%s'\n", curfilt->filter);
  149. goto fail;
  150. }
  151. avfilter_graph_add_filter(ctx, filt);
  152. if(avfilter_init_filter(filt, curfilt->args, NULL)) {
  153. av_log(&log_ctx, AV_LOG_ERROR,
  154. "error initializing filter '%s'\n", curfilt->filter);
  155. goto fail;
  156. }
  157. }
  158. /* create all links */
  159. for(curlink = desc->links; curlink; curlink = curlink->next) {
  160. snprintf(tmp, 20, "%d", curlink->src);
  161. if(!(filt = avfilter_graph_get_filter(ctx, tmp))) {
  162. av_log(&log_ctx, AV_LOG_ERROR, "link source does not exist in graph\n");
  163. goto fail;
  164. }
  165. snprintf(tmp, 20, "%d", curlink->dst);
  166. if(!(filtb = avfilter_graph_get_filter(ctx, tmp))) {
  167. av_log(&log_ctx, AV_LOG_ERROR, "link destination does not exist in graph\n");
  168. goto fail;
  169. }
  170. if(avfilter_link(filt, curlink->srcpad, filtb, curlink->dstpad)) {
  171. av_log(&log_ctx, AV_LOG_ERROR, "cannot create link between source and destination filters\n");
  172. goto fail;
  173. }
  174. }
  175. return 0;
  176. fail:
  177. uninit(ctx);
  178. return -1;
  179. }
  180. int graph_load_from_desc3(GraphContext *graph, AVFilterGraphDesc *desc, AVFilterContext *in, int inpad, AVFilterContext *out, int outpad)
  181. {
  182. AVFilterGraphDescExport *curpad;
  183. char tmp[20];
  184. AVFilterContext *filt;
  185. if (graph_load_from_desc2(graph, desc) < 0)
  186. goto fail;
  187. /* export all input pads */
  188. for(curpad = desc->inputs; curpad; curpad = curpad->next) {
  189. snprintf(tmp, 20, "%d", curpad->filter);
  190. if(!(filt = avfilter_graph_get_filter(graph, tmp))) {
  191. av_log(&log_ctx, AV_LOG_ERROR, "filter owning exported pad does not exist\n");
  192. goto fail;
  193. }
  194. if(avfilter_link(in, inpad, filt, curpad->pad)) {
  195. av_log(&log_ctx, AV_LOG_ERROR, "cannot create link between source and destination filters\n");
  196. goto fail;
  197. }
  198. }
  199. /* export all output pads */
  200. for(curpad = desc->outputs; curpad; curpad = curpad->next) {
  201. snprintf(tmp, 20, "%d", curpad->filter);
  202. if(!(filt = avfilter_graph_get_filter(graph, tmp))) {
  203. av_log(&log_ctx, AV_LOG_ERROR, "filter owning exported pad does not exist\n");
  204. goto fail;
  205. }
  206. if(avfilter_link(filt, curpad->pad, out, outpad)) {
  207. av_log(&log_ctx, AV_LOG_ERROR, "cannot create link between source and destination filters\n");
  208. goto fail;
  209. }
  210. }
  211. return 0;
  212. fail:
  213. uninit(graph);
  214. return -1;
  215. }
  216. static void consume_whitespace(const char **buf)
  217. {
  218. *buf += strspn(*buf, " \n\t");
  219. }
  220. /**
  221. * get the next non-whitespace char
  222. */
  223. static char consume_char(const char **buf)
  224. {
  225. char out;
  226. consume_whitespace(buf);
  227. out = **buf;
  228. if (out)
  229. (*buf)++;
  230. return out;
  231. }
  232. /**
  233. * remove the quotation marks from a string. Ex: "aaa'bb'cc" -> "aaabbcc"
  234. */
  235. static void unquote(char *str)
  236. {
  237. char *p1, *p2;
  238. p1=p2=str;
  239. while (*p1 != 0) {
  240. if (*p1 != '\'')
  241. *p2++ = *p1;
  242. p1++;
  243. }
  244. *p2 = 0;
  245. }
  246. /**
  247. * Consumes a string from *buf.
  248. * @return a copy of the consumed string, which should be free'd after use
  249. */
  250. static char *consume_string(const char **buf)
  251. {
  252. const char *start;
  253. char *ret;
  254. int size;
  255. consume_whitespace(buf);
  256. if (!(**buf))
  257. return av_mallocz(1);
  258. start = *buf;
  259. *buf += strcspn(*buf, " ()=,'");
  260. if (**buf == '\'') {
  261. char *p = strchr(*buf + 1, '\'');
  262. if (p)
  263. *buf = p + 1;
  264. else
  265. *buf += strlen(*buf); // Move the pointer to the null end byte
  266. }
  267. size = *buf - start + 1;
  268. ret = av_malloc(size);
  269. memcpy(ret, start, size - 1);
  270. ret[size-1] = 0;
  271. unquote(ret);
  272. return ret;
  273. }
  274. /**
  275. * Parse "(linkname)"
  276. * @arg name a pointer (that need to be free'd after use) to the name between
  277. * parenthesis
  278. */
  279. static void parse_link_name(const char **buf, char **name)
  280. {
  281. consume_char(buf);
  282. *name = consume_string(buf);
  283. if (!*name[0])
  284. goto fail;
  285. if (consume_char(buf) != ')')
  286. goto fail;
  287. return;
  288. fail:
  289. av_freep(name);
  290. av_log(&log_ctx, AV_LOG_ERROR, "Could not parse link name!\n");
  291. }
  292. /**
  293. * Parse "filter=params"
  294. * @arg name a pointer (that need to be free'd after use) to the name of the
  295. * filter
  296. * @arg ars a pointer (that need to be free'd after use) to the args of the
  297. * filter
  298. */
  299. static void parse_filter(const char **buf, char **name, char **opts)
  300. {
  301. *name = consume_string(buf);
  302. if (**buf == '=') {
  303. consume_char(buf);
  304. *opts = consume_string(buf);
  305. } else {
  306. *opts = NULL;
  307. }
  308. }
  309. enum LinkType {
  310. LinkTypeIn,
  311. LinkTypeOut,
  312. };
  313. /**
  314. * A linked-list of the inputs/outputs of the filter chain.
  315. */
  316. typedef struct AVFilterInOut {
  317. enum LinkType type;
  318. char *name;
  319. int instance;
  320. int pad_idx;
  321. struct AVFilterInOut *next;
  322. } AVFilterInOut;
  323. static void free_inout(AVFilterInOut *head)
  324. {
  325. while (head) {
  326. AVFilterInOut *next;
  327. next = head->next;
  328. av_free(head);
  329. head = next;
  330. }
  331. }
  332. /**
  333. * Parse "(a1)(link2) ... (etc)"
  334. */
  335. static int parse_inouts(const char **buf, AVFilterInOut **inout, int firstpad,
  336. enum LinkType type, int instance)
  337. {
  338. int pad = firstpad;
  339. while (**buf == '(') {
  340. AVFilterInOut *inoutn = av_malloc(sizeof(AVFilterInOut));
  341. parse_link_name(buf, &inoutn->name);
  342. inoutn->type = type;
  343. inoutn->instance = instance;
  344. inoutn->pad_idx = pad++;
  345. inoutn->next = *inout;
  346. *inout = inoutn;
  347. }
  348. return pad;
  349. }
  350. static AVFilterGraphDesc *parse_chain(const char *filters, int has_in)
  351. {
  352. AVFilterGraphDesc *ret;
  353. AVFilterGraphDescFilter **filterp, *filtern;
  354. AVFilterGraphDescLink **linkp, *linkn;
  355. AVFilterInOut *inout=NULL;
  356. AVFilterInOut *head;
  357. int index = 0;
  358. char chr = 0;
  359. int pad = 0;
  360. int has_out = 0;
  361. consume_whitespace(&filters);
  362. if(!(ret = av_mallocz(sizeof(AVFilterGraphDesc))))
  363. return NULL;
  364. filterp = &ret->filters;
  365. linkp = &ret->links;
  366. do {
  367. if(chr == ',') {
  368. linkn = av_mallocz(sizeof(AVFilterGraphDescLink));
  369. linkn->src = index-1;
  370. linkn->srcpad = pad;
  371. linkn->dst = index;
  372. linkn->dstpad = 0;
  373. *linkp = linkn;
  374. linkp = &linkn->next;
  375. }
  376. pad = parse_inouts(&filters, &inout, chr == ',' || (!has_in),
  377. LinkTypeIn, index);
  378. filtern = av_mallocz(sizeof(AVFilterGraphDescFilter));
  379. filtern->index = index;
  380. parse_filter(&filters, &filtern->filter, &filtern->args);
  381. *filterp = filtern;
  382. filterp = &filtern->next;
  383. pad = parse_inouts(&filters, &inout, 0,
  384. LinkTypeOut, index);
  385. chr = consume_char(&filters);
  386. index++;
  387. } while (chr == ',' || chr == ';');
  388. head = inout;
  389. for (; inout != NULL; inout = inout->next) {
  390. if (inout->instance == -1)
  391. continue; // Already processed
  392. if (!strcmp(inout->name, "in")) {
  393. if (!has_in)
  394. goto fail;
  395. ret->inputs = av_mallocz(sizeof(AVFilterGraphDescExport));
  396. ret->inputs->filter = inout->instance;
  397. ret->inputs->pad = inout->pad_idx;
  398. } else if (!strcmp(inout->name, "out")) {
  399. has_out = 1;
  400. ret->outputs = av_mallocz(sizeof(AVFilterGraphDescExport));
  401. ret->outputs->filter = inout->instance;
  402. ret->outputs->pad = inout->pad_idx;
  403. } else {
  404. AVFilterInOut *p, *src, *dst;
  405. for (p = inout->next;
  406. p && strcmp(p->name,inout->name); p = p->next);
  407. if (!p) {
  408. av_log(&log_ctx, AV_LOG_ERROR, "Unmatched link: %s.\n",
  409. inout->name);
  410. goto fail;
  411. }
  412. if (p->type == LinkTypeIn && inout->type == LinkTypeOut) {
  413. src = inout;
  414. dst = p;
  415. } else if (p->type == LinkTypeOut && inout->type == LinkTypeIn) {
  416. src = p;
  417. dst = inout;
  418. } else {
  419. av_log(&log_ctx, AV_LOG_ERROR, "Two links named '%s' are either both input or both output\n",
  420. inout->name);
  421. goto fail;
  422. }
  423. linkn = av_mallocz(sizeof(AVFilterGraphDescLink));
  424. linkn->src = src->instance;
  425. linkn->srcpad = src->pad_idx;
  426. linkn->dst = dst->instance;
  427. linkn->dstpad = dst->pad_idx;
  428. *linkp = linkn;
  429. linkp = &linkn->next;
  430. src->instance = -1;
  431. dst->instance = -1;
  432. }
  433. }
  434. free_inout(head);
  435. if (!has_in) {
  436. ret->inputs = av_mallocz(sizeof(AVFilterGraphDescExport));
  437. ret->inputs->filter = 0;
  438. }
  439. if (!has_out) {
  440. ret->outputs = av_mallocz(sizeof(AVFilterGraphDescExport));
  441. ret->outputs->filter = index-1;
  442. }
  443. return ret;
  444. fail:
  445. free_inout(head);
  446. avfilter_graph_free_desc(ret);
  447. return NULL;
  448. }
  449. /**
  450. * Parse a string describing a filter graph.
  451. */
  452. AVFilterGraphDesc *avfilter_graph_parse_chain(const char *filters)
  453. {
  454. AVFilterGraphDesc *ret;
  455. /* Try first to parse supposing there is no (in) element */
  456. if ((ret = parse_chain(filters, 0)))
  457. return ret;
  458. /* Parse supposing there is an (in) element */
  459. return parse_chain(filters, 1);
  460. }
  461. /**
  462. * Free a graph description.
  463. */
  464. void avfilter_graph_free_desc(AVFilterGraphDesc *desc)
  465. {
  466. void *next;
  467. while(desc->filters) {
  468. next = desc->filters->next;
  469. av_free(desc->filters->filter);
  470. av_free(desc->filters->args);
  471. av_free(desc->filters);
  472. desc->filters = next;
  473. }
  474. while(desc->links) {
  475. next = desc->links->next;
  476. av_free(desc->links);
  477. desc->links = next;
  478. }
  479. while(desc->inputs) {
  480. next = desc->inputs->next;
  481. av_free(desc->inputs);
  482. desc->inputs = next;
  483. }
  484. while(desc->outputs) {
  485. next = desc->outputs->next;
  486. av_free(desc->outputs);
  487. desc->outputs = next;
  488. }
  489. }