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.

243 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 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. 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 -1;
  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 -1;
  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 -1;
  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. }
  171. static void pick_formats(AVFilterGraph *graph)
  172. {
  173. int i, j;
  174. for (i = 0; i < graph->filter_count; i++) {
  175. AVFilterContext *filter = graph->filters[i];
  176. for (j = 0; j < filter->input_count; j++)
  177. pick_format(filter->inputs[j]);
  178. for (j = 0; j < filter->output_count; j++)
  179. pick_format(filter->outputs[j]);
  180. }
  181. }
  182. int ff_avfilter_graph_config_formats(AVFilterGraph *graph, AVClass *log_ctx)
  183. {
  184. /* find supported formats from sub-filters, and merge along links */
  185. if (query_formats(graph, log_ctx))
  186. return -1;
  187. /* Once everything is merged, it's possible that we'll still have
  188. * multiple valid media format choices. We pick the first one. */
  189. pick_formats(graph);
  190. return 0;
  191. }
  192. int avfilter_graph_config(AVFilterGraph *graphctx, AVClass *log_ctx)
  193. {
  194. int ret;
  195. if ((ret = ff_avfilter_graph_check_validity(graphctx, log_ctx)))
  196. return ret;
  197. if ((ret = ff_avfilter_graph_config_formats(graphctx, log_ctx)))
  198. return ret;
  199. if ((ret = ff_avfilter_graph_config_links(graphctx, log_ctx)))
  200. return ret;
  201. return 0;
  202. }