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.

305 lines
9.3KB

  1. /*
  2. * filter graphs
  3. * Copyright (c) 2008 Vitor Sessak
  4. * Copyright (c) 2007 Bobby Bingham
  5. *
  6. * This file is part of Libav.
  7. *
  8. * Libav 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. * Libav 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 Libav; 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. #include "libavutil/log.h"
  28. static const AVClass filtergraph_class = {
  29. .class_name = "AVFilterGraph",
  30. .item_name = av_default_item_name,
  31. .version = LIBAVUTIL_VERSION_INT,
  32. };
  33. AVFilterGraph *avfilter_graph_alloc(void)
  34. {
  35. AVFilterGraph *ret = av_mallocz(sizeof(AVFilterGraph));
  36. if (!ret)
  37. return NULL;
  38. #if FF_API_GRAPH_AVCLASS
  39. ret->av_class = &filtergraph_class;
  40. #endif
  41. return ret;
  42. }
  43. void avfilter_graph_free(AVFilterGraph **graph)
  44. {
  45. if (!*graph)
  46. return;
  47. for (; (*graph)->filter_count > 0; (*graph)->filter_count--)
  48. avfilter_free((*graph)->filters[(*graph)->filter_count - 1]);
  49. av_freep(&(*graph)->scale_sws_opts);
  50. av_freep(&(*graph)->filters);
  51. av_freep(graph);
  52. }
  53. int avfilter_graph_add_filter(AVFilterGraph *graph, AVFilterContext *filter)
  54. {
  55. AVFilterContext **filters = av_realloc(graph->filters,
  56. sizeof(AVFilterContext*) * (graph->filter_count+1));
  57. if (!filters)
  58. return AVERROR(ENOMEM);
  59. graph->filters = filters;
  60. graph->filters[graph->filter_count++] = filter;
  61. return 0;
  62. }
  63. int avfilter_graph_create_filter(AVFilterContext **filt_ctx, AVFilter *filt,
  64. const char *name, const char *args, void *opaque,
  65. AVFilterGraph *graph_ctx)
  66. {
  67. int ret;
  68. if ((ret = avfilter_open(filt_ctx, filt, name)) < 0)
  69. goto fail;
  70. if ((ret = avfilter_init_filter(*filt_ctx, args, opaque)) < 0)
  71. goto fail;
  72. if ((ret = avfilter_graph_add_filter(graph_ctx, *filt_ctx)) < 0)
  73. goto fail;
  74. return 0;
  75. fail:
  76. if (*filt_ctx)
  77. avfilter_free(*filt_ctx);
  78. *filt_ctx = NULL;
  79. return ret;
  80. }
  81. int ff_avfilter_graph_check_validity(AVFilterGraph *graph, AVClass *log_ctx)
  82. {
  83. AVFilterContext *filt;
  84. int i, j;
  85. for (i = 0; i < graph->filter_count; i++) {
  86. filt = graph->filters[i];
  87. for (j = 0; j < filt->input_count; j++) {
  88. if (!filt->inputs[j] || !filt->inputs[j]->src) {
  89. av_log(log_ctx, AV_LOG_ERROR,
  90. "Input pad \"%s\" for the filter \"%s\" of type \"%s\" not connected to any source\n",
  91. filt->input_pads[j].name, filt->name, filt->filter->name);
  92. return AVERROR(EINVAL);
  93. }
  94. }
  95. for (j = 0; j < filt->output_count; j++) {
  96. if (!filt->outputs[j] || !filt->outputs[j]->dst) {
  97. av_log(log_ctx, AV_LOG_ERROR,
  98. "Output pad \"%s\" for the filter \"%s\" of type \"%s\" not connected to any destination\n",
  99. filt->output_pads[j].name, filt->name, filt->filter->name);
  100. return AVERROR(EINVAL);
  101. }
  102. }
  103. }
  104. return 0;
  105. }
  106. int ff_avfilter_graph_config_links(AVFilterGraph *graph, AVClass *log_ctx)
  107. {
  108. AVFilterContext *filt;
  109. int i, ret;
  110. for (i=0; i < graph->filter_count; i++) {
  111. filt = graph->filters[i];
  112. if (!filt->output_count) {
  113. if ((ret = avfilter_config_links(filt)))
  114. return ret;
  115. }
  116. }
  117. return 0;
  118. }
  119. AVFilterContext *avfilter_graph_get_filter(AVFilterGraph *graph, char *name)
  120. {
  121. int i;
  122. for (i = 0; i < graph->filter_count; i++)
  123. if (graph->filters[i]->name && !strcmp(name, graph->filters[i]->name))
  124. return graph->filters[i];
  125. return NULL;
  126. }
  127. static int query_formats(AVFilterGraph *graph, AVClass *log_ctx)
  128. {
  129. int i, j, ret;
  130. int scaler_count = 0;
  131. char inst_name[30];
  132. /* ask all the sub-filters for their supported media formats */
  133. for (i = 0; i < graph->filter_count; i++) {
  134. if (graph->filters[i]->filter->query_formats)
  135. graph->filters[i]->filter->query_formats(graph->filters[i]);
  136. else
  137. avfilter_default_query_formats(graph->filters[i]);
  138. }
  139. /* go through and merge as many format lists as possible */
  140. for (i = 0; i < graph->filter_count; i++) {
  141. AVFilterContext *filter = graph->filters[i];
  142. for (j = 0; j < filter->input_count; j++) {
  143. AVFilterLink *link = filter->inputs[j];
  144. if (link && link->in_formats != link->out_formats) {
  145. if (!avfilter_merge_formats(link->in_formats,
  146. link->out_formats)) {
  147. AVFilterContext *scale;
  148. char scale_args[256];
  149. /* couldn't merge format lists. auto-insert scale filter */
  150. snprintf(inst_name, sizeof(inst_name), "auto-inserted scaler %d",
  151. scaler_count++);
  152. snprintf(scale_args, sizeof(scale_args), "0:0:%s", graph->scale_sws_opts);
  153. if ((ret = avfilter_graph_create_filter(&scale, avfilter_get_by_name("scale"),
  154. inst_name, scale_args, NULL, graph)) < 0)
  155. return ret;
  156. if ((ret = avfilter_insert_filter(link, scale, 0, 0)) < 0)
  157. return ret;
  158. scale->filter->query_formats(scale);
  159. if (((link = scale-> inputs[0]) &&
  160. !avfilter_merge_formats(link->in_formats, link->out_formats)) ||
  161. ((link = scale->outputs[0]) &&
  162. !avfilter_merge_formats(link->in_formats, link->out_formats))) {
  163. av_log(log_ctx, AV_LOG_ERROR,
  164. "Impossible to convert between the formats supported by the filter "
  165. "'%s' and the filter '%s'\n", link->src->name, link->dst->name);
  166. return AVERROR(EINVAL);
  167. }
  168. }
  169. }
  170. }
  171. }
  172. return 0;
  173. }
  174. static void pick_format(AVFilterLink *link)
  175. {
  176. if (!link || !link->in_formats)
  177. return;
  178. link->in_formats->format_count = 1;
  179. link->format = link->in_formats->formats[0];
  180. avfilter_formats_unref(&link->in_formats);
  181. avfilter_formats_unref(&link->out_formats);
  182. }
  183. static int reduce_formats_on_filter(AVFilterContext *filter)
  184. {
  185. int i, j, k, ret = 0;
  186. for (i = 0; i < filter->input_count; i++) {
  187. AVFilterLink *link = filter->inputs[i];
  188. int format = link->out_formats->formats[0];
  189. if (link->out_formats->format_count != 1)
  190. continue;
  191. for (j = 0; j < filter->output_count; j++) {
  192. AVFilterLink *out_link = filter->outputs[j];
  193. AVFilterFormats *fmts = out_link->in_formats;
  194. if (link->type != out_link->type ||
  195. out_link->in_formats->format_count == 1)
  196. continue;
  197. for (k = 0; k < out_link->in_formats->format_count; k++)
  198. if (fmts->formats[k] == format) {
  199. fmts->formats[0] = format;
  200. fmts->format_count = 1;
  201. ret = 1;
  202. break;
  203. }
  204. }
  205. }
  206. return ret;
  207. }
  208. static void reduce_formats(AVFilterGraph *graph)
  209. {
  210. int i, reduced;
  211. do {
  212. reduced = 0;
  213. for (i = 0; i < graph->filter_count; i++)
  214. reduced |= reduce_formats_on_filter(graph->filters[i]);
  215. } while (reduced);
  216. }
  217. static void pick_formats(AVFilterGraph *graph)
  218. {
  219. int i, j;
  220. for (i = 0; i < graph->filter_count; i++) {
  221. AVFilterContext *filter = graph->filters[i];
  222. for (j = 0; j < filter->input_count; j++)
  223. pick_format(filter->inputs[j]);
  224. for (j = 0; j < filter->output_count; j++)
  225. pick_format(filter->outputs[j]);
  226. }
  227. }
  228. int ff_avfilter_graph_config_formats(AVFilterGraph *graph, AVClass *log_ctx)
  229. {
  230. int ret;
  231. /* find supported formats from sub-filters, and merge along links */
  232. if ((ret = query_formats(graph, log_ctx)) < 0)
  233. return ret;
  234. /* Once everything is merged, it's possible that we'll still have
  235. * multiple valid media format choices. We try to minimize the amount
  236. * of format conversion inside filters */
  237. reduce_formats(graph);
  238. pick_formats(graph);
  239. return 0;
  240. }
  241. int avfilter_graph_config(AVFilterGraph *graphctx, void *log_ctx)
  242. {
  243. int ret;
  244. if ((ret = ff_avfilter_graph_check_validity(graphctx, log_ctx)))
  245. return ret;
  246. if ((ret = ff_avfilter_graph_config_formats(graphctx, log_ctx)))
  247. return ret;
  248. if ((ret = ff_avfilter_graph_config_links(graphctx, log_ctx)))
  249. return ret;
  250. return 0;
  251. }