avfiltergraph.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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/audioconvert.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 insert_conv_filter(AVFilterGraph *graph, AVFilterLink *link,
  117. const char *filt_name, const char *filt_args)
  118. {
  119. static int auto_count = 0, ret;
  120. char inst_name[32];
  121. AVFilterContext *filt_ctx;
  122. snprintf(inst_name, sizeof(inst_name), "auto-inserted %s %d",
  123. filt_name, auto_count++);
  124. if ((ret = avfilter_graph_create_filter(&filt_ctx,
  125. avfilter_get_by_name(filt_name),
  126. inst_name, filt_args, NULL, graph)) < 0)
  127. return ret;
  128. if ((ret = avfilter_insert_filter(link, filt_ctx, 0, 0)) < 0)
  129. return ret;
  130. filt_ctx->filter->query_formats(filt_ctx);
  131. if ( ((link = filt_ctx-> inputs[0]) &&
  132. !avfilter_merge_formats(link->in_formats, link->out_formats)) ||
  133. ((link = filt_ctx->outputs[0]) &&
  134. !avfilter_merge_formats(link->in_formats, link->out_formats))
  135. ) {
  136. av_log(NULL, AV_LOG_ERROR,
  137. "Impossible to convert between the formats supported by the filter "
  138. "'%s' and the filter '%s'\n", link->src->name, link->dst->name);
  139. return AVERROR(EINVAL);
  140. }
  141. if (link->type == AVMEDIA_TYPE_AUDIO &&
  142. (((link = filt_ctx-> inputs[0]) &&
  143. (!avfilter_merge_formats(link->in_chlayouts, link->out_chlayouts) ||
  144. !avfilter_merge_formats(link->in_packing, link->out_packing))) ||
  145. ((link = filt_ctx->outputs[0]) &&
  146. (!avfilter_merge_formats(link->in_chlayouts, link->out_chlayouts) ||
  147. !avfilter_merge_formats(link->in_packing, link->out_packing))))
  148. ) {
  149. av_log(NULL, AV_LOG_ERROR,
  150. "Impossible to convert between the channel layouts/packing formats supported by the filter "
  151. "'%s' and the filter '%s'\n", link->src->name, link->dst->name);
  152. return AVERROR(EINVAL);
  153. }
  154. return 0;
  155. }
  156. static int query_formats(AVFilterGraph *graph, AVClass *log_ctx)
  157. {
  158. int i, j, ret;
  159. char filt_args[128];
  160. /* ask all the sub-filters for their supported media formats */
  161. for (i = 0; i < graph->filter_count; i++) {
  162. if (graph->filters[i]->filter->query_formats)
  163. graph->filters[i]->filter->query_formats(graph->filters[i]);
  164. else
  165. avfilter_default_query_formats(graph->filters[i]);
  166. }
  167. /* go through and merge as many format lists as possible */
  168. for (i = 0; i < graph->filter_count; i++) {
  169. AVFilterContext *filter = graph->filters[i];
  170. for (j = 0; j < filter->input_count; j++) {
  171. AVFilterLink *link = filter->inputs[j];
  172. if (!link) continue;
  173. if (!link->in_formats || !link->out_formats)
  174. return AVERROR(EINVAL);
  175. if (link->type == AVMEDIA_TYPE_VIDEO &&
  176. !avfilter_merge_formats(link->in_formats, link->out_formats)) {
  177. /* couldn't merge format lists, auto-insert scale filter */
  178. snprintf(filt_args, sizeof(filt_args), "0:0:%s",
  179. graph->scale_sws_opts);
  180. if (ret = insert_conv_filter(graph, link, "scale", filt_args))
  181. return ret;
  182. }
  183. else if (link->type == AVMEDIA_TYPE_AUDIO) {
  184. if (!link->in_chlayouts || !link->out_chlayouts ||
  185. !link->in_packing || !link->out_packing)
  186. return AVERROR(EINVAL);
  187. if (!avfilter_merge_formats(link->in_formats, link->out_formats) ||
  188. !avfilter_merge_formats(link->in_chlayouts, link->out_chlayouts) ||
  189. !avfilter_merge_formats(link->in_packing, link->out_packing))
  190. if (ret = insert_conv_filter(graph, link, "aconvert", NULL))
  191. return ret;
  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. if (link->type == AVMEDIA_TYPE_AUDIO) {
  206. link->in_chlayouts->format_count = 1;
  207. link->channel_layout = link->in_chlayouts->formats[0];
  208. avfilter_formats_unref(&link->in_chlayouts);
  209. avfilter_formats_unref(&link->out_chlayouts);
  210. link->in_packing->format_count = 1;
  211. link->planar = link->in_packing->formats[0] == AVFILTER_PLANAR;
  212. avfilter_formats_unref(&link->in_packing);
  213. avfilter_formats_unref(&link->out_packing);
  214. }
  215. }
  216. static void pick_formats(AVFilterGraph *graph)
  217. {
  218. int i, j;
  219. for (i = 0; i < graph->filter_count; i++) {
  220. AVFilterContext *filter = graph->filters[i];
  221. for (j = 0; j < filter->input_count; j++)
  222. pick_format(filter->inputs[j]);
  223. for (j = 0; j < filter->output_count; j++)
  224. pick_format(filter->outputs[j]);
  225. }
  226. }
  227. int ff_avfilter_graph_config_formats(AVFilterGraph *graph, AVClass *log_ctx)
  228. {
  229. int ret;
  230. /* find supported formats from sub-filters, and merge along links */
  231. if ((ret = query_formats(graph, log_ctx)) < 0)
  232. return ret;
  233. /* Once everything is merged, it's possible that we'll still have
  234. * multiple valid media format choices. We pick the first one. */
  235. pick_formats(graph);
  236. return 0;
  237. }
  238. int avfilter_graph_config(AVFilterGraph *graphctx, void *log_ctx)
  239. {
  240. int ret;
  241. if ((ret = ff_avfilter_graph_check_validity(graphctx, log_ctx)))
  242. return ret;
  243. if ((ret = ff_avfilter_graph_config_formats(graphctx, log_ctx)))
  244. return ret;
  245. if ((ret = ff_avfilter_graph_config_links(graphctx, log_ctx)))
  246. return ret;
  247. return 0;
  248. }
  249. int avfilter_graph_send_command(AVFilterGraph *graph, const char *target, const char *cmd, const char *arg, char *res, int res_len, int flags)
  250. {
  251. int i, r = AVERROR(ENOSYS);
  252. if(!graph)
  253. return r;
  254. if((flags & AVFILTER_CMD_FLAG_ONE) && !(flags & AVFILTER_CMD_FLAG_FAST)) {
  255. r=avfilter_graph_send_command(graph, target, cmd, arg, res, res_len, flags | AVFILTER_CMD_FLAG_FAST);
  256. if(r != AVERROR(ENOSYS))
  257. return r;
  258. }
  259. if(res_len && res)
  260. res[0]= 0;
  261. for (i = 0; i < graph->filter_count; i++) {
  262. AVFilterContext *filter = graph->filters[i];
  263. if(!strcmp(target, "all") || (filter->name && !strcmp(target, filter->name)) || !strcmp(target, filter->filter->name)){
  264. r = avfilter_process_command(filter, cmd, arg, res, res_len, flags);
  265. if(r != AVERROR(ENOSYS)) {
  266. if((flags & AVFILTER_CMD_FLAG_ONE) || r<0)
  267. return r;
  268. }
  269. }
  270. }
  271. return r;
  272. }
  273. int avfilter_graph_queue_command(AVFilterGraph *graph, const char *target, const char *command, const char *arg, int flags, double ts)
  274. {
  275. int i;
  276. if(!graph)
  277. return 0;
  278. for (i = 0; i < graph->filter_count; i++) {
  279. AVFilterContext *filter = graph->filters[i];
  280. if(filter && (!strcmp(target, "all") || !strcmp(target, filter->name) || !strcmp(target, filter->filter->name))){
  281. AVFilterCommand **que = &filter->command_queue, *next;
  282. while(*que && (*que)->time <= ts)
  283. que = &(*que)->next;
  284. next= *que;
  285. *que= av_mallocz(sizeof(AVFilterCommand));
  286. (*que)->command = av_strdup(command);
  287. (*que)->arg = av_strdup(arg);
  288. (*que)->time = ts;
  289. (*que)->flags = flags;
  290. (*que)->next = next;
  291. if(flags & AVFILTER_CMD_FLAG_ONE)
  292. return 0;
  293. }
  294. }
  295. return 0;
  296. }