avfiltergraph.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653
  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. /** Linked-list of filters to create for an AVFilterGraphDesc */
  27. typedef struct AVFilterGraphDescFilter
  28. {
  29. int index; ///< filter instance index
  30. char *filter; ///< name of filter type
  31. char *args; ///< filter parameters
  32. struct AVFilterGraphDescFilter *next;
  33. } AVFilterGraphDescFilter;
  34. /** Linked-list of links between filters */
  35. typedef struct AVFilterGraphDescLink
  36. {
  37. /* TODO: allow referencing pads by name, not just by index */
  38. int src; ///< index of the source filter
  39. unsigned srcpad; ///< index of the output pad on the source filter
  40. int dst; ///< index of the dest filter
  41. unsigned dstpad; ///< index of the input pad on the dest filter
  42. struct AVFilterGraphDescLink *next;
  43. } AVFilterGraphDescLink;
  44. /** Linked-list of filter pads to be exported from the graph */
  45. typedef struct AVFilterGraphDescExport
  46. {
  47. /* TODO: allow referencing pads by name, not just by index */
  48. char *name; ///< name of the exported pad
  49. int filter; ///< index of the filter
  50. unsigned pad; ///< index of the pad to be exported
  51. struct AVFilterGraphDescExport *next;
  52. } AVFilterGraphDescExport;
  53. /** Description of a graph to be loaded from a file, etc */
  54. typedef struct
  55. {
  56. AVFilterGraphDescFilter *filters; ///< filters in the graph
  57. AVFilterGraphDescLink *links; ///< links between the filters
  58. AVFilterGraphDescExport *inputs; ///< inputs to export
  59. AVFilterGraphDescExport *outputs; ///< outputs to export
  60. } AVFilterGraphDesc;
  61. /**
  62. * For use in av_log
  63. */
  64. static const char *log_name(void *p)
  65. {
  66. return "Filter parser";
  67. }
  68. static const AVClass filter_parser_class = {
  69. "Filter parser",
  70. log_name
  71. };
  72. static const AVClass *log_ctx = &filter_parser_class;
  73. void avfilter_destroy_graph(AVFilterGraph *graph)
  74. {
  75. for(; graph->filter_count > 0; graph->filter_count --)
  76. avfilter_destroy(graph->filters[graph->filter_count - 1]);
  77. av_freep(&graph->filters);
  78. }
  79. /* TODO: insert in sorted order */
  80. void avfilter_graph_add_filter(AVFilterGraph *graph, AVFilterContext *filter)
  81. {
  82. graph->filters = av_realloc(graph->filters,
  83. sizeof(AVFilterContext*) * ++graph->filter_count);
  84. graph->filters[graph->filter_count - 1] = filter;
  85. }
  86. /* search intelligently, once we insert in order */
  87. AVFilterContext *avfilter_graph_get_filter(AVFilterGraph *graph, char *name)
  88. {
  89. int i;
  90. if(!name)
  91. return NULL;
  92. for(i = 0; i < graph->filter_count; i ++)
  93. if(graph->filters[i]->name && !strcmp(name, graph->filters[i]->name))
  94. return graph->filters[i];
  95. return NULL;
  96. }
  97. static int query_formats(AVFilterGraph *graph)
  98. {
  99. int i, j;
  100. /* ask all the sub-filters for their supported colorspaces */
  101. for(i = 0; i < graph->filter_count; i ++) {
  102. if(graph->filters[i]->filter->query_formats)
  103. graph->filters[i]->filter->query_formats(graph->filters[i]);
  104. else
  105. avfilter_default_query_formats(graph->filters[i]);
  106. }
  107. /* go through and merge as many format lists as possible */
  108. for(i = 0; i < graph->filter_count; i ++) {
  109. AVFilterContext *filter = graph->filters[i];
  110. for(j = 0; j < filter->input_count; j ++) {
  111. AVFilterLink *link;
  112. if(!(link = filter->inputs[j]))
  113. continue;
  114. if(link->in_formats != link->out_formats) {
  115. if(!avfilter_merge_formats(link->in_formats,
  116. link->out_formats)) {
  117. /* couldn't merge format lists. auto-insert scale filter */
  118. AVFilterContext *scale;
  119. if(!(scale =
  120. avfilter_open(avfilter_get_by_name("scale"), NULL)))
  121. return -1;
  122. if(scale->filter->init(scale, NULL, NULL) ||
  123. avfilter_insert_filter(link, scale, 0, 0)) {
  124. avfilter_destroy(scale);
  125. return -1;
  126. }
  127. avfilter_graph_add_filter(graph, scale);
  128. scale->filter->query_formats(scale);
  129. if(!avfilter_merge_formats(scale-> inputs[0]->in_formats,
  130. scale-> inputs[0]->out_formats) ||
  131. !avfilter_merge_formats(scale->outputs[0]->in_formats,
  132. scale->outputs[0]->out_formats))
  133. return -1;
  134. }
  135. }
  136. }
  137. }
  138. return 0;
  139. }
  140. static void pick_format(AVFilterLink *link)
  141. {
  142. if(!link || !link->in_formats)
  143. return;
  144. link->in_formats->format_count = 1;
  145. link->format = link->in_formats->formats[0];
  146. avfilter_formats_unref(&link->in_formats);
  147. avfilter_formats_unref(&link->out_formats);
  148. }
  149. static void pick_formats(AVFilterGraph *graph)
  150. {
  151. int i, j;
  152. for(i = 0; i < graph->filter_count; i ++) {
  153. AVFilterContext *filter = graph->filters[i];
  154. for(j = 0; j < filter->input_count; j ++)
  155. pick_format(filter->inputs[j]);
  156. for(j = 0; j < filter->output_count; j ++)
  157. pick_format(filter->outputs[j]);
  158. }
  159. }
  160. int avfilter_graph_config_formats(AVFilterGraph *graph)
  161. {
  162. /* find supported formats from sub-filters, and merge along links */
  163. if(query_formats(graph))
  164. return -1;
  165. /* Once everything is merged, it's possible that we'll still have
  166. * multiple valid colorspace choices. We pick the first one. */
  167. pick_formats(graph);
  168. return 0;
  169. }
  170. static int create_filter(AVFilterGraph *ctx, int index, char *name,
  171. char *args)
  172. {
  173. AVFilterContext *filt;
  174. AVFilter *filterdef;
  175. char tmp[20];
  176. snprintf(tmp, 20, "%d", index);
  177. if(!(filterdef = avfilter_get_by_name(name)) ||
  178. !(filt = avfilter_open(filterdef, tmp))) {
  179. av_log(&log_ctx, AV_LOG_ERROR,
  180. "error creating filter '%s'\n", name);
  181. return -1;
  182. }
  183. avfilter_graph_add_filter(ctx, filt);
  184. if(avfilter_init_filter(filt, args, NULL)) {
  185. av_log(&log_ctx, AV_LOG_ERROR,
  186. "error initializing filter '%s'\n", name);
  187. return -1;
  188. }
  189. return 0;
  190. }
  191. static int link_filter(AVFilterGraph *ctx, int src, int srcpad,
  192. int dst, int dstpad)
  193. {
  194. AVFilterContext *filt, *filtb;
  195. char tmp[20];
  196. snprintf(tmp, 20, "%d", src);
  197. if(!(filt = avfilter_graph_get_filter(ctx, tmp))) {
  198. av_log(&log_ctx, AV_LOG_ERROR, "link source does not exist in graph\n");
  199. return -1;
  200. }
  201. snprintf(tmp, 20, "%d", dst);
  202. if(!(filtb = avfilter_graph_get_filter(ctx, tmp))) {
  203. av_log(&log_ctx, AV_LOG_ERROR, "link destination does not exist in graph\n");
  204. return -1;
  205. }
  206. if(avfilter_link(filt, srcpad, filtb, dstpad)) {
  207. av_log(&log_ctx, AV_LOG_ERROR, "cannot create link between source and destination filters\n");
  208. return -1;
  209. }
  210. return 0;
  211. }
  212. static int load_from_desc(AVFilterGraph *graph, AVFilterGraphDesc *desc, AVFilterContext *in, int inpad, AVFilterContext *out, int outpad)
  213. {
  214. AVFilterGraphDescExport *curpad;
  215. char tmp[20];
  216. AVFilterContext *filt;
  217. AVFilterGraphDescFilter *curfilt;
  218. AVFilterGraphDescLink *curlink;
  219. /* create all filters */
  220. for(curfilt = desc->filters; curfilt; curfilt = curfilt->next) {
  221. if (create_filter(graph, curfilt->index, curfilt->filter,
  222. curfilt->args) < 0)
  223. goto fail;
  224. }
  225. /* create all links */
  226. for(curlink = desc->links; curlink; curlink = curlink->next) {
  227. if (link_filter(graph, curlink->src, curlink->srcpad,
  228. curlink->dst, curlink->dstpad) < 0)
  229. goto fail;
  230. }
  231. /* export all input pads */
  232. for(curpad = desc->inputs; curpad; curpad = curpad->next) {
  233. snprintf(tmp, 20, "%d", curpad->filter);
  234. if(!(filt = avfilter_graph_get_filter(graph, tmp))) {
  235. av_log(&log_ctx, AV_LOG_ERROR, "filter owning exported pad does not exist\n");
  236. goto fail;
  237. }
  238. if(avfilter_link(in, inpad, filt, curpad->pad)) {
  239. av_log(&log_ctx, AV_LOG_ERROR, "cannot create link between source and destination filters\n");
  240. goto fail;
  241. }
  242. }
  243. /* export all output pads */
  244. for(curpad = desc->outputs; curpad; curpad = curpad->next) {
  245. snprintf(tmp, 20, "%d", curpad->filter);
  246. if(!(filt = avfilter_graph_get_filter(graph, tmp))) {
  247. av_log(&log_ctx, AV_LOG_ERROR, "filter owning exported pad does not exist\n");
  248. goto fail;
  249. }
  250. if(avfilter_link(filt, curpad->pad, out, outpad)) {
  251. av_log(&log_ctx, AV_LOG_ERROR, "cannot create link between source and destination filters\n");
  252. goto fail;
  253. }
  254. }
  255. return 0;
  256. fail:
  257. avfilter_destroy_graph(graph);
  258. return -1;
  259. }
  260. static void consume_whitespace(const char **buf)
  261. {
  262. *buf += strspn(*buf, " \n\t");
  263. }
  264. /**
  265. * get the next non-whitespace char
  266. */
  267. static char consume_char(const char **buf)
  268. {
  269. char out;
  270. consume_whitespace(buf);
  271. out = **buf;
  272. if (out)
  273. (*buf)++;
  274. return out;
  275. }
  276. /**
  277. * remove the quotation marks from a string. Ex: "aaa'bb'cc" -> "aaabbcc"
  278. */
  279. static void unquote(char *str)
  280. {
  281. char *p1, *p2;
  282. p1=p2=str;
  283. while (*p1 != 0) {
  284. if (*p1 != '\'')
  285. *p2++ = *p1;
  286. p1++;
  287. }
  288. *p2 = 0;
  289. }
  290. /**
  291. * Consumes a string from *buf.
  292. * @return a copy of the consumed string, which should be free'd after use
  293. */
  294. static char *consume_string(const char **buf)
  295. {
  296. const char *start;
  297. char *ret;
  298. int size;
  299. consume_whitespace(buf);
  300. if (!(**buf))
  301. return av_mallocz(1);
  302. start = *buf;
  303. *buf += strcspn(*buf, " ()=,'");
  304. if (**buf == '\'') {
  305. char *p = strchr(*buf + 1, '\'');
  306. if (p)
  307. *buf = p + 1;
  308. else
  309. *buf += strlen(*buf); // Move the pointer to the null end byte
  310. }
  311. size = *buf - start + 1;
  312. ret = av_malloc(size);
  313. memcpy(ret, start, size - 1);
  314. ret[size-1] = 0;
  315. unquote(ret);
  316. return ret;
  317. }
  318. /**
  319. * Parse "(linkname)"
  320. * @arg name a pointer (that need to be free'd after use) to the name between
  321. * parenthesis
  322. */
  323. static void parse_link_name(const char **buf, char **name)
  324. {
  325. consume_char(buf);
  326. *name = consume_string(buf);
  327. if (!*name[0])
  328. goto fail;
  329. if (consume_char(buf) != ')')
  330. goto fail;
  331. return;
  332. fail:
  333. av_freep(name);
  334. av_log(&log_ctx, AV_LOG_ERROR, "Could not parse link name!\n");
  335. }
  336. /**
  337. * Parse "filter=params"
  338. * @arg name a pointer (that need to be free'd after use) to the name of the
  339. * filter
  340. * @arg ars a pointer (that need to be free'd after use) to the args of the
  341. * filter
  342. */
  343. static void parse_filter(const char **buf, char **name, char **opts)
  344. {
  345. *name = consume_string(buf);
  346. if (**buf == '=') {
  347. consume_char(buf);
  348. *opts = consume_string(buf);
  349. } else {
  350. *opts = NULL;
  351. }
  352. }
  353. enum LinkType {
  354. LinkTypeIn,
  355. LinkTypeOut,
  356. };
  357. /**
  358. * A linked-list of the inputs/outputs of the filter chain.
  359. */
  360. typedef struct AVFilterInOut {
  361. enum LinkType type;
  362. char *name;
  363. int instance;
  364. int pad_idx;
  365. struct AVFilterInOut *next;
  366. } AVFilterInOut;
  367. static void free_inout(AVFilterInOut *head)
  368. {
  369. while (head) {
  370. AVFilterInOut *next;
  371. next = head->next;
  372. av_free(head);
  373. head = next;
  374. }
  375. }
  376. /**
  377. * Parse "(a1)(link2) ... (etc)"
  378. */
  379. static int parse_inouts(const char **buf, AVFilterInOut **inout, int firstpad,
  380. enum LinkType type, int instance)
  381. {
  382. int pad = firstpad;
  383. while (**buf == '(') {
  384. AVFilterInOut *inoutn = av_malloc(sizeof(AVFilterInOut));
  385. parse_link_name(buf, &inoutn->name);
  386. inoutn->type = type;
  387. inoutn->instance = instance;
  388. inoutn->pad_idx = pad++;
  389. inoutn->next = *inout;
  390. *inout = inoutn;
  391. }
  392. return pad;
  393. }
  394. /**
  395. * Free a graph description.
  396. */
  397. static void free_desc(AVFilterGraphDesc *desc)
  398. {
  399. void *next;
  400. while(desc->filters) {
  401. next = desc->filters->next;
  402. av_free(desc->filters->filter);
  403. av_free(desc->filters->args);
  404. av_free(desc->filters);
  405. desc->filters = next;
  406. }
  407. while(desc->links) {
  408. next = desc->links->next;
  409. av_free(desc->links);
  410. desc->links = next;
  411. }
  412. while(desc->inputs) {
  413. next = desc->inputs->next;
  414. av_free(desc->inputs);
  415. desc->inputs = next;
  416. }
  417. while(desc->outputs) {
  418. next = desc->outputs->next;
  419. av_free(desc->outputs);
  420. desc->outputs = next;
  421. }
  422. }
  423. static AVFilterGraphDesc *parse_chain(const char *filters, int has_in)
  424. {
  425. AVFilterGraphDesc *ret;
  426. AVFilterGraphDescFilter **filterp, *filtern;
  427. AVFilterGraphDescLink **linkp, *linkn;
  428. AVFilterInOut *inout=NULL;
  429. AVFilterInOut *head;
  430. int index = 0;
  431. char chr = 0;
  432. int pad = 0;
  433. int has_out = 0;
  434. consume_whitespace(&filters);
  435. if(!(ret = av_mallocz(sizeof(AVFilterGraphDesc))))
  436. return NULL;
  437. filterp = &ret->filters;
  438. linkp = &ret->links;
  439. do {
  440. if(chr == ',') {
  441. linkn = av_mallocz(sizeof(AVFilterGraphDescLink));
  442. linkn->src = index-1;
  443. linkn->srcpad = pad;
  444. linkn->dst = index;
  445. linkn->dstpad = 0;
  446. *linkp = linkn;
  447. linkp = &linkn->next;
  448. }
  449. pad = parse_inouts(&filters, &inout, chr == ',' || (!has_in),
  450. LinkTypeIn, index);
  451. filtern = av_mallocz(sizeof(AVFilterGraphDescFilter));
  452. filtern->index = index;
  453. parse_filter(&filters, &filtern->filter, &filtern->args);
  454. *filterp = filtern;
  455. filterp = &filtern->next;
  456. pad = parse_inouts(&filters, &inout, 0,
  457. LinkTypeOut, index);
  458. chr = consume_char(&filters);
  459. index++;
  460. } while (chr == ',' || chr == ';');
  461. head = inout;
  462. for (; inout != NULL; inout = inout->next) {
  463. if (inout->instance == -1)
  464. continue; // Already processed
  465. if (!strcmp(inout->name, "in")) {
  466. if (!has_in)
  467. goto fail;
  468. ret->inputs = av_mallocz(sizeof(AVFilterGraphDescExport));
  469. ret->inputs->filter = inout->instance;
  470. ret->inputs->pad = inout->pad_idx;
  471. } else if (!strcmp(inout->name, "out")) {
  472. has_out = 1;
  473. ret->outputs = av_mallocz(sizeof(AVFilterGraphDescExport));
  474. ret->outputs->filter = inout->instance;
  475. ret->outputs->pad = inout->pad_idx;
  476. } else {
  477. AVFilterInOut *p, *src, *dst;
  478. for (p = inout->next;
  479. p && strcmp(p->name,inout->name); p = p->next);
  480. if (!p) {
  481. av_log(&log_ctx, AV_LOG_ERROR, "Unmatched link: %s.\n",
  482. inout->name);
  483. goto fail;
  484. }
  485. if (p->type == LinkTypeIn && inout->type == LinkTypeOut) {
  486. src = inout;
  487. dst = p;
  488. } else if (p->type == LinkTypeOut && inout->type == LinkTypeIn) {
  489. src = p;
  490. dst = inout;
  491. } else {
  492. av_log(&log_ctx, AV_LOG_ERROR, "Two links named '%s' are either both input or both output\n",
  493. inout->name);
  494. goto fail;
  495. }
  496. linkn = av_mallocz(sizeof(AVFilterGraphDescLink));
  497. linkn->src = src->instance;
  498. linkn->srcpad = src->pad_idx;
  499. linkn->dst = dst->instance;
  500. linkn->dstpad = dst->pad_idx;
  501. *linkp = linkn;
  502. linkp = &linkn->next;
  503. src->instance = -1;
  504. dst->instance = -1;
  505. }
  506. }
  507. free_inout(head);
  508. if (!has_in) {
  509. ret->inputs = av_mallocz(sizeof(AVFilterGraphDescExport));
  510. ret->inputs->filter = 0;
  511. }
  512. if (!has_out) {
  513. ret->outputs = av_mallocz(sizeof(AVFilterGraphDescExport));
  514. ret->outputs->filter = index-1;
  515. }
  516. return ret;
  517. fail:
  518. free_inout(head);
  519. free_desc(ret);
  520. return NULL;
  521. }
  522. /**
  523. * Parse a string describing a filter graph.
  524. */
  525. int avfilter_graph_parse_chain(AVFilterGraph *graph, const char *filters, AVFilterContext *in, int inpad, AVFilterContext *out, int outpad)
  526. {
  527. AVFilterGraphDesc *desc;
  528. /* Try first to parse supposing there is no (in) element */
  529. if (!(desc = parse_chain(filters, 0))) {
  530. /* If it didn't work, parse supposing there is an (in) element */
  531. desc = parse_chain(filters, 1);
  532. }
  533. if (!desc)
  534. return -1;
  535. if (load_from_desc(graph, desc, in, inpad, out, outpad) < 0) {
  536. free_desc(desc);
  537. return -1;
  538. }
  539. free_desc(desc);
  540. return 0;
  541. }