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.

331 lines
11KB

  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, resampler_count = 0;
  131. /* ask all the sub-filters for their supported media formats */
  132. for (i = 0; i < graph->filter_count; i++) {
  133. if (graph->filters[i]->filter->query_formats)
  134. graph->filters[i]->filter->query_formats(graph->filters[i]);
  135. else
  136. avfilter_default_query_formats(graph->filters[i]);
  137. }
  138. /* go through and merge as many format lists as possible */
  139. for (i = 0; i < graph->filter_count; i++) {
  140. AVFilterContext *filter = graph->filters[i];
  141. for (j = 0; j < filter->input_count; j++) {
  142. AVFilterLink *link = filter->inputs[j];
  143. if (link && link->in_formats != link->out_formats) {
  144. if (!avfilter_merge_formats(link->in_formats,
  145. link->out_formats)) {
  146. AVFilterContext *convert;
  147. AVFilter *filter;
  148. AVFilterLink *inlink, *outlink;
  149. char scale_args[256];
  150. char inst_name[30];
  151. /* couldn't merge format lists. auto-insert conversion filter */
  152. switch (link->type) {
  153. case AVMEDIA_TYPE_VIDEO:
  154. snprintf(inst_name, sizeof(inst_name), "auto-inserted scaler %d",
  155. scaler_count++);
  156. snprintf(scale_args, sizeof(scale_args), "0:0:%s", graph->scale_sws_opts);
  157. if ((ret = avfilter_graph_create_filter(&convert,
  158. avfilter_get_by_name("scale"),
  159. inst_name, scale_args, NULL,
  160. graph)) < 0)
  161. return ret;
  162. break;
  163. case AVMEDIA_TYPE_AUDIO:
  164. if (!(filter = avfilter_get_by_name("resample"))) {
  165. av_log(log_ctx, AV_LOG_ERROR, "'resample' filter "
  166. "not present, cannot convert audio formats.\n");
  167. return AVERROR(EINVAL);
  168. }
  169. snprintf(inst_name, sizeof(inst_name), "auto-inserted resampler %d",
  170. resampler_count++);
  171. if ((ret = avfilter_graph_create_filter(&convert,
  172. avfilter_get_by_name("resample"),
  173. inst_name, NULL, NULL, graph)) < 0)
  174. return ret;
  175. break;
  176. default:
  177. return AVERROR(EINVAL);
  178. }
  179. if ((ret = avfilter_insert_filter(link, convert, 0, 0)) < 0)
  180. return ret;
  181. convert->filter->query_formats(convert);
  182. inlink = convert->inputs[0];
  183. outlink = convert->outputs[0];
  184. if (!avfilter_merge_formats( inlink->in_formats, inlink->out_formats) ||
  185. !avfilter_merge_formats(outlink->in_formats, outlink->out_formats)) {
  186. av_log(log_ctx, AV_LOG_ERROR,
  187. "Impossible to convert between the formats supported by the filter "
  188. "'%s' and the filter '%s'\n", link->src->name, link->dst->name);
  189. return AVERROR(EINVAL);
  190. }
  191. }
  192. }
  193. }
  194. }
  195. return 0;
  196. }
  197. static void pick_format(AVFilterLink *link)
  198. {
  199. if (!link || !link->in_formats)
  200. return;
  201. link->in_formats->format_count = 1;
  202. link->format = link->in_formats->formats[0];
  203. avfilter_formats_unref(&link->in_formats);
  204. avfilter_formats_unref(&link->out_formats);
  205. }
  206. static int reduce_formats_on_filter(AVFilterContext *filter)
  207. {
  208. int i, j, k, ret = 0;
  209. for (i = 0; i < filter->input_count; i++) {
  210. AVFilterLink *link = filter->inputs[i];
  211. int format = link->out_formats->formats[0];
  212. if (link->out_formats->format_count != 1)
  213. continue;
  214. for (j = 0; j < filter->output_count; j++) {
  215. AVFilterLink *out_link = filter->outputs[j];
  216. AVFilterFormats *fmts = out_link->in_formats;
  217. if (link->type != out_link->type ||
  218. out_link->in_formats->format_count == 1)
  219. continue;
  220. for (k = 0; k < out_link->in_formats->format_count; k++)
  221. if (fmts->formats[k] == format) {
  222. fmts->formats[0] = format;
  223. fmts->format_count = 1;
  224. ret = 1;
  225. break;
  226. }
  227. }
  228. }
  229. return ret;
  230. }
  231. static void reduce_formats(AVFilterGraph *graph)
  232. {
  233. int i, reduced;
  234. do {
  235. reduced = 0;
  236. for (i = 0; i < graph->filter_count; i++)
  237. reduced |= reduce_formats_on_filter(graph->filters[i]);
  238. } while (reduced);
  239. }
  240. static void pick_formats(AVFilterGraph *graph)
  241. {
  242. int i, j;
  243. for (i = 0; i < graph->filter_count; i++) {
  244. AVFilterContext *filter = graph->filters[i];
  245. for (j = 0; j < filter->input_count; j++)
  246. pick_format(filter->inputs[j]);
  247. for (j = 0; j < filter->output_count; j++)
  248. pick_format(filter->outputs[j]);
  249. }
  250. }
  251. int ff_avfilter_graph_config_formats(AVFilterGraph *graph, AVClass *log_ctx)
  252. {
  253. int ret;
  254. /* find supported formats from sub-filters, and merge along links */
  255. if ((ret = query_formats(graph, log_ctx)) < 0)
  256. return ret;
  257. /* Once everything is merged, it's possible that we'll still have
  258. * multiple valid media format choices. We try to minimize the amount
  259. * of format conversion inside filters */
  260. reduce_formats(graph);
  261. pick_formats(graph);
  262. return 0;
  263. }
  264. int avfilter_graph_config(AVFilterGraph *graphctx, void *log_ctx)
  265. {
  266. int ret;
  267. if ((ret = ff_avfilter_graph_check_validity(graphctx, log_ctx)))
  268. return ret;
  269. if ((ret = ff_avfilter_graph_config_formats(graphctx, log_ctx)))
  270. return ret;
  271. if ((ret = ff_avfilter_graph_config_links(graphctx, log_ctx)))
  272. return ret;
  273. return 0;
  274. }