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.

144 lines
4.6KB

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