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.

518 lines
19KB

  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 "formats.h"
  27. #include "internal.h"
  28. #include "libavutil/audioconvert.h"
  29. #include "libavutil/log.h"
  30. static const AVClass filtergraph_class = {
  31. .class_name = "AVFilterGraph",
  32. .item_name = av_default_item_name,
  33. .version = LIBAVUTIL_VERSION_INT,
  34. };
  35. AVFilterGraph *avfilter_graph_alloc(void)
  36. {
  37. AVFilterGraph *ret = av_mallocz(sizeof(AVFilterGraph));
  38. if (!ret)
  39. return NULL;
  40. #if FF_API_GRAPH_AVCLASS
  41. ret->av_class = &filtergraph_class;
  42. #endif
  43. return ret;
  44. }
  45. void avfilter_graph_free(AVFilterGraph **graph)
  46. {
  47. if (!*graph)
  48. return;
  49. for (; (*graph)->filter_count > 0; (*graph)->filter_count--)
  50. avfilter_free((*graph)->filters[(*graph)->filter_count - 1]);
  51. av_freep(&(*graph)->scale_sws_opts);
  52. av_freep(&(*graph)->filters);
  53. av_freep(graph);
  54. }
  55. int avfilter_graph_add_filter(AVFilterGraph *graph, AVFilterContext *filter)
  56. {
  57. AVFilterContext **filters = av_realloc(graph->filters,
  58. sizeof(AVFilterContext*) * (graph->filter_count+1));
  59. if (!filters)
  60. return AVERROR(ENOMEM);
  61. graph->filters = filters;
  62. graph->filters[graph->filter_count++] = filter;
  63. return 0;
  64. }
  65. int avfilter_graph_create_filter(AVFilterContext **filt_ctx, AVFilter *filt,
  66. const char *name, const char *args, void *opaque,
  67. AVFilterGraph *graph_ctx)
  68. {
  69. int ret;
  70. if ((ret = avfilter_open(filt_ctx, filt, name)) < 0)
  71. goto fail;
  72. if ((ret = avfilter_init_filter(*filt_ctx, args, opaque)) < 0)
  73. goto fail;
  74. if ((ret = avfilter_graph_add_filter(graph_ctx, *filt_ctx)) < 0)
  75. goto fail;
  76. return 0;
  77. fail:
  78. if (*filt_ctx)
  79. avfilter_free(*filt_ctx);
  80. *filt_ctx = NULL;
  81. return ret;
  82. }
  83. int ff_avfilter_graph_check_validity(AVFilterGraph *graph, AVClass *log_ctx)
  84. {
  85. AVFilterContext *filt;
  86. int i, j;
  87. for (i = 0; i < graph->filter_count; i++) {
  88. filt = graph->filters[i];
  89. for (j = 0; j < filt->input_count; j++) {
  90. if (!filt->inputs[j] || !filt->inputs[j]->src) {
  91. av_log(log_ctx, AV_LOG_ERROR,
  92. "Input pad \"%s\" for the filter \"%s\" of type \"%s\" not connected to any source\n",
  93. filt->input_pads[j].name, filt->name, filt->filter->name);
  94. return AVERROR(EINVAL);
  95. }
  96. }
  97. for (j = 0; j < filt->output_count; j++) {
  98. if (!filt->outputs[j] || !filt->outputs[j]->dst) {
  99. av_log(log_ctx, AV_LOG_ERROR,
  100. "Output pad \"%s\" for the filter \"%s\" of type \"%s\" not connected to any destination\n",
  101. filt->output_pads[j].name, filt->name, filt->filter->name);
  102. return AVERROR(EINVAL);
  103. }
  104. }
  105. }
  106. return 0;
  107. }
  108. int ff_avfilter_graph_config_links(AVFilterGraph *graph, AVClass *log_ctx)
  109. {
  110. AVFilterContext *filt;
  111. int i, ret;
  112. for (i=0; i < graph->filter_count; i++) {
  113. filt = graph->filters[i];
  114. if (!filt->output_count) {
  115. if ((ret = avfilter_config_links(filt)))
  116. return ret;
  117. }
  118. }
  119. return 0;
  120. }
  121. AVFilterContext *avfilter_graph_get_filter(AVFilterGraph *graph, char *name)
  122. {
  123. int i;
  124. for (i = 0; i < graph->filter_count; i++)
  125. if (graph->filters[i]->name && !strcmp(name, graph->filters[i]->name))
  126. return graph->filters[i];
  127. return NULL;
  128. }
  129. static int query_formats(AVFilterGraph *graph, AVClass *log_ctx)
  130. {
  131. int i, j, ret;
  132. int scaler_count = 0, resampler_count = 0;
  133. /* ask all the sub-filters for their supported media formats */
  134. for (i = 0; i < graph->filter_count; i++) {
  135. if (graph->filters[i]->filter->query_formats)
  136. graph->filters[i]->filter->query_formats(graph->filters[i]);
  137. else
  138. avfilter_default_query_formats(graph->filters[i]);
  139. }
  140. /* go through and merge as many format lists as possible */
  141. for (i = 0; i < graph->filter_count; i++) {
  142. AVFilterContext *filter = graph->filters[i];
  143. for (j = 0; j < filter->input_count; j++) {
  144. AVFilterLink *link = filter->inputs[j];
  145. int convert_needed = 0;
  146. if (!link)
  147. continue;
  148. if (link->in_formats != link->out_formats &&
  149. !avfilter_merge_formats(link->in_formats,
  150. link->out_formats))
  151. convert_needed = 1;
  152. if (link->type == AVMEDIA_TYPE_AUDIO) {
  153. if (link->in_channel_layouts != link->out_channel_layouts &&
  154. !ff_merge_channel_layouts(link->in_channel_layouts,
  155. link->out_channel_layouts))
  156. convert_needed = 1;
  157. if (link->in_samplerates != link->out_samplerates &&
  158. !ff_merge_samplerates(link->in_samplerates,
  159. link->out_samplerates))
  160. convert_needed = 1;
  161. }
  162. if (convert_needed) {
  163. AVFilterContext *convert;
  164. AVFilter *filter;
  165. AVFilterLink *inlink, *outlink;
  166. char scale_args[256];
  167. char inst_name[30];
  168. /* couldn't merge format lists. auto-insert conversion filter */
  169. switch (link->type) {
  170. case AVMEDIA_TYPE_VIDEO:
  171. snprintf(inst_name, sizeof(inst_name), "auto-inserted scaler %d",
  172. scaler_count++);
  173. snprintf(scale_args, sizeof(scale_args), "0:0:%s", graph->scale_sws_opts);
  174. if ((ret = avfilter_graph_create_filter(&convert,
  175. avfilter_get_by_name("scale"),
  176. inst_name, scale_args, NULL,
  177. graph)) < 0)
  178. return ret;
  179. break;
  180. case AVMEDIA_TYPE_AUDIO:
  181. if (!(filter = avfilter_get_by_name("resample"))) {
  182. av_log(log_ctx, AV_LOG_ERROR, "'resample' filter "
  183. "not present, cannot convert audio formats.\n");
  184. return AVERROR(EINVAL);
  185. }
  186. snprintf(inst_name, sizeof(inst_name), "auto-inserted resampler %d",
  187. resampler_count++);
  188. if ((ret = avfilter_graph_create_filter(&convert,
  189. avfilter_get_by_name("resample"),
  190. inst_name, NULL, NULL, graph)) < 0)
  191. return ret;
  192. break;
  193. default:
  194. return AVERROR(EINVAL);
  195. }
  196. if ((ret = avfilter_insert_filter(link, convert, 0, 0)) < 0)
  197. return ret;
  198. convert->filter->query_formats(convert);
  199. inlink = convert->inputs[0];
  200. outlink = convert->outputs[0];
  201. if (!avfilter_merge_formats( inlink->in_formats, inlink->out_formats) ||
  202. !avfilter_merge_formats(outlink->in_formats, outlink->out_formats))
  203. ret |= AVERROR(ENOSYS);
  204. if (inlink->type == AVMEDIA_TYPE_AUDIO &&
  205. (!ff_merge_samplerates(inlink->in_samplerates,
  206. inlink->out_samplerates) ||
  207. !ff_merge_channel_layouts(inlink->in_channel_layouts,
  208. inlink->out_channel_layouts)))
  209. ret |= AVERROR(ENOSYS);
  210. if (outlink->type == AVMEDIA_TYPE_AUDIO &&
  211. (!ff_merge_samplerates(outlink->in_samplerates,
  212. outlink->out_samplerates) ||
  213. !ff_merge_channel_layouts(outlink->in_channel_layouts,
  214. outlink->out_channel_layouts)))
  215. ret |= AVERROR(ENOSYS);
  216. if (ret < 0) {
  217. av_log(log_ctx, AV_LOG_ERROR,
  218. "Impossible to convert between the formats supported by the filter "
  219. "'%s' and the filter '%s'\n", link->src->name, link->dst->name);
  220. return ret;
  221. }
  222. }
  223. }
  224. }
  225. return 0;
  226. }
  227. static int pick_format(AVFilterLink *link)
  228. {
  229. if (!link || !link->in_formats)
  230. return 0;
  231. link->in_formats->format_count = 1;
  232. link->format = link->in_formats->formats[0];
  233. if (link->type == AVMEDIA_TYPE_AUDIO) {
  234. if (!link->in_samplerates->format_count) {
  235. av_log(link->src, AV_LOG_ERROR, "Cannot select sample rate for"
  236. " the link between filters %s and %s.\n", link->src->name,
  237. link->dst->name);
  238. return AVERROR(EINVAL);
  239. }
  240. link->in_samplerates->format_count = 1;
  241. link->sample_rate = link->in_samplerates->formats[0];
  242. if (!link->in_channel_layouts->nb_channel_layouts) {
  243. av_log(link->src, AV_LOG_ERROR, "Cannot select channel layout for"
  244. "the link between filters %s and %s.\n", link->src->name,
  245. link->dst->name);
  246. return AVERROR(EINVAL);
  247. }
  248. link->in_channel_layouts->nb_channel_layouts = 1;
  249. link->channel_layout = link->in_channel_layouts->channel_layouts[0];
  250. }
  251. avfilter_formats_unref(&link->in_formats);
  252. avfilter_formats_unref(&link->out_formats);
  253. avfilter_formats_unref(&link->in_samplerates);
  254. avfilter_formats_unref(&link->out_samplerates);
  255. ff_channel_layouts_unref(&link->in_channel_layouts);
  256. ff_channel_layouts_unref(&link->out_channel_layouts);
  257. return 0;
  258. }
  259. #define REDUCE_FORMATS(fmt_type, list_type, list, var, nb, add_format) \
  260. do { \
  261. for (i = 0; i < filter->input_count; i++) { \
  262. AVFilterLink *link = filter->inputs[i]; \
  263. fmt_type fmt; \
  264. \
  265. if (!link->out_ ## list || link->out_ ## list->nb != 1) \
  266. continue; \
  267. fmt = link->out_ ## list->var[0]; \
  268. \
  269. for (j = 0; j < filter->output_count; j++) { \
  270. AVFilterLink *out_link = filter->outputs[j]; \
  271. list_type *fmts; \
  272. \
  273. if (link->type != out_link->type || \
  274. out_link->in_ ## list->nb == 1) \
  275. continue; \
  276. fmts = out_link->in_ ## list; \
  277. \
  278. if (!out_link->in_ ## list->nb) { \
  279. add_format(&out_link->in_ ##list, fmt); \
  280. break; \
  281. } \
  282. \
  283. for (k = 0; k < out_link->in_ ## list->nb; k++) \
  284. if (fmts->var[k] == fmt) { \
  285. fmts->var[0] = fmt; \
  286. fmts->nb = 1; \
  287. ret = 1; \
  288. break; \
  289. } \
  290. } \
  291. } \
  292. } while (0)
  293. static int reduce_formats_on_filter(AVFilterContext *filter)
  294. {
  295. int i, j, k, ret = 0;
  296. REDUCE_FORMATS(int, AVFilterFormats, formats, formats,
  297. format_count, avfilter_add_format);
  298. REDUCE_FORMATS(int, AVFilterFormats, samplerates, formats,
  299. format_count, avfilter_add_format);
  300. REDUCE_FORMATS(uint64_t, AVFilterChannelLayouts, channel_layouts,
  301. channel_layouts, nb_channel_layouts, ff_add_channel_layout);
  302. return ret;
  303. }
  304. static void reduce_formats(AVFilterGraph *graph)
  305. {
  306. int i, reduced;
  307. do {
  308. reduced = 0;
  309. for (i = 0; i < graph->filter_count; i++)
  310. reduced |= reduce_formats_on_filter(graph->filters[i]);
  311. } while (reduced);
  312. }
  313. static void swap_samplerates_on_filter(AVFilterContext *filter)
  314. {
  315. AVFilterLink *link = NULL;
  316. int sample_rate;
  317. int i, j;
  318. for (i = 0; i < filter->input_count; i++) {
  319. link = filter->inputs[i];
  320. if (link->type == AVMEDIA_TYPE_AUDIO &&
  321. link->out_samplerates->format_count == 1)
  322. break;
  323. }
  324. if (i == filter->input_count)
  325. return;
  326. sample_rate = link->out_samplerates->formats[0];
  327. for (i = 0; i < filter->output_count; i++) {
  328. AVFilterLink *outlink = filter->outputs[i];
  329. int best_idx, best_diff = INT_MAX;
  330. if (outlink->type != AVMEDIA_TYPE_AUDIO ||
  331. outlink->in_samplerates->format_count < 2)
  332. continue;
  333. for (j = 0; j < outlink->in_samplerates->format_count; j++) {
  334. int diff = abs(sample_rate - outlink->in_samplerates->formats[j]);
  335. if (diff < best_diff) {
  336. best_diff = diff;
  337. best_idx = j;
  338. }
  339. }
  340. FFSWAP(int, outlink->in_samplerates->formats[0],
  341. outlink->in_samplerates->formats[best_idx]);
  342. }
  343. }
  344. static void swap_samplerates(AVFilterGraph *graph)
  345. {
  346. int i;
  347. for (i = 0; i < graph->filter_count; i++)
  348. swap_samplerates_on_filter(graph->filters[i]);
  349. }
  350. static void swap_channel_layouts_on_filter(AVFilterContext *filter)
  351. {
  352. AVFilterLink *link = NULL;
  353. uint64_t chlayout;
  354. int i, j;
  355. for (i = 0; i < filter->input_count; i++) {
  356. link = filter->inputs[i];
  357. if (link->type == AVMEDIA_TYPE_AUDIO &&
  358. link->out_channel_layouts->nb_channel_layouts == 1)
  359. break;
  360. }
  361. if (i == filter->input_count)
  362. return;
  363. chlayout = link->out_channel_layouts->channel_layouts[0];
  364. for (i = 0; i < filter->output_count; i++) {
  365. AVFilterLink *outlink = filter->outputs[i];
  366. int best_idx, best_score = INT_MIN;
  367. if (outlink->type != AVMEDIA_TYPE_AUDIO ||
  368. outlink->in_channel_layouts->nb_channel_layouts < 2)
  369. continue;
  370. for (j = 0; j < outlink->in_channel_layouts->nb_channel_layouts; j++) {
  371. uint64_t out_chlayout = outlink->in_channel_layouts->channel_layouts[j];
  372. int matched_channels = av_get_channel_layout_nb_channels(chlayout &
  373. out_chlayout);
  374. int extra_channels = av_get_channel_layout_nb_channels(out_chlayout &
  375. (~chlayout));
  376. int score = matched_channels - extra_channels;
  377. if (score > best_score) {
  378. best_score = score;
  379. best_idx = j;
  380. }
  381. }
  382. FFSWAP(uint64_t, outlink->in_channel_layouts->channel_layouts[0],
  383. outlink->in_channel_layouts->channel_layouts[best_idx]);
  384. }
  385. }
  386. static void swap_channel_layouts(AVFilterGraph *graph)
  387. {
  388. int i;
  389. for (i = 0; i < graph->filter_count; i++)
  390. swap_channel_layouts_on_filter(graph->filters[i]);
  391. }
  392. static int pick_formats(AVFilterGraph *graph)
  393. {
  394. int i, j, ret;
  395. for (i = 0; i < graph->filter_count; i++) {
  396. AVFilterContext *filter = graph->filters[i];
  397. for (j = 0; j < filter->input_count; j++)
  398. if ((ret = pick_format(filter->inputs[j])) < 0)
  399. return ret;
  400. for (j = 0; j < filter->output_count; j++)
  401. if ((ret = pick_format(filter->outputs[j])) < 0)
  402. return ret;
  403. }
  404. return 0;
  405. }
  406. int ff_avfilter_graph_config_formats(AVFilterGraph *graph, AVClass *log_ctx)
  407. {
  408. int ret;
  409. /* find supported formats from sub-filters, and merge along links */
  410. if ((ret = query_formats(graph, log_ctx)) < 0)
  411. return ret;
  412. /* Once everything is merged, it's possible that we'll still have
  413. * multiple valid media format choices. We try to minimize the amount
  414. * of format conversion inside filters */
  415. reduce_formats(graph);
  416. /* for audio filters, ensure the best sample rate and channel layout
  417. * is selected */
  418. swap_samplerates(graph);
  419. swap_channel_layouts(graph);
  420. if ((ret = pick_formats(graph)) < 0)
  421. return ret;
  422. return 0;
  423. }
  424. int avfilter_graph_config(AVFilterGraph *graphctx, void *log_ctx)
  425. {
  426. int ret;
  427. if ((ret = ff_avfilter_graph_check_validity(graphctx, log_ctx)))
  428. return ret;
  429. if ((ret = ff_avfilter_graph_config_formats(graphctx, log_ctx)))
  430. return ret;
  431. if ((ret = ff_avfilter_graph_config_links(graphctx, log_ctx)))
  432. return ret;
  433. return 0;
  434. }