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.

370 lines
11KB

  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 "avfilter.h"
  22. #include "avfiltergraph.h"
  23. typedef struct AVFilterGraph {
  24. unsigned filter_count;
  25. AVFilterContext **filters;
  26. /** fake filters to handle links to internal filters */
  27. AVFilterContext *link_filter_in;
  28. AVFilterContext *link_filter_out;
  29. } GraphContext;
  30. typedef struct {
  31. AVFilterContext *graph;
  32. } GraphLinkContext;
  33. static int link_init(AVFilterContext *ctx, const char *args, void *opaque)
  34. {
  35. GraphLinkContext *linkctx = ctx->priv;
  36. linkctx->graph = opaque;
  37. return !opaque;
  38. }
  39. /**
  40. * Given the link between the dummy filter and an internal filter whose input
  41. * is being exported outside the graph, this returns the externally visible
  42. * link.
  43. */
  44. static inline AVFilterLink *get_extern_input_link(AVFilterLink *link)
  45. {
  46. GraphLinkContext *lctx = link->src->priv;
  47. return lctx->graph->inputs[link->srcpad];
  48. }
  49. /**
  50. * Given the link between the dummy filter and an internal filter whose output
  51. * is being exported outside the graph, this returns the externally visible
  52. * link.
  53. */
  54. static inline AVFilterLink *get_extern_output_link(AVFilterLink *link)
  55. {
  56. GraphLinkContext *lctx = link->dst->priv;
  57. return lctx->graph->outputs[link->dstpad];
  58. }
  59. /** dummy filter used to help export filters pads outside the graph */
  60. static AVFilter vf_graph_dummy =
  61. {
  62. .name = "graph_dummy",
  63. .priv_size = sizeof(GraphLinkContext),
  64. .init = link_init,
  65. .inputs = (AVFilterPad[]) {{ .name = NULL, }},
  66. .outputs = (AVFilterPad[]) {{ .name = NULL, }},
  67. };
  68. static void uninit(AVFilterContext *ctx)
  69. {
  70. GraphContext *graph = ctx->priv;
  71. if(graph->link_filter_in) {
  72. avfilter_destroy(graph->link_filter_in);
  73. graph->link_filter_in = NULL;
  74. }
  75. if(graph->link_filter_out) {
  76. avfilter_destroy(graph->link_filter_out);
  77. graph->link_filter_out = NULL;
  78. }
  79. for(; graph->filter_count > 0; graph->filter_count --)
  80. avfilter_destroy(graph->filters[graph->filter_count - 1]);
  81. av_freep(&graph->filters);
  82. }
  83. /* TODO: insert in sorted order */
  84. void avfilter_graph_add_filter(AVFilterContext *graphctx, AVFilterContext *filter)
  85. {
  86. GraphContext *graph = graphctx->priv;
  87. graph->filters = av_realloc(graph->filters,
  88. sizeof(AVFilterContext*) * ++graph->filter_count);
  89. graph->filters[graph->filter_count - 1] = filter;
  90. }
  91. /* search intelligently, once we insert in order */
  92. AVFilterContext *avfilter_graph_get_filter(AVFilterContext *ctx, char *name)
  93. {
  94. GraphContext *graph = ctx->priv;
  95. int i;
  96. if(!name)
  97. return NULL;
  98. for(i = 0; i < graph->filter_count; i ++)
  99. if(graph->filters[i]->name && !strcmp(name, graph->filters[i]->name))
  100. return graph->filters[i];
  101. return NULL;
  102. }
  103. static int query_formats(AVFilterContext *graphctx)
  104. {
  105. GraphContext *graph = graphctx->priv;
  106. AVFilterContext *linkfiltin = graph->link_filter_in;
  107. AVFilterContext *linkfiltout = graph->link_filter_out;
  108. int i, j;
  109. /* ask all the sub-filters for their supported colorspaces */
  110. for(i = 0; i < graph->filter_count; i ++) {
  111. if(graph->filters[i]->filter->query_formats)
  112. graph->filters[i]->filter->query_formats(graph->filters[i]);
  113. else
  114. avfilter_default_query_formats(graph->filters[i]);
  115. }
  116. /* use these formats on our exported links */
  117. for(i = 0; i < linkfiltout->input_count; i ++) {
  118. avfilter_formats_ref( linkfiltout->inputs[i]->in_formats,
  119. &linkfiltout->inputs[i]->out_formats);
  120. if(graphctx->outputs[i])
  121. avfilter_formats_ref(linkfiltout->inputs[i]->in_formats,
  122. &graphctx->outputs[i]->in_formats);
  123. }
  124. for(i = 0; i < linkfiltin->output_count; i ++) {
  125. avfilter_formats_ref( linkfiltin->outputs[i]->out_formats,
  126. &linkfiltin->outputs[i]->in_formats);
  127. if(graphctx->inputs[i])
  128. avfilter_formats_ref(linkfiltin->outputs[i]->out_formats,
  129. &graphctx-> inputs[i]->out_formats);
  130. }
  131. /* go through and merge as many format lists as possible */
  132. for(i = 0; i < graph->filter_count; i ++) {
  133. AVFilterContext *filter = graph->filters[i];
  134. for(j = 0; j < filter->input_count; j ++) {
  135. AVFilterLink *link;
  136. if(!(link = filter->inputs[j]))
  137. continue;
  138. if(link->in_formats != link->out_formats) {
  139. if(!avfilter_merge_formats(link->in_formats,
  140. link->out_formats)) {
  141. /* couldn't merge format lists. auto-insert scale filter */
  142. AVFilterContext *scale;
  143. if(!(scale =
  144. avfilter_open(avfilter_get_by_name("scale"), NULL)))
  145. return -1;
  146. if(scale->filter->init(scale, NULL, NULL) ||
  147. avfilter_insert_filter(link, scale, 0, 0)) {
  148. avfilter_destroy(scale);
  149. return -1;
  150. }
  151. avfilter_graph_add_filter(graphctx, scale);
  152. scale->filter->query_formats(scale);
  153. if(!avfilter_merge_formats(scale-> inputs[0]->in_formats,
  154. scale-> inputs[0]->out_formats) ||
  155. !avfilter_merge_formats(scale->outputs[0]->in_formats,
  156. scale->outputs[0]->out_formats))
  157. return -1;
  158. }
  159. }
  160. }
  161. }
  162. return 0;
  163. }
  164. static void pick_format(AVFilterLink *link)
  165. {
  166. if(!link || !link->in_formats)
  167. return;
  168. link->in_formats->format_count = 1;
  169. link->format = link->in_formats->formats[0];
  170. avfilter_formats_unref(&link->in_formats);
  171. avfilter_formats_unref(&link->out_formats);
  172. }
  173. static void pick_formats(GraphContext *graph)
  174. {
  175. int i, j;
  176. for(i = 0; i < graph->filter_count; i ++) {
  177. AVFilterContext *filter = graph->filters[i];
  178. if(filter->filter == &avfilter_vf_graph)
  179. pick_formats(filter->priv);
  180. for(j = 0; j < filter->input_count; j ++)
  181. pick_format(filter->inputs[j]);
  182. for(j = 0; j < filter->output_count; j ++)
  183. pick_format(filter->outputs[j]);
  184. }
  185. }
  186. int avfilter_graph_config_formats(AVFilterContext *graphctx)
  187. {
  188. GraphContext *graph = graphctx->priv;
  189. /* find supported formats from sub-filters, and merge along links */
  190. if(query_formats(graphctx))
  191. return -1;
  192. /* Once everything is merged, it's possible that we'll still have
  193. * multiple valid colorspace choices. We pick the first one. */
  194. pick_formats(graph);
  195. return 0;
  196. }
  197. static int graph_load_from_desc2(AVFilterContext *ctx, AVFilterGraphDesc *desc)
  198. {
  199. AVFilterGraphDescFilter *curfilt;
  200. AVFilterGraphDescLink *curlink;
  201. AVFilterContext *filt, *filtb;
  202. AVFilter *filterdef;
  203. char tmp[20];
  204. /* create all filters */
  205. for(curfilt = desc->filters; curfilt; curfilt = curfilt->next) {
  206. snprintf(tmp, 20, "%d", curfilt->index);
  207. if(!(filterdef = avfilter_get_by_name(curfilt->filter)) ||
  208. !(filt = avfilter_open(filterdef, tmp))) {
  209. av_log(ctx, AV_LOG_ERROR,
  210. "error creating filter '%s'\n", curfilt->filter);
  211. goto fail;
  212. }
  213. avfilter_graph_add_filter(ctx, filt);
  214. if(avfilter_init_filter(filt, curfilt->args, NULL)) {
  215. av_log(ctx, AV_LOG_ERROR,
  216. "error initializing filter '%s'\n", curfilt->filter);
  217. goto fail;
  218. }
  219. }
  220. /* create all links */
  221. for(curlink = desc->links; curlink; curlink = curlink->next) {
  222. snprintf(tmp, 20, "%d", curlink->src);
  223. if(!(filt = avfilter_graph_get_filter(ctx, tmp))) {
  224. av_log(ctx, AV_LOG_ERROR, "link source does not exist in graph\n");
  225. goto fail;
  226. }
  227. snprintf(tmp, 20, "%d", curlink->dst);
  228. if(!(filtb = avfilter_graph_get_filter(ctx, tmp))) {
  229. av_log(ctx, AV_LOG_ERROR, "link destination does not exist in graph\n");
  230. goto fail;
  231. }
  232. if(avfilter_link(filt, curlink->srcpad, filtb, curlink->dstpad)) {
  233. av_log(ctx, AV_LOG_ERROR, "cannot create link between source and destination filters\n");
  234. goto fail;
  235. }
  236. }
  237. return 0;
  238. fail:
  239. uninit(ctx);
  240. return -1;
  241. }
  242. int graph_load_from_desc3(AVFilterContext *ctx, AVFilterGraphDesc *desc, AVFilterContext *in, int inpad, AVFilterContext *out, int outpad)
  243. {
  244. AVFilterGraphDescExport *curpad;
  245. char tmp[20];
  246. AVFilterContext *filt;
  247. if (graph_load_from_desc2(ctx, desc) < 0)
  248. goto fail;
  249. /* export all input pads */
  250. for(curpad = desc->inputs; curpad; curpad = curpad->next) {
  251. snprintf(tmp, 20, "%d", curpad->filter);
  252. if(!(filt = avfilter_graph_get_filter(ctx, tmp))) {
  253. av_log(ctx, AV_LOG_ERROR, "filter owning exported pad does not exist\n");
  254. goto fail;
  255. }
  256. if(avfilter_link(in, inpad, filt, curpad->pad)) {
  257. av_log(ctx, AV_LOG_ERROR, "cannot create link between source and destination filters\n");
  258. goto fail;
  259. }
  260. }
  261. /* export all output pads */
  262. for(curpad = desc->outputs; curpad; curpad = curpad->next) {
  263. snprintf(tmp, 20, "%d", curpad->filter);
  264. if(!(filt = avfilter_graph_get_filter(ctx, tmp))) {
  265. av_log(ctx, AV_LOG_ERROR, "filter owning exported pad does not exist\n");
  266. goto fail;
  267. }
  268. if(avfilter_link(filt, curpad->pad, out, outpad)) {
  269. av_log(ctx, AV_LOG_ERROR, "cannot create link between source and destination filters\n");
  270. goto fail;
  271. }
  272. }
  273. return 0;
  274. fail:
  275. uninit(ctx);
  276. return -1;
  277. }
  278. static int init(AVFilterContext *ctx, const char *args, void *opaque)
  279. {
  280. GraphContext *gctx = ctx->priv;
  281. if(!(gctx->link_filter_in = avfilter_open(&vf_graph_dummy, NULL)))
  282. return -1;
  283. if(avfilter_init_filter(gctx->link_filter_in, NULL, ctx))
  284. goto fail;
  285. if(!(gctx->link_filter_out = avfilter_open(&vf_graph_dummy, NULL)))
  286. goto fail;
  287. if(avfilter_init_filter(gctx->link_filter_out, NULL, ctx))
  288. goto fail;
  289. return 0;
  290. fail:
  291. avfilter_destroy(gctx->link_filter_in);
  292. if(gctx->link_filter_out)
  293. avfilter_destroy(gctx->link_filter_out);
  294. return -1;
  295. }
  296. AVFilter avfilter_vf_graph =
  297. {
  298. .name = "graph",
  299. .priv_size = sizeof(GraphContext),
  300. .init = init,
  301. .uninit = uninit,
  302. .query_formats = query_formats,
  303. .inputs = (AVFilterPad[]) {{ .name = NULL, }},
  304. .outputs = (AVFilterPad[]) {{ .name = NULL, }},
  305. };