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.

152 lines
4.7KB

  1. /*
  2. * filter graphs
  3. * copyright (c) 2008 Vitor Sessak
  4. * copyright (c) 2007 Bobby Bingham
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #include <ctype.h>
  23. #include <string.h>
  24. #include "avfilter.h"
  25. #include "avfiltergraph.h"
  26. void avfilter_destroy_graph(AVFilterGraph *graph)
  27. {
  28. for(; graph->filter_count > 0; graph->filter_count --)
  29. avfilter_destroy(graph->filters[graph->filter_count - 1]);
  30. av_freep(&graph->filters);
  31. }
  32. /**
  33. * @todo insert in sorted order
  34. */
  35. void avfilter_graph_add_filter(AVFilterGraph *graph, AVFilterContext *filter)
  36. {
  37. graph->filters = av_realloc(graph->filters,
  38. sizeof(AVFilterContext*) * ++graph->filter_count);
  39. graph->filters[graph->filter_count - 1] = filter;
  40. }
  41. /*
  42. * @todo search intelligently, once we insert in order
  43. */
  44. AVFilterContext *avfilter_graph_get_filter(AVFilterGraph *graph, char *name)
  45. {
  46. int i;
  47. if(!name)
  48. return NULL;
  49. for(i = 0; i < graph->filter_count; i ++)
  50. if(graph->filters[i]->name && !strcmp(name, graph->filters[i]->name))
  51. return graph->filters[i];
  52. return NULL;
  53. }
  54. static int query_formats(AVFilterGraph *graph)
  55. {
  56. int i, j;
  57. /* ask all the sub-filters for their supported colorspaces */
  58. for(i = 0; i < graph->filter_count; i ++) {
  59. if(graph->filters[i]->filter->query_formats)
  60. graph->filters[i]->filter->query_formats(graph->filters[i]);
  61. else
  62. avfilter_default_query_formats(graph->filters[i]);
  63. }
  64. /* go through and merge as many format lists as possible */
  65. for(i = 0; i < graph->filter_count; i ++) {
  66. AVFilterContext *filter = graph->filters[i];
  67. for(j = 0; j < filter->input_count; j ++) {
  68. AVFilterLink *link;
  69. if(!(link = filter->inputs[j]))
  70. continue;
  71. if(link->in_formats != link->out_formats) {
  72. if(!avfilter_merge_formats(link->in_formats,
  73. link->out_formats)) {
  74. /* couldn't merge format lists. auto-insert scale filter */
  75. AVFilterContext *scale;
  76. if(!(scale =
  77. avfilter_open(avfilter_get_by_name("scale"), NULL)))
  78. return -1;
  79. if(scale->filter->init(scale, NULL, NULL) ||
  80. avfilter_insert_filter(link, scale, 0, 0)) {
  81. avfilter_destroy(scale);
  82. return -1;
  83. }
  84. avfilter_graph_add_filter(graph, scale);
  85. scale->filter->query_formats(scale);
  86. if(!avfilter_merge_formats(scale-> inputs[0]->in_formats,
  87. scale-> inputs[0]->out_formats) ||
  88. !avfilter_merge_formats(scale->outputs[0]->in_formats,
  89. scale->outputs[0]->out_formats))
  90. return -1;
  91. }
  92. }
  93. }
  94. }
  95. return 0;
  96. }
  97. static void pick_format(AVFilterLink *link)
  98. {
  99. if(!link || !link->in_formats)
  100. return;
  101. link->in_formats->format_count = 1;
  102. link->format = link->in_formats->formats[0];
  103. avfilter_formats_unref(&link->in_formats);
  104. avfilter_formats_unref(&link->out_formats);
  105. }
  106. static void pick_formats(AVFilterGraph *graph)
  107. {
  108. int i, j;
  109. for(i = 0; i < graph->filter_count; i ++) {
  110. AVFilterContext *filter = graph->filters[i];
  111. for(j = 0; j < filter->input_count; j ++)
  112. pick_format(filter->inputs[j]);
  113. for(j = 0; j < filter->output_count; j ++)
  114. pick_format(filter->outputs[j]);
  115. }
  116. }
  117. int avfilter_graph_config_formats(AVFilterGraph *graph)
  118. {
  119. /* find supported formats from sub-filters, and merge along links */
  120. if(query_formats(graph))
  121. return -1;
  122. /* Once everything is merged, it's possible that we'll still have
  123. * multiple valid colorspace choices. We pick the first one. */
  124. pick_formats(graph);
  125. return 0;
  126. }