avfiltergraph.c 4.8 KB

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