avfiltergraph.c 8.0 KB

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