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.

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