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.

704 lines
19KB

  1. /*
  2. * Filter graphs
  3. * copyright (c) 2007 Bobby Bingham
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include <string.h>
  22. #include <stddef.h>
  23. #include "avstring.h"
  24. #include "avfilter.h"
  25. #include "avfiltergraph.h"
  26. #include "allfilters.h"
  27. typedef struct AVFilterGraph {
  28. unsigned filter_count;
  29. AVFilterContext **filters;
  30. /** fake filter to handle links to internal filters */
  31. AVFilterContext *link_filter;
  32. } GraphContext;
  33. typedef struct {
  34. AVFilterContext *graph;
  35. } GraphLinkContext;
  36. static int link_init(AVFilterContext *ctx, const char *args, void *opaque)
  37. {
  38. GraphLinkContext *linkctx = ctx->priv;
  39. linkctx->graph = opaque;
  40. return !opaque;
  41. }
  42. /**
  43. * Given the link between the dummy filter and an internal filter whose input
  44. * is being exported outside the graph, this returns the externally visible
  45. * link
  46. */
  47. static inline AVFilterLink *get_extern_input_link(AVFilterLink *link)
  48. {
  49. GraphLinkContext *lctx = link->src->priv;
  50. return lctx->graph->inputs[link->srcpad];
  51. }
  52. /** query the formats supported by a filter providing input to the graph */
  53. static int *link_in_query_formats(AVFilterLink *link)
  54. {
  55. AVFilterLink *link2 = get_extern_input_link(link);
  56. int *(*query_formats)(AVFilterLink *);
  57. if(!link2)
  58. return avfilter_make_format_list(0);
  59. if(!(query_formats = link2->src->output_pads[link2->srcpad].query_formats))
  60. query_formats = avfilter_default_query_output_formats;
  61. return query_formats(link2);
  62. }
  63. /** request a frame from a filter providing input to the graph */
  64. static int link_in_request_frame(AVFilterLink *link)
  65. {
  66. AVFilterLink *link2 = get_extern_input_link(link);
  67. if(!link2)
  68. return -1;
  69. return avfilter_request_frame(link2);
  70. }
  71. static int link_in_config_props(AVFilterLink *link)
  72. {
  73. AVFilterLink *link2 = get_extern_input_link(link);
  74. int (*config_props)(AVFilterLink *);
  75. int ret;
  76. if(!link2)
  77. return -1;
  78. if(!(config_props = link2->src->output_pads[link2->srcpad].config_props))
  79. config_props = avfilter_default_config_output_link;
  80. ret = config_props(link2);
  81. link->w = link2->w;
  82. link->h = link2->h;
  83. return ret;
  84. }
  85. /**
  86. * Given the link between the dummy filter and an internal filter whose input
  87. * is being exported outside the graph, this returns the externally visible
  88. * link
  89. */
  90. static inline AVFilterLink *get_extern_output_link(AVFilterLink *link)
  91. {
  92. GraphLinkContext *lctx = link->dst->priv;
  93. return lctx->graph->outputs[link->dstpad];
  94. }
  95. /** query the formats supported by a filter taking output from the graph */
  96. static int *link_out_query_formats(AVFilterLink *link)
  97. {
  98. AVFilterLink *link2 = get_extern_output_link(link);
  99. if(!link2)
  100. return avfilter_make_format_list(0);
  101. return link2->dst->input_pads[link2->dstpad].query_formats(link2);
  102. }
  103. static int link_out_config_props(AVFilterLink *link)
  104. {
  105. AVFilterLink *link2 = get_extern_output_link(link);
  106. int (*config_props)(AVFilterLink *);
  107. if(!link2)
  108. return 0;
  109. link2->w = link->w;
  110. link2->h = link->h;
  111. if(!(config_props = link2->dst->input_pads[link2->dstpad].config_props))
  112. config_props = avfilter_default_config_input_link;
  113. return config_props(link2);
  114. }
  115. static void link_out_start_frame(AVFilterLink *link, AVFilterPicRef *picref)
  116. {
  117. AVFilterLink *link2 = get_extern_output_link(link);
  118. if(!link2)
  119. avfilter_unref_pic(picref);
  120. else
  121. avfilter_start_frame(link2, picref);
  122. }
  123. static void link_out_end_frame(AVFilterLink *link)
  124. {
  125. AVFilterLink *link2 = get_extern_output_link(link);
  126. if(link2)
  127. avfilter_end_frame(link2);
  128. }
  129. static AVFilterPicRef *link_out_get_video_buffer(AVFilterLink *link, int perms)
  130. {
  131. AVFilterLink *link2 = get_extern_output_link(link);
  132. if(!link2)
  133. return NULL;
  134. else
  135. return avfilter_get_video_buffer(link2, perms);
  136. }
  137. static void link_out_draw_slice(AVFilterLink *link, int y, int height)
  138. {
  139. AVFilterLink *link2 = get_extern_output_link(link);
  140. if(link2)
  141. avfilter_draw_slice(link2, y, height);
  142. }
  143. /** dummy filter used to help export filters pads outside the graph */
  144. static AVFilter vf_graph_dummy =
  145. {
  146. .name = "graph_dummy",
  147. .author = "Bobby Bingham",
  148. .priv_size = sizeof(GraphLinkContext),
  149. .init = link_init,
  150. .inputs = (AVFilterPad[]) {{ .name = NULL, }},
  151. .outputs = (AVFilterPad[]) {{ .name = NULL, }},
  152. };
  153. static AVFilterLink *get_intern_input_link(AVFilterLink *link)
  154. {
  155. GraphContext *graph = link->dst->priv;
  156. return graph->link_filter->outputs[link->dstpad];
  157. }
  158. static void graph_in_start_frame(AVFilterLink *link, AVFilterPicRef *picref)
  159. {
  160. AVFilterLink *link2 = get_intern_input_link(link);
  161. if(link2)
  162. avfilter_start_frame(link2, picref);
  163. }
  164. static void graph_in_end_frame(AVFilterLink *link)
  165. {
  166. AVFilterLink *link2 = get_intern_input_link(link);
  167. if(link2)
  168. avfilter_end_frame(link2);
  169. }
  170. static AVFilterPicRef *graph_in_get_video_buffer(AVFilterLink *link, int perms)
  171. {
  172. AVFilterLink *link2 = get_intern_input_link(link);
  173. if(link2)
  174. return avfilter_get_video_buffer(link2, perms);
  175. return NULL;
  176. }
  177. static void graph_in_draw_slice(AVFilterLink *link, int y, int height)
  178. {
  179. AVFilterLink *link2 = get_intern_input_link(link);
  180. if(link2)
  181. avfilter_draw_slice(link2, y, height);
  182. }
  183. static int *graph_in_query_formats(AVFilterLink *link)
  184. {
  185. AVFilterLink *link2 = get_intern_input_link(link);
  186. if(!link2 || !link2->dst->input_pads[link2->dstpad].query_formats)
  187. return avfilter_make_format_list(0);
  188. return link2->dst->input_pads[link2->dstpad].query_formats(link2);
  189. }
  190. static int graph_in_config_props(AVFilterLink *link)
  191. {
  192. AVFilterLink *link2 = get_intern_input_link(link);
  193. int (*config_props)(AVFilterLink *);
  194. if(!link2)
  195. return -1;
  196. /* copy link properties over to the dummy internal link */
  197. link2->w = link->w;
  198. link2->h = link->h;
  199. link2->format = link->format;
  200. if(!(config_props = link2->dst->input_pads[link2->dstpad].config_props))
  201. return 0; /* FIXME? */
  202. //config_props = avfilter_default_config_input_link;
  203. return config_props(link2);
  204. }
  205. static AVFilterLink *get_intern_output_link(AVFilterLink *link)
  206. {
  207. GraphContext *graph = link->src->priv;
  208. return graph->link_filter->inputs[link->srcpad];
  209. }
  210. static int *graph_out_query_formats(AVFilterLink *link)
  211. {
  212. AVFilterLink *link2 = get_intern_output_link(link);
  213. if(!link2)
  214. return avfilter_make_format_list(0);
  215. if(!link2->src->output_pads[link2->srcpad].query_formats)
  216. return avfilter_default_query_output_formats(link2);
  217. return link2->src->output_pads[link2->srcpad].query_formats(link2);
  218. }
  219. static int graph_out_request_frame(AVFilterLink *link)
  220. {
  221. AVFilterLink *link2 = get_intern_output_link(link);
  222. if(link2)
  223. return avfilter_request_frame(link2);
  224. return -1;
  225. }
  226. static int graph_out_config_props(AVFilterLink *link)
  227. {
  228. AVFilterLink *link2 = get_intern_output_link(link);
  229. int (*config_props)(AVFilterLink *);
  230. int ret;
  231. if(!link2)
  232. return 0;
  233. link2->w = link->w;
  234. link2->h = link->h;
  235. link2->format = link->format;
  236. if(!(config_props = link2->src->output_pads[link2->srcpad].config_props))
  237. config_props = avfilter_default_config_output_link;
  238. ret = config_props(link2);
  239. link->w = link2->w;
  240. link->h = link2->h;
  241. link->format = link2->format;
  242. return ret;
  243. }
  244. static int add_graph_input(AVFilterContext *gctx, AVFilterContext *filt, unsigned idx,
  245. char *name)
  246. {
  247. GraphContext *graph = gctx->priv;
  248. AVFilterPad graph_inpad =
  249. {
  250. .name = name,
  251. .type = AV_PAD_VIDEO,
  252. .start_frame = graph_in_start_frame,
  253. .end_frame = graph_in_end_frame,
  254. .get_video_buffer = graph_in_get_video_buffer,
  255. .draw_slice = graph_in_draw_slice,
  256. .query_formats = graph_in_query_formats,
  257. .config_props = graph_in_config_props,
  258. /* XXX */
  259. };
  260. AVFilterPad dummy_outpad =
  261. {
  262. .name = NULL, /* FIXME? */
  263. .type = AV_PAD_VIDEO,
  264. .query_formats = link_in_query_formats,
  265. .request_frame = link_in_request_frame,
  266. .config_props = link_in_config_props,
  267. };
  268. avfilter_insert_inpad (gctx, gctx->input_count, &graph_inpad);
  269. avfilter_insert_outpad(graph->link_filter, graph->link_filter->output_count,
  270. &dummy_outpad);
  271. return avfilter_link(graph->link_filter,
  272. graph->link_filter->output_count-1, filt, idx);
  273. }
  274. static int add_graph_output(AVFilterContext *gctx, AVFilterContext *filt, unsigned idx,
  275. char *name)
  276. {
  277. GraphContext *graph = gctx->priv;
  278. AVFilterPad graph_outpad =
  279. {
  280. .name = name,
  281. .type = AV_PAD_VIDEO,
  282. .request_frame = graph_out_request_frame,
  283. .query_formats = graph_out_query_formats,
  284. .config_props = graph_out_config_props,
  285. };
  286. AVFilterPad dummy_inpad =
  287. {
  288. .name = NULL, /* FIXME? */
  289. .type = AV_PAD_VIDEO,
  290. .start_frame = link_out_start_frame,
  291. .end_frame = link_out_end_frame,
  292. .draw_slice = link_out_draw_slice,
  293. .get_video_buffer = link_out_get_video_buffer,
  294. .query_formats = link_out_query_formats,
  295. .config_props = link_out_config_props,
  296. };
  297. avfilter_insert_outpad(gctx, gctx->output_count, &graph_outpad);
  298. avfilter_insert_inpad (graph->link_filter, graph->link_filter->input_count,
  299. &dummy_inpad);
  300. return avfilter_link(filt, idx, graph->link_filter,
  301. graph->link_filter->input_count-1);
  302. }
  303. static void uninit(AVFilterContext *ctx)
  304. {
  305. GraphContext *graph = ctx->priv;
  306. if(graph->link_filter) {
  307. avfilter_destroy(graph->link_filter);
  308. graph->link_filter = NULL;
  309. }
  310. for(; graph->filter_count > 0; graph->filter_count --)
  311. avfilter_destroy(graph->filters[graph->filter_count - 1]);
  312. av_freep(&graph->filters);
  313. }
  314. /* TODO: insert in sorted order */
  315. void avfilter_graph_add_filter(AVFilterContext *graphctx, AVFilterContext *filter)
  316. {
  317. GraphContext *graph = graphctx->priv;
  318. graph->filters = av_realloc(graph->filters,
  319. sizeof(AVFilterContext*) * ++graph->filter_count);
  320. graph->filters[graph->filter_count - 1] = filter;
  321. }
  322. /* search intelligently, once we insert in order */
  323. AVFilterContext *avfilter_graph_get_filter(AVFilterContext *ctx, char *name)
  324. {
  325. GraphContext *graph = ctx->priv;
  326. int i;
  327. if(!name)
  328. return NULL;
  329. for(i = 0; i < graph->filter_count; i ++)
  330. if(graph->filters[i]->name && !strcmp(name, graph->filters[i]->name))
  331. return graph->filters[i];
  332. return NULL;
  333. }
  334. int avfilter_graph_config_links(AVFilterContext *graphctx)
  335. {
  336. GraphContext *graph = graphctx->priv;
  337. int i, j;
  338. for(i = 0; i < graph->filter_count; i ++) {
  339. for(j = 0; j < graph->filters[i]->input_count; j ++) {
  340. /* ensure that graphs contained within graphs are configured */
  341. if((graph->filters[i]->filter == &avfilter_vf_graph ||
  342. graph->filters[i]->filter == &avfilter_vf_graphfile ||
  343. graph->filters[i]->filter == &avfilter_vf_graphdesc) &&
  344. avfilter_graph_config_links(graph->filters[i]))
  345. return -1;
  346. if(avfilter_config_link(graph->filters[i]->inputs[j]))
  347. return -1;
  348. }
  349. }
  350. return 0;
  351. }
  352. static AVFilterContext *create_filter_with_args(const char *filt, void *opaque)
  353. {
  354. AVFilterContext *ret;
  355. char *filter = av_strdup(filt); /* copy - don't mangle the input string */
  356. char *name, *args;
  357. name = filter;
  358. if((args = strchr(filter, '='))) {
  359. /* ensure we at least have a name */
  360. if(args == filter)
  361. goto fail;
  362. *args ++ = 0;
  363. }
  364. av_log(NULL, AV_LOG_INFO, "creating filter \"%s\" with args \"%s\"\n",
  365. name, args ? args : "(none)");
  366. if((ret = avfilter_open(avfilter_get_by_name(name), NULL))) {
  367. if(avfilter_init_filter(ret, args, opaque)) {
  368. av_log(NULL, AV_LOG_ERROR, "error initializing filter!\n");
  369. avfilter_destroy(ret);
  370. goto fail;
  371. }
  372. } else {
  373. av_log(NULL, AV_LOG_ERROR,
  374. "error creating filter \"%s\" with args \"%s\"\n",
  375. name, args ? args : "(none)");
  376. return NULL;
  377. }
  378. av_free(filter);
  379. return ret;
  380. fail:
  381. av_free(filter);
  382. return NULL;
  383. }
  384. static int graph_load_chain(AVFilterContext *graphctx,
  385. unsigned count, char **filter_list, void **opaque,
  386. AVFilterContext **first, AVFilterContext **last)
  387. {
  388. unsigned i;
  389. AVFilterContext *filters[2] = {NULL,NULL};
  390. for(i = 0; i < count; i ++) {
  391. void *op;
  392. if(opaque) op = opaque[i];
  393. else op = NULL;
  394. if(!(filters[1] = create_filter_with_args(filter_list[i], op)))
  395. goto fail;
  396. if(i == 0) {
  397. if(first) *first = filters[1];
  398. } else {
  399. if(avfilter_link(filters[0], 0, filters[1], 0)) {
  400. av_log(NULL, AV_LOG_ERROR, "error linking filters!\n");
  401. goto fail;
  402. }
  403. }
  404. avfilter_graph_add_filter(graphctx, filters[1]);
  405. if(i == 0 && filters[1]->input_count > 0)
  406. add_graph_input(graphctx, filters[1], 0, "default");
  407. filters[0] = filters[1];
  408. }
  409. if(filters[1]->output_count > 0)
  410. add_graph_output(graphctx, filters[1], 0, "default");
  411. if(last) *last = filters[1];
  412. return 0;
  413. fail:
  414. uninit(graphctx);
  415. if(first) *first = NULL;
  416. if(last) *last = NULL;
  417. return -1;
  418. }
  419. static int graph_load_chain_from_string(AVFilterContext *ctx, const char *str,
  420. AVFilterContext **first,
  421. AVFilterContext **last)
  422. {
  423. int count, ret = 0;
  424. char **strings;
  425. char *filt;
  426. strings = av_malloc(sizeof(char *));
  427. strings[0] = av_strdup(str);
  428. filt = strchr(strings[0], ',');
  429. for(count = 1; filt; count ++) {
  430. if(filt == strings[count-1]) {
  431. ret = -1;
  432. goto done;
  433. }
  434. strings = av_realloc(strings, sizeof(char *) * (count+1));
  435. strings[count] = filt + 1;
  436. *filt = '\0';
  437. filt = strchr(strings[count], ',');
  438. }
  439. ret = graph_load_chain(ctx, count, strings, NULL, first, last);
  440. done:
  441. av_free(strings[0]);
  442. av_free(strings);
  443. return ret;
  444. }
  445. static int init(AVFilterContext *ctx, const char *args, void *opaque)
  446. {
  447. GraphContext *gctx = ctx->priv;
  448. if(!(gctx->link_filter = avfilter_open(&vf_graph_dummy, NULL)))
  449. return -1;
  450. if(avfilter_init_filter(gctx->link_filter, NULL, ctx))
  451. goto fail;
  452. if(!args)
  453. return 0;
  454. return graph_load_chain_from_string(ctx, args, NULL, NULL);
  455. fail:
  456. avfilter_destroy(gctx->link_filter);
  457. return -1;
  458. }
  459. AVFilter avfilter_vf_graph =
  460. {
  461. .name = "graph",
  462. .author = "Bobby Bingham",
  463. .priv_size = sizeof(GraphContext),
  464. .init = init,
  465. .uninit = uninit,
  466. .inputs = (AVFilterPad[]) {{ .name = NULL, }},
  467. .outputs = (AVFilterPad[]) {{ .name = NULL, }},
  468. };
  469. static int graph_load_from_desc(AVFilterContext *ctx, AVFilterGraphDesc *desc)
  470. {
  471. AVFilterGraphDescFilter *curfilt;
  472. AVFilterGraphDescLink *curlink;
  473. AVFilterGraphDescExport *curpad;
  474. AVFilterContext *filt, *filtb;
  475. AVFilter *filterdef;
  476. /* create all filters */
  477. for(curfilt = desc->filters; curfilt; curfilt = curfilt->next) {
  478. if(!(filterdef = avfilter_get_by_name(curfilt->filter)) ||
  479. !(filt = avfilter_open(filterdef, curfilt->name))) {
  480. av_log(ctx, AV_LOG_ERROR, "error creating filter\n");
  481. goto fail;
  482. }
  483. avfilter_graph_add_filter(ctx, filt);
  484. if(avfilter_init_filter(filt, curfilt->args, NULL)) {
  485. av_log(ctx, AV_LOG_ERROR, "error initializing filter\n");
  486. goto fail;
  487. }
  488. }
  489. /* create all links */
  490. for(curlink = desc->links; curlink; curlink = curlink->next) {
  491. if(!(filt = avfilter_graph_get_filter(ctx, curlink->src))) {
  492. av_log(ctx, AV_LOG_ERROR, "link source does not exist in graph\n");
  493. goto fail;
  494. }
  495. if(!(filtb = avfilter_graph_get_filter(ctx, curlink->dst))) {
  496. av_log(ctx, AV_LOG_ERROR, "link destination does not exist in graph\n");
  497. goto fail;
  498. }
  499. if(avfilter_link(filt, curlink->srcpad, filtb, curlink->dstpad)) {
  500. av_log(ctx, AV_LOG_ERROR, "cannot create link between source and destination filters\n");
  501. goto fail;
  502. }
  503. }
  504. /* export all input pads */
  505. for(curpad = desc->inputs; curpad; curpad = curpad->next) {
  506. if(!(filt = avfilter_graph_get_filter(ctx, curpad->filter))) {
  507. av_log(ctx, AV_LOG_ERROR, "filter owning exported pad does not exist\n");
  508. goto fail;
  509. }
  510. add_graph_input(ctx, filt, curpad->pad, curpad->name);
  511. }
  512. /* export all output pads */
  513. for(curpad = desc->outputs; curpad; curpad = curpad->next) {
  514. if(!(filt = avfilter_graph_get_filter(ctx, curpad->filter))) {
  515. av_log(ctx, AV_LOG_ERROR, "filter owning exported pad does not exist\n");
  516. goto fail;
  517. }
  518. add_graph_output(ctx, filt, curpad->pad, curpad->name);
  519. }
  520. return 0;
  521. fail:
  522. uninit(ctx);
  523. return -1;
  524. }
  525. static int init_desc(AVFilterContext *ctx, const char *args, void *opaque)
  526. {
  527. GraphContext *gctx = ctx->priv;
  528. if(!opaque)
  529. return -1;
  530. if(!(gctx->link_filter = avfilter_open(&vf_graph_dummy, NULL)))
  531. return -1;
  532. if(avfilter_init_filter(gctx->link_filter, NULL, ctx))
  533. goto fail;
  534. return graph_load_from_desc(ctx, opaque);
  535. fail:
  536. avfilter_destroy(gctx->link_filter);
  537. return -1;
  538. }
  539. AVFilter avfilter_vf_graphdesc =
  540. {
  541. .name = "graph_desc",
  542. .author = "Bobby Bingham",
  543. .priv_size = sizeof(GraphContext),
  544. .init = init_desc,
  545. .uninit = uninit,
  546. .inputs = (AVFilterPad[]) {{ .name = NULL, }},
  547. .outputs = (AVFilterPad[]) {{ .name = NULL, }},
  548. };
  549. static int init_file(AVFilterContext *ctx, const char *args, void *opaque)
  550. {
  551. AVFilterGraphDesc *desc;
  552. int ret;
  553. if(!args)
  554. return -1;
  555. if(!(desc = avfilter_graph_load_desc(args)))
  556. return -1;
  557. ret = init_desc(ctx, NULL, desc);
  558. avfilter_graph_free_desc(desc);
  559. return ret;
  560. }
  561. AVFilter avfilter_vf_graphfile =
  562. {
  563. .name = "graph_file",
  564. .author = "Bobby Bingham",
  565. .priv_size = sizeof(GraphContext),
  566. .init = init_file,
  567. .uninit = uninit,
  568. .inputs = (AVFilterPad[]) {{ .name = NULL, }},
  569. .outputs = (AVFilterPad[]) {{ .name = NULL, }},
  570. };