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.

424 lines
13KB

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