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.

697 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. AVFilterContext *ret;
  356. char *filter = av_strdup(filt); /* copy - don't mangle the input string */
  357. char *name, *args;
  358. name = filter;
  359. if((args = strchr(filter, '='))) {
  360. /* ensure we at least have a name */
  361. if(args == filter)
  362. goto fail;
  363. *args ++ = 0;
  364. }
  365. av_log(NULL, AV_LOG_INFO, "creating filter \"%s\" with args \"%s\"\n",
  366. name, args ? args : "(none)");
  367. if((ret = avfilter_create_by_name(name, NULL))) {
  368. if(avfilter_init_filter(ret, args, opaque)) {
  369. av_log(NULL, AV_LOG_ERROR, "error initializing filter!\n");
  370. avfilter_destroy(ret);
  371. goto fail;
  372. }
  373. } else av_log(NULL, AV_LOG_ERROR, "error creating filter!\n");
  374. av_free(filter);
  375. return ret;
  376. fail:
  377. av_free(filter);
  378. return NULL;
  379. }
  380. static int graph_load_chain(AVFilterContext *graphctx,
  381. unsigned count, char **filter_list, void **opaque,
  382. AVFilterContext **first, AVFilterContext **last)
  383. {
  384. unsigned i;
  385. AVFilterContext *filters[2] = {NULL,NULL};
  386. for(i = 0; i < count; i ++) {
  387. void *op;
  388. if(opaque) op = opaque[i];
  389. else op = NULL;
  390. if(!(filters[1] = create_filter_with_args(filter_list[i], op)))
  391. goto fail;
  392. if(i == 0) {
  393. if(first) *first = filters[1];
  394. } else {
  395. if(avfilter_link(filters[0], 0, filters[1], 0)) {
  396. av_log(NULL, AV_LOG_ERROR, "error linking filters!\n");
  397. goto fail;
  398. }
  399. }
  400. avfilter_graph_add_filter(graphctx, filters[1]);
  401. if(i == 0 && filters[1]->input_count > 0)
  402. add_graph_input(graphctx, filters[1], 0, "default");
  403. filters[0] = filters[1];
  404. }
  405. if(filters[1]->output_count > 0)
  406. add_graph_output(graphctx, filters[1], 0, "default");
  407. if(last) *last = filters[1];
  408. return 0;
  409. fail:
  410. uninit(graphctx);
  411. if(first) *first = NULL;
  412. if(last) *last = NULL;
  413. return -1;
  414. }
  415. static int graph_load_chain_from_string(AVFilterContext *ctx, const char *str,
  416. AVFilterContext **first,
  417. AVFilterContext **last)
  418. {
  419. int count, ret = 0;
  420. char **strings;
  421. char *filt;
  422. strings = av_malloc(sizeof(char *));
  423. strings[0] = av_strdup(str);
  424. filt = strchr(strings[0], ',');
  425. for(count = 1; filt; count ++) {
  426. if(filt == strings[count-1]) {
  427. ret = -1;
  428. goto done;
  429. }
  430. strings = av_realloc(strings, sizeof(char *) * (count+1));
  431. strings[count] = filt + 1;
  432. *filt = '\0';
  433. filt = strchr(strings[count], ',');
  434. }
  435. ret = graph_load_chain(ctx, count, strings, NULL, first, last);
  436. done:
  437. av_free(strings[0]);
  438. av_free(strings);
  439. return ret;
  440. }
  441. static int init(AVFilterContext *ctx, const char *args, void *opaque)
  442. {
  443. GraphContext *gctx = ctx->priv;
  444. if(!args)
  445. return 0;
  446. if(!(gctx->link_filter = avfilter_create(&vf_graph_dummy, NULL)))
  447. return -1;
  448. if(avfilter_init_filter(gctx->link_filter, NULL, ctx))
  449. goto fail;
  450. return graph_load_chain_from_string(ctx, args, NULL, NULL);
  451. fail:
  452. avfilter_destroy(gctx->link_filter);
  453. return -1;
  454. }
  455. AVFilter vf_graph =
  456. {
  457. .name = "graph",
  458. .author = "Bobby Bingham",
  459. .priv_size = sizeof(GraphContext),
  460. .init = init,
  461. .uninit = uninit,
  462. .inputs = (AVFilterPad[]) {{ .name = NULL, }},
  463. .outputs = (AVFilterPad[]) {{ .name = NULL, }},
  464. };
  465. static int graph_load_from_desc(AVFilterContext *ctx, AVFilterGraphDesc *desc)
  466. {
  467. AVFilterGraphDescFilter *curfilt;
  468. AVFilterGraphDescLink *curlink;
  469. AVFilterGraphDescExport *curpad;
  470. AVFilterContext *filt, *filtb;
  471. /* create all filters */
  472. for(curfilt = desc->filters; curfilt; curfilt = curfilt->next) {
  473. if(!(filt = avfilter_create_by_name(curfilt->filter, curfilt->name))) {
  474. av_log(ctx, AV_LOG_ERROR, "error creating filter\n");
  475. goto fail;
  476. }
  477. avfilter_graph_add_filter(ctx, filt);
  478. if(avfilter_init_filter(filt, curfilt->args, NULL)) {
  479. av_log(ctx, AV_LOG_ERROR, "error initializing filter\n");
  480. goto fail;
  481. }
  482. }
  483. /* create all links */
  484. for(curlink = desc->links; curlink; curlink = curlink->next) {
  485. if(!(filt = avfilter_graph_get_filter(ctx, curlink->src))) {
  486. av_log(ctx, AV_LOG_ERROR, "link source does not exist in graph\n");
  487. goto fail;
  488. }
  489. if(!(filtb = avfilter_graph_get_filter(ctx, curlink->dst))) {
  490. av_log(ctx, AV_LOG_ERROR, "link destination does not exist in graph\n");
  491. goto fail;
  492. }
  493. if(avfilter_link(filt, curlink->srcpad, filtb, curlink->dstpad)) {
  494. av_log(ctx, AV_LOG_ERROR, "cannot create link between source and destination filters\n");
  495. goto fail;
  496. }
  497. }
  498. /* export all input pads */
  499. for(curpad = desc->inputs; curpad; curpad = curpad->next) {
  500. if(!(filt = avfilter_graph_get_filter(ctx, curpad->filter))) {
  501. av_log(ctx, AV_LOG_ERROR, "filter owning exported pad does not exist\n");
  502. goto fail;
  503. }
  504. add_graph_input(ctx, filt, curpad->pad, curpad->name);
  505. }
  506. /* export all output pads */
  507. for(curpad = desc->outputs; curpad; curpad = curpad->next) {
  508. if(!(filt = avfilter_graph_get_filter(ctx, curpad->filter))) {
  509. av_log(ctx, AV_LOG_ERROR, "filter owning exported pad does not exist\n");
  510. goto fail;
  511. }
  512. add_graph_output(ctx, filt, curpad->pad, curpad->name);
  513. }
  514. return 0;
  515. fail:
  516. uninit(ctx);
  517. return -1;
  518. }
  519. static int init_desc(AVFilterContext *ctx, const char *args, void *opaque)
  520. {
  521. GraphContext *gctx = ctx->priv;
  522. if(!opaque)
  523. return -1;
  524. if(!(gctx->link_filter = avfilter_create(&vf_graph_dummy, NULL)))
  525. return -1;
  526. if(avfilter_init_filter(gctx->link_filter, NULL, ctx))
  527. goto fail;
  528. return graph_load_from_desc(ctx, opaque);
  529. fail:
  530. avfilter_destroy(gctx->link_filter);
  531. return -1;
  532. }
  533. AVFilter vf_graphdesc =
  534. {
  535. .name = "graph_desc",
  536. .author = "Bobby Bingham",
  537. .priv_size = sizeof(GraphContext),
  538. .init = init_desc,
  539. .uninit = uninit,
  540. .inputs = (AVFilterPad[]) {{ .name = NULL, }},
  541. .outputs = (AVFilterPad[]) {{ .name = NULL, }},
  542. };
  543. static int init_file(AVFilterContext *ctx, const char *args, void *opaque)
  544. {
  545. AVFilterGraphDesc *desc;
  546. int ret;
  547. if(!args)
  548. return -1;
  549. if(!(desc = avfilter_graph_load_desc(args)))
  550. return -1;
  551. ret = init_desc(ctx, NULL, desc);
  552. avfilter_graph_free_desc(desc);
  553. return ret;
  554. }
  555. AVFilter vf_graphfile =
  556. {
  557. .name = "graph_file",
  558. .author = "Bobby Bingham",
  559. .priv_size = sizeof(GraphContext),
  560. .init = init_file,
  561. .uninit = uninit,
  562. .inputs = (AVFilterPad[]) {{ .name = NULL, }},
  563. .outputs = (AVFilterPad[]) {{ .name = NULL, }},
  564. };