avfiltergraph.c 8.2 KB

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