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.

252 lines
7.9KB

  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. /**
  24. * For use in av_log
  25. */
  26. static const char *log_name(void *p)
  27. {
  28. return "Filter parser";
  29. }
  30. static const AVClass filter_parser_class = {
  31. "Filter parser",
  32. log_name
  33. };
  34. static const AVClass *log_ctx = &filter_parser_class;
  35. static void uninit(GraphContext *graph)
  36. {
  37. for(; graph->filter_count > 0; graph->filter_count --)
  38. avfilter_destroy(graph->filters[graph->filter_count - 1]);
  39. av_freep(&graph->filters);
  40. }
  41. /* TODO: insert in sorted order */
  42. void avfilter_graph_add_filter(GraphContext *graph, AVFilterContext *filter)
  43. {
  44. graph->filters = av_realloc(graph->filters,
  45. sizeof(AVFilterContext*) * ++graph->filter_count);
  46. graph->filters[graph->filter_count - 1] = filter;
  47. }
  48. /* search intelligently, once we insert in order */
  49. AVFilterContext *avfilter_graph_get_filter(GraphContext *graph, char *name)
  50. {
  51. int i;
  52. if(!name)
  53. return NULL;
  54. for(i = 0; i < graph->filter_count; i ++)
  55. if(graph->filters[i]->name && !strcmp(name, graph->filters[i]->name))
  56. return graph->filters[i];
  57. return NULL;
  58. }
  59. static int query_formats(GraphContext *graph)
  60. {
  61. int i, j;
  62. /* ask all the sub-filters for their supported colorspaces */
  63. for(i = 0; i < graph->filter_count; i ++) {
  64. if(graph->filters[i]->filter->query_formats)
  65. graph->filters[i]->filter->query_formats(graph->filters[i]);
  66. else
  67. avfilter_default_query_formats(graph->filters[i]);
  68. }
  69. /* go through and merge as many format lists as possible */
  70. for(i = 0; i < graph->filter_count; i ++) {
  71. AVFilterContext *filter = graph->filters[i];
  72. for(j = 0; j < filter->input_count; j ++) {
  73. AVFilterLink *link;
  74. if(!(link = filter->inputs[j]))
  75. continue;
  76. if(link->in_formats != link->out_formats) {
  77. if(!avfilter_merge_formats(link->in_formats,
  78. link->out_formats)) {
  79. /* couldn't merge format lists. auto-insert scale filter */
  80. AVFilterContext *scale;
  81. if(!(scale =
  82. avfilter_open(avfilter_get_by_name("scale"), NULL)))
  83. return -1;
  84. if(scale->filter->init(scale, NULL, NULL) ||
  85. avfilter_insert_filter(link, scale, 0, 0)) {
  86. avfilter_destroy(scale);
  87. return -1;
  88. }
  89. avfilter_graph_add_filter(graph, scale);
  90. scale->filter->query_formats(scale);
  91. if(!avfilter_merge_formats(scale-> inputs[0]->in_formats,
  92. scale-> inputs[0]->out_formats) ||
  93. !avfilter_merge_formats(scale->outputs[0]->in_formats,
  94. scale->outputs[0]->out_formats))
  95. return -1;
  96. }
  97. }
  98. }
  99. }
  100. return 0;
  101. }
  102. static void pick_format(AVFilterLink *link)
  103. {
  104. if(!link || !link->in_formats)
  105. return;
  106. link->in_formats->format_count = 1;
  107. link->format = link->in_formats->formats[0];
  108. avfilter_formats_unref(&link->in_formats);
  109. avfilter_formats_unref(&link->out_formats);
  110. }
  111. static void pick_formats(GraphContext *graph)
  112. {
  113. int i, j;
  114. for(i = 0; i < graph->filter_count; i ++) {
  115. AVFilterContext *filter = graph->filters[i];
  116. for(j = 0; j < filter->input_count; j ++)
  117. pick_format(filter->inputs[j]);
  118. for(j = 0; j < filter->output_count; j ++)
  119. pick_format(filter->outputs[j]);
  120. }
  121. }
  122. int avfilter_graph_config_formats(GraphContext *graph)
  123. {
  124. /* find supported formats from sub-filters, and merge along links */
  125. if(query_formats(graph))
  126. return -1;
  127. /* Once everything is merged, it's possible that we'll still have
  128. * multiple valid colorspace choices. We pick the first one. */
  129. pick_formats(graph);
  130. return 0;
  131. }
  132. static int graph_load_from_desc2(GraphContext *ctx, AVFilterGraphDesc *desc)
  133. {
  134. AVFilterGraphDescFilter *curfilt;
  135. AVFilterGraphDescLink *curlink;
  136. AVFilterContext *filt, *filtb;
  137. AVFilter *filterdef;
  138. char tmp[20];
  139. /* create all filters */
  140. for(curfilt = desc->filters; curfilt; curfilt = curfilt->next) {
  141. snprintf(tmp, 20, "%d", curfilt->index);
  142. if(!(filterdef = avfilter_get_by_name(curfilt->filter)) ||
  143. !(filt = avfilter_open(filterdef, tmp))) {
  144. av_log(&log_ctx, AV_LOG_ERROR,
  145. "error creating filter '%s'\n", curfilt->filter);
  146. goto fail;
  147. }
  148. avfilter_graph_add_filter(ctx, filt);
  149. if(avfilter_init_filter(filt, curfilt->args, NULL)) {
  150. av_log(&log_ctx, AV_LOG_ERROR,
  151. "error initializing filter '%s'\n", curfilt->filter);
  152. goto fail;
  153. }
  154. }
  155. /* create all links */
  156. for(curlink = desc->links; curlink; curlink = curlink->next) {
  157. snprintf(tmp, 20, "%d", curlink->src);
  158. if(!(filt = avfilter_graph_get_filter(ctx, tmp))) {
  159. av_log(&log_ctx, AV_LOG_ERROR, "link source does not exist in graph\n");
  160. goto fail;
  161. }
  162. snprintf(tmp, 20, "%d", curlink->dst);
  163. if(!(filtb = avfilter_graph_get_filter(ctx, tmp))) {
  164. av_log(&log_ctx, AV_LOG_ERROR, "link destination does not exist in graph\n");
  165. goto fail;
  166. }
  167. if(avfilter_link(filt, curlink->srcpad, filtb, curlink->dstpad)) {
  168. av_log(&log_ctx, AV_LOG_ERROR, "cannot create link between source and destination filters\n");
  169. goto fail;
  170. }
  171. }
  172. return 0;
  173. fail:
  174. uninit(ctx);
  175. return -1;
  176. }
  177. int graph_load_from_desc3(GraphContext *graph, AVFilterGraphDesc *desc, AVFilterContext *in, int inpad, AVFilterContext *out, int outpad)
  178. {
  179. AVFilterGraphDescExport *curpad;
  180. char tmp[20];
  181. AVFilterContext *filt;
  182. if (graph_load_from_desc2(graph, desc) < 0)
  183. goto fail;
  184. /* export all input pads */
  185. for(curpad = desc->inputs; curpad; curpad = curpad->next) {
  186. snprintf(tmp, 20, "%d", curpad->filter);
  187. if(!(filt = avfilter_graph_get_filter(graph, tmp))) {
  188. av_log(&log_ctx, AV_LOG_ERROR, "filter owning exported pad does not exist\n");
  189. goto fail;
  190. }
  191. if(avfilter_link(in, inpad, filt, curpad->pad)) {
  192. av_log(&log_ctx, AV_LOG_ERROR, "cannot create link between source and destination filters\n");
  193. goto fail;
  194. }
  195. }
  196. /* export all output pads */
  197. for(curpad = desc->outputs; curpad; curpad = curpad->next) {
  198. snprintf(tmp, 20, "%d", curpad->filter);
  199. if(!(filt = avfilter_graph_get_filter(graph, tmp))) {
  200. av_log(&log_ctx, AV_LOG_ERROR, "filter owning exported pad does not exist\n");
  201. goto fail;
  202. }
  203. if(avfilter_link(filt, curpad->pad, out, outpad)) {
  204. av_log(&log_ctx, AV_LOG_ERROR, "cannot create link between source and destination filters\n");
  205. goto fail;
  206. }
  207. }
  208. return 0;
  209. fail:
  210. uninit(graph);
  211. return -1;
  212. }