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.

224 lines
7.1KB

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