avfiltergraph.c 6.6 KB

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