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.

702 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. //.uninit = uninit,
  151. .inputs = (AVFilterPad[]) {{ .name = NULL, }},
  152. .outputs = (AVFilterPad[]) {{ .name = NULL, }},
  153. };
  154. static AVFilterLink *get_intern_input_link(AVFilterLink *link)
  155. {
  156. GraphContext *graph = link->dst->priv;
  157. return graph->link_filter->outputs[link->dstpad];
  158. }
  159. static void graph_in_start_frame(AVFilterLink *link, AVFilterPicRef *picref)
  160. {
  161. AVFilterLink *link2 = get_intern_input_link(link);
  162. if(link2)
  163. avfilter_start_frame(link2, picref);
  164. }
  165. static void graph_in_end_frame(AVFilterLink *link)
  166. {
  167. AVFilterLink *link2 = get_intern_input_link(link);
  168. if(link2)
  169. avfilter_end_frame(link2);
  170. }
  171. static AVFilterPicRef *graph_in_get_video_buffer(AVFilterLink *link, int perms)
  172. {
  173. AVFilterLink *link2 = get_intern_input_link(link);
  174. if(link2)
  175. return avfilter_get_video_buffer(link2, perms);
  176. return NULL;
  177. }
  178. static void graph_in_draw_slice(AVFilterLink *link, int y, int height)
  179. {
  180. AVFilterLink *link2 = get_intern_input_link(link);
  181. if(link2)
  182. avfilter_draw_slice(link2, y, height);
  183. }
  184. static int *graph_in_query_formats(AVFilterLink *link)
  185. {
  186. AVFilterLink *link2 = get_intern_input_link(link);
  187. if(!link2 || !link2->dst->input_pads[link2->dstpad].query_formats)
  188. return avfilter_make_format_list(0);
  189. return link2->dst->input_pads[link2->dstpad].query_formats(link2);
  190. }
  191. static int graph_in_config_props(AVFilterLink *link)
  192. {
  193. AVFilterLink *link2 = get_intern_input_link(link);
  194. int (*config_props)(AVFilterLink *);
  195. if(!link2)
  196. return -1;
  197. /* copy link properties over to the dummy internal link */
  198. link2->w = link->w;
  199. link2->h = link->h;
  200. link2->format = link->format;
  201. if(!(config_props = link2->dst->input_pads[link2->dstpad].config_props))
  202. return 0; /* FIXME? */
  203. //config_props = avfilter_default_config_input_link;
  204. return config_props(link2);
  205. }
  206. static AVFilterLink *get_intern_output_link(AVFilterLink *link)
  207. {
  208. GraphContext *graph = link->src->priv;
  209. return graph->link_filter->inputs[link->srcpad];
  210. }
  211. static int *graph_out_query_formats(AVFilterLink *link)
  212. {
  213. AVFilterLink *link2 = get_intern_output_link(link);
  214. if(!link2)
  215. return avfilter_make_format_list(0);
  216. if(!link2->src->output_pads[link2->srcpad].query_formats)
  217. return avfilter_default_query_output_formats(link2);
  218. return link2->src->output_pads[link2->srcpad].query_formats(link2);
  219. }
  220. static int graph_out_request_frame(AVFilterLink *link)
  221. {
  222. AVFilterLink *link2 = get_intern_output_link(link);
  223. if(link2)
  224. return avfilter_request_frame(link2);
  225. return -1;
  226. }
  227. static int graph_out_config_props(AVFilterLink *link)
  228. {
  229. AVFilterLink *link2 = get_intern_output_link(link);
  230. int (*config_props)(AVFilterLink *);
  231. int ret;
  232. if(!link2)
  233. return 0;
  234. link2->w = link->w;
  235. link2->h = link->h;
  236. link2->format = link->format;
  237. if(!(config_props = link2->src->output_pads[link2->srcpad].config_props))
  238. config_props = avfilter_default_config_output_link;
  239. ret = config_props(link2);
  240. link->w = link2->w;
  241. link->h = link2->h;
  242. link->format = link2->format;
  243. return ret;
  244. }
  245. static int add_graph_input(AVFilterContext *gctx, AVFilterContext *filt, unsigned idx,
  246. char *name)
  247. {
  248. GraphContext *graph = gctx->priv;
  249. AVFilterPad graph_inpad =
  250. {
  251. .name = name,
  252. .type = AV_PAD_VIDEO,
  253. .start_frame = graph_in_start_frame,
  254. .end_frame = graph_in_end_frame,
  255. .get_video_buffer = graph_in_get_video_buffer,
  256. .draw_slice = graph_in_draw_slice,
  257. .query_formats = graph_in_query_formats,
  258. .config_props = graph_in_config_props,
  259. /* XXX */
  260. };
  261. AVFilterPad dummy_outpad =
  262. {
  263. .name = NULL, /* FIXME? */
  264. .type = AV_PAD_VIDEO,
  265. .query_formats = link_in_query_formats,
  266. .request_frame = link_in_request_frame,
  267. .config_props = link_in_config_props,
  268. };
  269. avfilter_insert_inpad (gctx, gctx->input_count, &graph_inpad);
  270. avfilter_insert_outpad(graph->link_filter, graph->link_filter->output_count,
  271. &dummy_outpad);
  272. return avfilter_link(graph->link_filter,
  273. graph->link_filter->output_count-1, filt, idx);
  274. }
  275. static int add_graph_output(AVFilterContext *gctx, AVFilterContext *filt, unsigned idx,
  276. char *name)
  277. {
  278. GraphContext *graph = gctx->priv;
  279. AVFilterPad graph_outpad =
  280. {
  281. .name = name,
  282. .type = AV_PAD_VIDEO,
  283. .request_frame = graph_out_request_frame,
  284. .query_formats = graph_out_query_formats,
  285. .config_props = graph_out_config_props,
  286. };
  287. AVFilterPad dummy_inpad =
  288. {
  289. .name = NULL, /* FIXME? */
  290. .type = AV_PAD_VIDEO,
  291. .start_frame = link_out_start_frame,
  292. .end_frame = link_out_end_frame,
  293. .draw_slice = link_out_draw_slice,
  294. .get_video_buffer = link_out_get_video_buffer,
  295. .query_formats = link_out_query_formats,
  296. .config_props = link_out_config_props,
  297. };
  298. avfilter_insert_outpad(gctx, gctx->output_count, &graph_outpad);
  299. avfilter_insert_inpad (graph->link_filter, graph->link_filter->input_count,
  300. &dummy_inpad);
  301. return avfilter_link(filt, idx, graph->link_filter,
  302. graph->link_filter->input_count-1);
  303. }
  304. static void uninit(AVFilterContext *ctx)
  305. {
  306. GraphContext *graph = ctx->priv;
  307. if(graph->link_filter) {
  308. avfilter_destroy(graph->link_filter);
  309. graph->link_filter = NULL;
  310. }
  311. for(; graph->filter_count > 0; graph->filter_count --)
  312. avfilter_destroy(graph->filters[graph->filter_count - 1]);
  313. av_freep(&graph->filters);
  314. }
  315. /* TODO: insert in sorted order */
  316. void avfilter_graph_add_filter(AVFilterContext *graphctx, AVFilterContext *filter)
  317. {
  318. GraphContext *graph = graphctx->priv;
  319. graph->filters = av_realloc(graph->filters,
  320. sizeof(AVFilterContext*) * ++graph->filter_count);
  321. graph->filters[graph->filter_count - 1] = filter;
  322. }
  323. /* search intelligently, once we insert in order */
  324. AVFilterContext *avfilter_graph_get_filter(AVFilterContext *ctx, char *name)
  325. {
  326. GraphContext *graph = ctx->priv;
  327. int i;
  328. if(!name)
  329. return NULL;
  330. for(i = 0; i < graph->filter_count; i ++)
  331. if(graph->filters[i]->name && !strcmp(name, graph->filters[i]->name))
  332. return graph->filters[i];
  333. return NULL;
  334. }
  335. int avfilter_graph_config_links(AVFilterContext *graphctx)
  336. {
  337. GraphContext *graph = graphctx->priv;
  338. int i, j;
  339. for(i = 0; i < graph->filter_count; i ++) {
  340. for(j = 0; j < graph->filters[i]->input_count; j ++) {
  341. /* ensure that graphs contained within graphs are configured */
  342. if((graph->filters[i]->filter == &vf_graph ||
  343. graph->filters[i]->filter == &vf_graphfile ||
  344. graph->filters[i]->filter == &vf_graphdesc) &&
  345. avfilter_graph_config_links(graph->filters[i]))
  346. return -1;
  347. if(avfilter_config_link(graph->filters[i]->inputs[j]))
  348. return -1;
  349. }
  350. }
  351. return 0;
  352. }
  353. static AVFilterContext *create_filter_with_args(const char *filt, void *opaque)
  354. {
  355. AVFilter *filterdef;
  356. AVFilterContext *ret;
  357. char *filter = av_strdup(filt); /* copy - don't mangle the input string */
  358. char *name, *args;
  359. name = filter;
  360. if((args = strchr(filter, '='))) {
  361. /* ensure we at least have a name */
  362. if(args == filter)
  363. goto fail;
  364. *args ++ = 0;
  365. }
  366. av_log(NULL, AV_LOG_INFO, "creating filter \"%s\" with args \"%s\"\n",
  367. name, args ? args : "(none)");
  368. if((filterdef = avfilter_get_by_name(name)) &&
  369. (ret = avfilter_open(filterdef, NULL))) {
  370. if(avfilter_init_filter(ret, args, opaque)) {
  371. av_log(NULL, AV_LOG_ERROR, "error initializing filter!\n");
  372. avfilter_destroy(ret);
  373. goto fail;
  374. }
  375. } else av_log(NULL, AV_LOG_ERROR, "error creating filter!\n");
  376. av_free(filter);
  377. return ret;
  378. fail:
  379. av_free(filter);
  380. return NULL;
  381. }
  382. static int graph_load_chain(AVFilterContext *graphctx,
  383. unsigned count, char **filter_list, void **opaque,
  384. AVFilterContext **first, AVFilterContext **last)
  385. {
  386. unsigned i;
  387. AVFilterContext *filters[2] = {NULL,NULL};
  388. for(i = 0; i < count; i ++) {
  389. void *op;
  390. if(opaque) op = opaque[i];
  391. else op = NULL;
  392. if(!(filters[1] = create_filter_with_args(filter_list[i], op)))
  393. goto fail;
  394. if(i == 0) {
  395. if(first) *first = filters[1];
  396. } else {
  397. if(avfilter_link(filters[0], 0, filters[1], 0)) {
  398. av_log(NULL, AV_LOG_ERROR, "error linking filters!\n");
  399. goto fail;
  400. }
  401. }
  402. avfilter_graph_add_filter(graphctx, filters[1]);
  403. if(i == 0 && filters[1]->input_count > 0)
  404. add_graph_input(graphctx, filters[1], 0, "default");
  405. filters[0] = filters[1];
  406. }
  407. if(filters[1]->output_count > 0)
  408. add_graph_output(graphctx, filters[1], 0, "default");
  409. if(last) *last = filters[1];
  410. return 0;
  411. fail:
  412. uninit(graphctx);
  413. if(first) *first = NULL;
  414. if(last) *last = NULL;
  415. return -1;
  416. }
  417. static int graph_load_chain_from_string(AVFilterContext *ctx, const char *str,
  418. AVFilterContext **first,
  419. AVFilterContext **last)
  420. {
  421. int count, ret = 0;
  422. char **strings;
  423. char *filt;
  424. strings = av_malloc(sizeof(char *));
  425. strings[0] = av_strdup(str);
  426. filt = strchr(strings[0], ',');
  427. for(count = 1; filt; count ++) {
  428. if(filt == strings[count-1]) {
  429. ret = -1;
  430. goto done;
  431. }
  432. strings = av_realloc(strings, sizeof(char *) * (count+1));
  433. strings[count] = filt + 1;
  434. *filt = '\0';
  435. filt = strchr(strings[count], ',');
  436. }
  437. ret = graph_load_chain(ctx, count, strings, NULL, first, last);
  438. done:
  439. av_free(strings[0]);
  440. av_free(strings);
  441. return ret;
  442. }
  443. static int init(AVFilterContext *ctx, const char *args, void *opaque)
  444. {
  445. GraphContext *gctx = ctx->priv;
  446. if(!args)
  447. return 0;
  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. return graph_load_chain_from_string(ctx, args, NULL, NULL);
  453. fail:
  454. avfilter_destroy(gctx->link_filter);
  455. return -1;
  456. }
  457. AVFilter vf_graph =
  458. {
  459. .name = "graph",
  460. .author = "Bobby Bingham",
  461. .priv_size = sizeof(GraphContext),
  462. .init = init,
  463. .uninit = uninit,
  464. .inputs = (AVFilterPad[]) {{ .name = NULL, }},
  465. .outputs = (AVFilterPad[]) {{ .name = NULL, }},
  466. };
  467. static int graph_load_from_desc(AVFilterContext *ctx, AVFilterGraphDesc *desc)
  468. {
  469. AVFilterGraphDescFilter *curfilt;
  470. AVFilterGraphDescLink *curlink;
  471. AVFilterGraphDescExport *curpad;
  472. AVFilterContext *filt, *filtb;
  473. AVFilter *filterdef;
  474. /* create all filters */
  475. for(curfilt = desc->filters; curfilt; curfilt = curfilt->next) {
  476. if(!(filterdef = avfilter_get_by_name(curfilt->filter)) ||
  477. !(filt = avfilter_open(filterdef, curfilt->name))) {
  478. av_log(ctx, AV_LOG_ERROR, "error creating filter\n");
  479. goto fail;
  480. }
  481. avfilter_graph_add_filter(ctx, filt);
  482. if(avfilter_init_filter(filt, curfilt->args, NULL)) {
  483. av_log(ctx, AV_LOG_ERROR, "error initializing filter\n");
  484. goto fail;
  485. }
  486. }
  487. /* create all links */
  488. for(curlink = desc->links; curlink; curlink = curlink->next) {
  489. if(!(filt = avfilter_graph_get_filter(ctx, curlink->src))) {
  490. av_log(ctx, AV_LOG_ERROR, "link source does not exist in graph\n");
  491. goto fail;
  492. }
  493. if(!(filtb = avfilter_graph_get_filter(ctx, curlink->dst))) {
  494. av_log(ctx, AV_LOG_ERROR, "link destination does not exist in graph\n");
  495. goto fail;
  496. }
  497. if(avfilter_link(filt, curlink->srcpad, filtb, curlink->dstpad)) {
  498. av_log(ctx, AV_LOG_ERROR, "cannot create link between source and destination filters\n");
  499. goto fail;
  500. }
  501. }
  502. /* export all input pads */
  503. for(curpad = desc->inputs; curpad; curpad = curpad->next) {
  504. if(!(filt = avfilter_graph_get_filter(ctx, curpad->filter))) {
  505. av_log(ctx, AV_LOG_ERROR, "filter owning exported pad does not exist\n");
  506. goto fail;
  507. }
  508. add_graph_input(ctx, filt, curpad->pad, curpad->name);
  509. }
  510. /* export all output pads */
  511. for(curpad = desc->outputs; curpad; curpad = curpad->next) {
  512. if(!(filt = avfilter_graph_get_filter(ctx, curpad->filter))) {
  513. av_log(ctx, AV_LOG_ERROR, "filter owning exported pad does not exist\n");
  514. goto fail;
  515. }
  516. add_graph_output(ctx, filt, curpad->pad, curpad->name);
  517. }
  518. return 0;
  519. fail:
  520. uninit(ctx);
  521. return -1;
  522. }
  523. static int init_desc(AVFilterContext *ctx, const char *args, void *opaque)
  524. {
  525. GraphContext *gctx = ctx->priv;
  526. if(!opaque)
  527. return -1;
  528. if(!(gctx->link_filter = avfilter_open(&vf_graph_dummy, NULL)))
  529. return -1;
  530. if(avfilter_init_filter(gctx->link_filter, NULL, ctx))
  531. goto fail;
  532. return graph_load_from_desc(ctx, opaque);
  533. fail:
  534. avfilter_destroy(gctx->link_filter);
  535. return -1;
  536. }
  537. AVFilter vf_graphdesc =
  538. {
  539. .name = "graph_desc",
  540. .author = "Bobby Bingham",
  541. .priv_size = sizeof(GraphContext),
  542. .init = init_desc,
  543. .uninit = uninit,
  544. .inputs = (AVFilterPad[]) {{ .name = NULL, }},
  545. .outputs = (AVFilterPad[]) {{ .name = NULL, }},
  546. };
  547. static int init_file(AVFilterContext *ctx, const char *args, void *opaque)
  548. {
  549. AVFilterGraphDesc *desc;
  550. int ret;
  551. if(!args)
  552. return -1;
  553. if(!(desc = avfilter_graph_load_desc(args)))
  554. return -1;
  555. ret = init_desc(ctx, NULL, desc);
  556. avfilter_graph_free_desc(desc);
  557. return ret;
  558. }
  559. AVFilter vf_graphfile =
  560. {
  561. .name = "graph_file",
  562. .author = "Bobby Bingham",
  563. .priv_size = sizeof(GraphContext),
  564. .init = init_file,
  565. .uninit = uninit,
  566. .inputs = (AVFilterPad[]) {{ .name = NULL, }},
  567. .outputs = (AVFilterPad[]) {{ .name = NULL, }},
  568. };