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.

139 lines
4.4KB

  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. void avfilter_graph_add_filter(AVFilterGraph *graph, AVFilterContext *filter)
  33. {
  34. graph->filters = av_realloc(graph->filters,
  35. sizeof(AVFilterContext*) * ++graph->filter_count);
  36. graph->filters[graph->filter_count - 1] = filter;
  37. }
  38. AVFilterContext *avfilter_graph_get_filter(AVFilterGraph *graph, char *name)
  39. {
  40. int i;
  41. for(i = 0; i < graph->filter_count; i ++)
  42. if(graph->filters[i]->name && !strcmp(name, graph->filters[i]->name))
  43. return graph->filters[i];
  44. return NULL;
  45. }
  46. static int query_formats(AVFilterGraph *graph)
  47. {
  48. int i, j;
  49. /* ask all the sub-filters for their supported colorspaces */
  50. for(i = 0; i < graph->filter_count; i ++) {
  51. if(graph->filters[i]->filter->query_formats)
  52. graph->filters[i]->filter->query_formats(graph->filters[i]);
  53. else
  54. avfilter_default_query_formats(graph->filters[i]);
  55. }
  56. /* go through and merge as many format lists as possible */
  57. for(i = 0; i < graph->filter_count; i ++) {
  58. AVFilterContext *filter = graph->filters[i];
  59. for(j = 0; j < filter->input_count; j ++) {
  60. AVFilterLink *link = filter->inputs[j];
  61. if(link && link->in_formats != link->out_formats) {
  62. if(!avfilter_merge_formats(link->in_formats,
  63. link->out_formats)) {
  64. /* couldn't merge format lists. auto-insert scale filter */
  65. AVFilterContext *scale =
  66. avfilter_open(avfilter_get_by_name("scale"), NULL);
  67. if(!scale || scale->filter->init(scale, NULL, NULL) ||
  68. avfilter_insert_filter(link, scale, 0, 0)) {
  69. avfilter_destroy(scale);
  70. return -1;
  71. }
  72. avfilter_graph_add_filter(graph, scale);
  73. scale->filter->query_formats(scale);
  74. if(!avfilter_merge_formats(scale-> inputs[0]->in_formats,
  75. scale-> inputs[0]->out_formats)||
  76. !avfilter_merge_formats(scale->outputs[0]->in_formats,
  77. scale->outputs[0]->out_formats))
  78. return -1;
  79. }
  80. }
  81. }
  82. }
  83. return 0;
  84. }
  85. static void pick_format(AVFilterLink *link)
  86. {
  87. if(!link || !link->in_formats)
  88. return;
  89. link->in_formats->format_count = 1;
  90. link->format = link->in_formats->formats[0];
  91. avfilter_formats_unref(&link->in_formats);
  92. avfilter_formats_unref(&link->out_formats);
  93. }
  94. static void pick_formats(AVFilterGraph *graph)
  95. {
  96. int i, j;
  97. for(i = 0; i < graph->filter_count; i ++) {
  98. AVFilterContext *filter = graph->filters[i];
  99. for(j = 0; j < filter->input_count; j ++)
  100. pick_format(filter->inputs[j]);
  101. for(j = 0; j < filter->output_count; j ++)
  102. pick_format(filter->outputs[j]);
  103. }
  104. }
  105. int avfilter_graph_config_formats(AVFilterGraph *graph)
  106. {
  107. /* find supported formats from sub-filters, and merge along links */
  108. if(query_formats(graph))
  109. return -1;
  110. /* Once everything is merged, it's possible that we'll still have
  111. * multiple valid colorspace choices. We pick the first one. */
  112. pick_formats(graph);
  113. return 0;
  114. }