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.

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