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.

182 lines
5.8KB

  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. int avfilter_graph_check_validity(AVFilterGraph *graph, AVClass *log_ctx)
  42. {
  43. AVFilterContext *filt;
  44. int i, j;
  45. for (i=0; i < graph->filter_count; i++) {
  46. filt = graph->filters[i];
  47. for (j = 0; j < filt->input_count; j++) {
  48. if (!filt->inputs[j] || !filt->inputs[j]->src) {
  49. av_log(log_ctx, AV_LOG_ERROR,
  50. "Input pad \"%s\" for the filter \"%s\" of type \"%s\" not connected to any source\n",
  51. filt->input_pads[j].name, filt->name, filt->filter->name);
  52. return -1;
  53. }
  54. }
  55. for (j = 0; j < filt->output_count; j++) {
  56. if (!filt->outputs[j] || !filt->outputs[j]->dst) {
  57. av_log(log_ctx, AV_LOG_ERROR,
  58. "Output pad \"%s\" for the filter \"%s\" of type \"%s\" not connected to any destination\n",
  59. filt->output_pads[j].name, filt->name, filt->filter->name);
  60. return -1;
  61. }
  62. }
  63. }
  64. return 0;
  65. }
  66. AVFilterContext *avfilter_graph_get_filter(AVFilterGraph *graph, char *name)
  67. {
  68. int i;
  69. for(i = 0; i < graph->filter_count; i ++)
  70. if(graph->filters[i]->name && !strcmp(name, graph->filters[i]->name))
  71. return graph->filters[i];
  72. return NULL;
  73. }
  74. static int query_formats(AVFilterGraph *graph)
  75. {
  76. int i, j;
  77. int scaler_count = 0;
  78. char inst_name[30];
  79. /* ask all the sub-filters for their supported colorspaces */
  80. for(i = 0; i < graph->filter_count; i ++) {
  81. if(graph->filters[i]->filter->query_formats)
  82. graph->filters[i]->filter->query_formats(graph->filters[i]);
  83. else
  84. avfilter_default_query_formats(graph->filters[i]);
  85. }
  86. /* go through and merge as many format lists as possible */
  87. for(i = 0; i < graph->filter_count; i ++) {
  88. AVFilterContext *filter = graph->filters[i];
  89. for(j = 0; j < filter->input_count; j ++) {
  90. AVFilterLink *link = filter->inputs[j];
  91. if(link && link->in_formats != link->out_formats) {
  92. if(!avfilter_merge_formats(link->in_formats,
  93. link->out_formats)) {
  94. AVFilterContext *scale;
  95. /* couldn't merge format lists. auto-insert scale filter */
  96. snprintf(inst_name, sizeof(inst_name), "auto-inserted scaler %d",
  97. scaler_count);
  98. scale =
  99. avfilter_open(avfilter_get_by_name("scale"),inst_name);
  100. if(!scale || scale->filter->init(scale, NULL, NULL) ||
  101. avfilter_insert_filter(link, scale, 0, 0)) {
  102. avfilter_destroy(scale);
  103. return -1;
  104. }
  105. if (avfilter_graph_add_filter(graph, scale) < 0)
  106. return -1;
  107. scale->filter->query_formats(scale);
  108. if(!avfilter_merge_formats(scale-> inputs[0]->in_formats,
  109. scale-> inputs[0]->out_formats)||
  110. !avfilter_merge_formats(scale->outputs[0]->in_formats,
  111. scale->outputs[0]->out_formats))
  112. return -1;
  113. }
  114. }
  115. }
  116. }
  117. return 0;
  118. }
  119. static void pick_format(AVFilterLink *link)
  120. {
  121. if(!link || !link->in_formats)
  122. return;
  123. link->in_formats->format_count = 1;
  124. link->format = link->in_formats->formats[0];
  125. avfilter_formats_unref(&link->in_formats);
  126. avfilter_formats_unref(&link->out_formats);
  127. }
  128. static void pick_formats(AVFilterGraph *graph)
  129. {
  130. int i, j;
  131. for(i = 0; i < graph->filter_count; i ++) {
  132. AVFilterContext *filter = graph->filters[i];
  133. for(j = 0; j < filter->input_count; j ++)
  134. pick_format(filter->inputs[j]);
  135. for(j = 0; j < filter->output_count; j ++)
  136. pick_format(filter->outputs[j]);
  137. }
  138. }
  139. int avfilter_graph_config_formats(AVFilterGraph *graph)
  140. {
  141. /* find supported formats from sub-filters, and merge along links */
  142. if(query_formats(graph))
  143. return -1;
  144. /* Once everything is merged, it's possible that we'll still have
  145. * multiple valid colorspace choices. We pick the first one. */
  146. pick_formats(graph);
  147. return 0;
  148. }