avfiltergraph.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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 "libavutil/avstring.h"
  26. #include "avfilter.h"
  27. #include "avfiltergraph.h"
  28. #include "internal.h"
  29. AVFilterGraph *avfilter_graph_alloc(void)
  30. {
  31. return av_mallocz(sizeof(AVFilterGraph));
  32. }
  33. void avfilter_graph_free(AVFilterGraph **graph)
  34. {
  35. if (!*graph)
  36. return;
  37. for (; (*graph)->filter_count > 0; (*graph)->filter_count--)
  38. avfilter_free((*graph)->filters[(*graph)->filter_count - 1]);
  39. av_freep(&(*graph)->scale_sws_opts);
  40. av_freep(&(*graph)->filters);
  41. av_freep(graph);
  42. }
  43. int avfilter_graph_add_filter(AVFilterGraph *graph, AVFilterContext *filter)
  44. {
  45. AVFilterContext **filters = av_realloc(graph->filters,
  46. sizeof(AVFilterContext*) * (graph->filter_count+1));
  47. if (!filters)
  48. return AVERROR(ENOMEM);
  49. graph->filters = filters;
  50. graph->filters[graph->filter_count++] = filter;
  51. return 0;
  52. }
  53. int avfilter_graph_create_filter(AVFilterContext **filt_ctx, AVFilter *filt,
  54. const char *name, const char *args, void *opaque,
  55. AVFilterGraph *graph_ctx)
  56. {
  57. int ret;
  58. if ((ret = avfilter_open(filt_ctx, filt, name)) < 0)
  59. goto fail;
  60. if ((ret = avfilter_init_filter(*filt_ctx, args, opaque)) < 0)
  61. goto fail;
  62. if ((ret = avfilter_graph_add_filter(graph_ctx, *filt_ctx)) < 0)
  63. goto fail;
  64. return 0;
  65. fail:
  66. if (*filt_ctx)
  67. avfilter_free(*filt_ctx);
  68. *filt_ctx = NULL;
  69. return ret;
  70. }
  71. int ff_avfilter_graph_check_validity(AVFilterGraph *graph, AVClass *log_ctx)
  72. {
  73. AVFilterContext *filt;
  74. int i, j;
  75. for (i = 0; i < graph->filter_count; i++) {
  76. filt = graph->filters[i];
  77. for (j = 0; j < filt->input_count; j++) {
  78. if (!filt->inputs[j] || !filt->inputs[j]->src) {
  79. av_log(log_ctx, AV_LOG_ERROR,
  80. "Input pad \"%s\" for the filter \"%s\" of type \"%s\" not connected to any source\n",
  81. filt->input_pads[j].name, filt->name, filt->filter->name);
  82. return AVERROR(EINVAL);
  83. }
  84. }
  85. for (j = 0; j < filt->output_count; j++) {
  86. if (!filt->outputs[j] || !filt->outputs[j]->dst) {
  87. av_log(log_ctx, AV_LOG_ERROR,
  88. "Output pad \"%s\" for the filter \"%s\" of type \"%s\" not connected to any destination\n",
  89. filt->output_pads[j].name, filt->name, filt->filter->name);
  90. return AVERROR(EINVAL);
  91. }
  92. }
  93. }
  94. return 0;
  95. }
  96. int ff_avfilter_graph_config_links(AVFilterGraph *graph, AVClass *log_ctx)
  97. {
  98. AVFilterContext *filt;
  99. int i, ret;
  100. for (i=0; i < graph->filter_count; i++) {
  101. filt = graph->filters[i];
  102. if (!filt->output_count) {
  103. if ((ret = avfilter_config_links(filt)))
  104. return ret;
  105. }
  106. }
  107. return 0;
  108. }
  109. AVFilterContext *avfilter_graph_get_filter(AVFilterGraph *graph, char *name)
  110. {
  111. int i;
  112. for (i = 0; i < graph->filter_count; i++)
  113. if (graph->filters[i]->name && !strcmp(name, graph->filters[i]->name))
  114. return graph->filters[i];
  115. return NULL;
  116. }
  117. static int insert_conv_filter(AVFilterGraph *graph, AVFilterLink *link,
  118. const char *filt_name, const char *filt_args)
  119. {
  120. static int auto_count = 0, ret;
  121. char inst_name[32];
  122. AVFilterContext *filt_ctx;
  123. snprintf(inst_name, sizeof(inst_name), "auto-inserted %s %d",
  124. filt_name, auto_count++);
  125. if ((ret = avfilter_graph_create_filter(&filt_ctx,
  126. avfilter_get_by_name(filt_name),
  127. inst_name, filt_args, NULL, graph)) < 0)
  128. return ret;
  129. if ((ret = avfilter_insert_filter(link, filt_ctx, 0, 0)) < 0)
  130. return ret;
  131. filt_ctx->filter->query_formats(filt_ctx);
  132. if ( ((link = filt_ctx-> inputs[0]) &&
  133. !avfilter_merge_formats(link->in_formats, link->out_formats)) ||
  134. ((link = filt_ctx->outputs[0]) &&
  135. !avfilter_merge_formats(link->in_formats, link->out_formats))
  136. ) {
  137. av_log(NULL, AV_LOG_ERROR,
  138. "Impossible to convert between the formats supported by the filter "
  139. "'%s' and the filter '%s'\n", link->src->name, link->dst->name);
  140. return AVERROR(EINVAL);
  141. }
  142. if (link->type == AVMEDIA_TYPE_AUDIO &&
  143. (((link = filt_ctx-> inputs[0]) &&
  144. (!avfilter_merge_formats(link->in_chlayouts, link->out_chlayouts) ||
  145. !avfilter_merge_formats(link->in_packing, link->out_packing))) ||
  146. ((link = filt_ctx->outputs[0]) &&
  147. (!avfilter_merge_formats(link->in_chlayouts, link->out_chlayouts) ||
  148. !avfilter_merge_formats(link->in_packing, link->out_packing))))
  149. ) {
  150. av_log(NULL, AV_LOG_ERROR,
  151. "Impossible to convert between the channel layouts/packing formats supported by the filter "
  152. "'%s' and the filter '%s'\n", link->src->name, link->dst->name);
  153. return AVERROR(EINVAL);
  154. }
  155. return 0;
  156. }
  157. static int query_formats(AVFilterGraph *graph, AVClass *log_ctx)
  158. {
  159. int i, j, ret;
  160. char filt_args[128];
  161. /* ask all the sub-filters for their supported media formats */
  162. for (i = 0; i < graph->filter_count; i++) {
  163. if (graph->filters[i]->filter->query_formats)
  164. graph->filters[i]->filter->query_formats(graph->filters[i]);
  165. else
  166. avfilter_default_query_formats(graph->filters[i]);
  167. }
  168. /* go through and merge as many format lists as possible */
  169. for (i = 0; i < graph->filter_count; i++) {
  170. AVFilterContext *filter = graph->filters[i];
  171. for (j = 0; j < filter->input_count; j++) {
  172. AVFilterLink *link = filter->inputs[j];
  173. if (!link) continue;
  174. if (!link->in_formats || !link->out_formats)
  175. return AVERROR(EINVAL);
  176. if (link->type == AVMEDIA_TYPE_VIDEO &&
  177. !avfilter_merge_formats(link->in_formats, link->out_formats)) {
  178. /* couldn't merge format lists, auto-insert scale filter */
  179. av_strlcpy(filt_args, "0:0", sizeof(filt_args));
  180. if (graph->scale_sws_opts) {
  181. av_strlcat(filt_args, ":", sizeof(filt_args));
  182. av_strlcat(filt_args, graph->scale_sws_opts, sizeof(filt_args));
  183. }
  184. if (ret = insert_conv_filter(graph, link, "scale", filt_args))
  185. return ret;
  186. }
  187. else if (link->type == AVMEDIA_TYPE_AUDIO) {
  188. if (!link->in_chlayouts || !link->out_chlayouts ||
  189. !link->in_packing || !link->out_packing)
  190. return AVERROR(EINVAL);
  191. if (!avfilter_merge_formats(link->in_formats, link->out_formats) ||
  192. !avfilter_merge_formats(link->in_chlayouts, link->out_chlayouts) ||
  193. !avfilter_merge_formats(link->in_packing, link->out_packing))
  194. if (ret = insert_conv_filter(graph, link, "aconvert", NULL))
  195. return ret;
  196. }
  197. }
  198. }
  199. return 0;
  200. }
  201. static void pick_format(AVFilterLink *link)
  202. {
  203. if (!link || !link->in_formats)
  204. return;
  205. link->in_formats->format_count = 1;
  206. link->format = link->in_formats->formats[0];
  207. avfilter_formats_unref(&link->in_formats);
  208. avfilter_formats_unref(&link->out_formats);
  209. if (link->type == AVMEDIA_TYPE_AUDIO) {
  210. link->in_chlayouts->format_count = 1;
  211. link->channel_layout = link->in_chlayouts->formats[0];
  212. avfilter_formats_unref(&link->in_chlayouts);
  213. avfilter_formats_unref(&link->out_chlayouts);
  214. link->in_packing->format_count = 1;
  215. link->planar = link->in_packing->formats[0] == AVFILTER_PLANAR;
  216. avfilter_formats_unref(&link->in_packing);
  217. avfilter_formats_unref(&link->out_packing);
  218. }
  219. }
  220. static void pick_formats(AVFilterGraph *graph)
  221. {
  222. int i, j;
  223. for (i = 0; i < graph->filter_count; i++) {
  224. AVFilterContext *filter = graph->filters[i];
  225. for (j = 0; j < filter->input_count; j++)
  226. pick_format(filter->inputs[j]);
  227. for (j = 0; j < filter->output_count; j++)
  228. pick_format(filter->outputs[j]);
  229. }
  230. }
  231. int ff_avfilter_graph_config_formats(AVFilterGraph *graph, AVClass *log_ctx)
  232. {
  233. int ret;
  234. /* find supported formats from sub-filters, and merge along links */
  235. if ((ret = query_formats(graph, log_ctx)) < 0)
  236. return ret;
  237. /* Once everything is merged, it's possible that we'll still have
  238. * multiple valid media format choices. We pick the first one. */
  239. pick_formats(graph);
  240. return 0;
  241. }
  242. int avfilter_graph_config(AVFilterGraph *graphctx, void *log_ctx)
  243. {
  244. int ret;
  245. if ((ret = ff_avfilter_graph_check_validity(graphctx, log_ctx)))
  246. return ret;
  247. if ((ret = ff_avfilter_graph_config_formats(graphctx, log_ctx)))
  248. return ret;
  249. if ((ret = ff_avfilter_graph_config_links(graphctx, log_ctx)))
  250. return ret;
  251. return 0;
  252. }
  253. int avfilter_graph_send_command(AVFilterGraph *graph, const char *target, const char *cmd, const char *arg, char *res, int res_len, int flags)
  254. {
  255. int i, r = AVERROR(ENOSYS);
  256. if(!graph)
  257. return r;
  258. if((flags & AVFILTER_CMD_FLAG_ONE) && !(flags & AVFILTER_CMD_FLAG_FAST)) {
  259. r=avfilter_graph_send_command(graph, target, cmd, arg, res, res_len, flags | AVFILTER_CMD_FLAG_FAST);
  260. if(r != AVERROR(ENOSYS))
  261. return r;
  262. }
  263. if(res_len && res)
  264. res[0]= 0;
  265. for (i = 0; i < graph->filter_count; i++) {
  266. AVFilterContext *filter = graph->filters[i];
  267. if(!strcmp(target, "all") || (filter->name && !strcmp(target, filter->name)) || !strcmp(target, filter->filter->name)){
  268. r = avfilter_process_command(filter, cmd, arg, res, res_len, flags);
  269. if(r != AVERROR(ENOSYS)) {
  270. if((flags & AVFILTER_CMD_FLAG_ONE) || r<0)
  271. return r;
  272. }
  273. }
  274. }
  275. return r;
  276. }
  277. int avfilter_graph_queue_command(AVFilterGraph *graph, const char *target, const char *command, const char *arg, int flags, double ts)
  278. {
  279. int i;
  280. if(!graph)
  281. return 0;
  282. for (i = 0; i < graph->filter_count; i++) {
  283. AVFilterContext *filter = graph->filters[i];
  284. if(filter && (!strcmp(target, "all") || !strcmp(target, filter->name) || !strcmp(target, filter->filter->name))){
  285. AVFilterCommand **que = &filter->command_queue, *next;
  286. while(*que && (*que)->time <= ts)
  287. que = &(*que)->next;
  288. next= *que;
  289. *que= av_mallocz(sizeof(AVFilterCommand));
  290. (*que)->command = av_strdup(command);
  291. (*que)->arg = av_strdup(arg);
  292. (*que)->time = ts;
  293. (*que)->flags = flags;
  294. (*que)->next = next;
  295. if(flags & AVFILTER_CMD_FLAG_ONE)
  296. return 0;
  297. }
  298. }
  299. return 0;
  300. }