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.

223 lines
7.0KB

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