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.

709 lines
25KB

  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 "libavutil/avassert.h"
  25. #include "libavutil/channel_layout.h"
  26. #include "libavutil/common.h"
  27. #include "libavutil/log.h"
  28. #include "avfilter.h"
  29. #include "avfiltergraph.h"
  30. #include "formats.h"
  31. #include "internal.h"
  32. static const AVClass filtergraph_class = {
  33. .class_name = "AVFilterGraph",
  34. .item_name = av_default_item_name,
  35. .version = LIBAVUTIL_VERSION_INT,
  36. };
  37. AVFilterGraph *avfilter_graph_alloc(void)
  38. {
  39. AVFilterGraph *ret = av_mallocz(sizeof(AVFilterGraph));
  40. if (!ret)
  41. return NULL;
  42. ret->av_class = &filtergraph_class;
  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. /**
  84. * Check for the validity of graph.
  85. *
  86. * A graph is considered valid if all its input and output pads are
  87. * connected.
  88. *
  89. * @return 0 in case of success, a negative value otherwise
  90. */
  91. static int graph_check_validity(AVFilterGraph *graph, AVClass *log_ctx)
  92. {
  93. AVFilterContext *filt;
  94. int i, j;
  95. for (i = 0; i < graph->filter_count; i++) {
  96. filt = graph->filters[i];
  97. for (j = 0; j < filt->nb_inputs; j++) {
  98. if (!filt->inputs[j] || !filt->inputs[j]->src) {
  99. av_log(log_ctx, AV_LOG_ERROR,
  100. "Input pad \"%s\" for the filter \"%s\" of type \"%s\" not connected to any source\n",
  101. filt->input_pads[j].name, filt->name, filt->filter->name);
  102. return AVERROR(EINVAL);
  103. }
  104. }
  105. for (j = 0; j < filt->nb_outputs; j++) {
  106. if (!filt->outputs[j] || !filt->outputs[j]->dst) {
  107. av_log(log_ctx, AV_LOG_ERROR,
  108. "Output pad \"%s\" for the filter \"%s\" of type \"%s\" not connected to any destination\n",
  109. filt->output_pads[j].name, filt->name, filt->filter->name);
  110. return AVERROR(EINVAL);
  111. }
  112. }
  113. }
  114. return 0;
  115. }
  116. /**
  117. * Configure all the links of graphctx.
  118. *
  119. * @return 0 in case of success, a negative value otherwise
  120. */
  121. static int graph_config_links(AVFilterGraph *graph, AVClass *log_ctx)
  122. {
  123. AVFilterContext *filt;
  124. int i, ret;
  125. for (i=0; i < graph->filter_count; i++) {
  126. filt = graph->filters[i];
  127. if (!filt->nb_outputs) {
  128. if ((ret = avfilter_config_links(filt)))
  129. return ret;
  130. }
  131. }
  132. return 0;
  133. }
  134. AVFilterContext *avfilter_graph_get_filter(AVFilterGraph *graph, char *name)
  135. {
  136. int i;
  137. for (i = 0; i < graph->filter_count; i++)
  138. if (graph->filters[i]->name && !strcmp(name, graph->filters[i]->name))
  139. return graph->filters[i];
  140. return NULL;
  141. }
  142. static int query_formats(AVFilterGraph *graph, AVClass *log_ctx)
  143. {
  144. int i, j, ret;
  145. int scaler_count = 0, resampler_count = 0;
  146. /* ask all the sub-filters for their supported media formats */
  147. for (i = 0; i < graph->filter_count; i++) {
  148. if (graph->filters[i]->filter->query_formats)
  149. graph->filters[i]->filter->query_formats(graph->filters[i]);
  150. else
  151. ff_default_query_formats(graph->filters[i]);
  152. }
  153. /* go through and merge as many format lists as possible */
  154. for (i = 0; i < graph->filter_count; i++) {
  155. AVFilterContext *filter = graph->filters[i];
  156. for (j = 0; j < filter->nb_inputs; j++) {
  157. AVFilterLink *link = filter->inputs[j];
  158. int convert_needed = 0;
  159. if (!link)
  160. continue;
  161. if (link->in_formats != link->out_formats &&
  162. !ff_merge_formats(link->in_formats,
  163. link->out_formats))
  164. convert_needed = 1;
  165. if (link->type == AVMEDIA_TYPE_AUDIO) {
  166. if (link->in_channel_layouts != link->out_channel_layouts &&
  167. !ff_merge_channel_layouts(link->in_channel_layouts,
  168. link->out_channel_layouts))
  169. convert_needed = 1;
  170. if (link->in_samplerates != link->out_samplerates &&
  171. !ff_merge_samplerates(link->in_samplerates,
  172. link->out_samplerates))
  173. convert_needed = 1;
  174. }
  175. if (convert_needed) {
  176. AVFilterContext *convert;
  177. AVFilter *filter;
  178. AVFilterLink *inlink, *outlink;
  179. char scale_args[256];
  180. char inst_name[30];
  181. /* couldn't merge format lists. auto-insert conversion filter */
  182. switch (link->type) {
  183. case AVMEDIA_TYPE_VIDEO:
  184. if (!(filter = avfilter_get_by_name("scale"))) {
  185. av_log(log_ctx, AV_LOG_ERROR, "'scale' filter "
  186. "not present, cannot convert pixel formats.\n");
  187. return AVERROR(EINVAL);
  188. }
  189. snprintf(inst_name, sizeof(inst_name), "auto-inserted scaler %d",
  190. scaler_count++);
  191. snprintf(scale_args, sizeof(scale_args), "0:0:%s", graph->scale_sws_opts);
  192. if ((ret = avfilter_graph_create_filter(&convert, filter,
  193. inst_name, scale_args, NULL,
  194. graph)) < 0)
  195. return ret;
  196. break;
  197. case AVMEDIA_TYPE_AUDIO:
  198. if (!(filter = avfilter_get_by_name("resample"))) {
  199. av_log(log_ctx, AV_LOG_ERROR, "'resample' filter "
  200. "not present, cannot convert audio formats.\n");
  201. return AVERROR(EINVAL);
  202. }
  203. snprintf(inst_name, sizeof(inst_name), "auto-inserted resampler %d",
  204. resampler_count++);
  205. if ((ret = avfilter_graph_create_filter(&convert, filter,
  206. inst_name, NULL, NULL, graph)) < 0)
  207. return ret;
  208. break;
  209. default:
  210. return AVERROR(EINVAL);
  211. }
  212. if ((ret = avfilter_insert_filter(link, convert, 0, 0)) < 0)
  213. return ret;
  214. convert->filter->query_formats(convert);
  215. inlink = convert->inputs[0];
  216. outlink = convert->outputs[0];
  217. if (!ff_merge_formats( inlink->in_formats, inlink->out_formats) ||
  218. !ff_merge_formats(outlink->in_formats, outlink->out_formats))
  219. ret |= AVERROR(ENOSYS);
  220. if (inlink->type == AVMEDIA_TYPE_AUDIO &&
  221. (!ff_merge_samplerates(inlink->in_samplerates,
  222. inlink->out_samplerates) ||
  223. !ff_merge_channel_layouts(inlink->in_channel_layouts,
  224. inlink->out_channel_layouts)))
  225. ret |= AVERROR(ENOSYS);
  226. if (outlink->type == AVMEDIA_TYPE_AUDIO &&
  227. (!ff_merge_samplerates(outlink->in_samplerates,
  228. outlink->out_samplerates) ||
  229. !ff_merge_channel_layouts(outlink->in_channel_layouts,
  230. outlink->out_channel_layouts)))
  231. ret |= AVERROR(ENOSYS);
  232. if (ret < 0) {
  233. av_log(log_ctx, AV_LOG_ERROR,
  234. "Impossible to convert between the formats supported by the filter "
  235. "'%s' and the filter '%s'\n", link->src->name, link->dst->name);
  236. return ret;
  237. }
  238. }
  239. }
  240. }
  241. return 0;
  242. }
  243. static int pick_format(AVFilterLink *link)
  244. {
  245. if (!link || !link->in_formats)
  246. return 0;
  247. link->in_formats->format_count = 1;
  248. link->format = link->in_formats->formats[0];
  249. if (link->type == AVMEDIA_TYPE_AUDIO) {
  250. if (!link->in_samplerates->format_count) {
  251. av_log(link->src, AV_LOG_ERROR, "Cannot select sample rate for"
  252. " the link between filters %s and %s.\n", link->src->name,
  253. link->dst->name);
  254. return AVERROR(EINVAL);
  255. }
  256. link->in_samplerates->format_count = 1;
  257. link->sample_rate = link->in_samplerates->formats[0];
  258. if (!link->in_channel_layouts->nb_channel_layouts) {
  259. av_log(link->src, AV_LOG_ERROR, "Cannot select channel layout for"
  260. "the link between filters %s and %s.\n", link->src->name,
  261. link->dst->name);
  262. return AVERROR(EINVAL);
  263. }
  264. link->in_channel_layouts->nb_channel_layouts = 1;
  265. link->channel_layout = link->in_channel_layouts->channel_layouts[0];
  266. }
  267. ff_formats_unref(&link->in_formats);
  268. ff_formats_unref(&link->out_formats);
  269. ff_formats_unref(&link->in_samplerates);
  270. ff_formats_unref(&link->out_samplerates);
  271. ff_channel_layouts_unref(&link->in_channel_layouts);
  272. ff_channel_layouts_unref(&link->out_channel_layouts);
  273. return 0;
  274. }
  275. #define REDUCE_FORMATS(fmt_type, list_type, list, var, nb, add_format) \
  276. do { \
  277. for (i = 0; i < filter->nb_inputs; i++) { \
  278. AVFilterLink *link = filter->inputs[i]; \
  279. fmt_type fmt; \
  280. \
  281. if (!link->out_ ## list || link->out_ ## list->nb != 1) \
  282. continue; \
  283. fmt = link->out_ ## list->var[0]; \
  284. \
  285. for (j = 0; j < filter->nb_outputs; j++) { \
  286. AVFilterLink *out_link = filter->outputs[j]; \
  287. list_type *fmts; \
  288. \
  289. if (link->type != out_link->type || \
  290. out_link->in_ ## list->nb == 1) \
  291. continue; \
  292. fmts = out_link->in_ ## list; \
  293. \
  294. if (!out_link->in_ ## list->nb) { \
  295. add_format(&out_link->in_ ##list, fmt); \
  296. break; \
  297. } \
  298. \
  299. for (k = 0; k < out_link->in_ ## list->nb; k++) \
  300. if (fmts->var[k] == fmt) { \
  301. fmts->var[0] = fmt; \
  302. fmts->nb = 1; \
  303. ret = 1; \
  304. break; \
  305. } \
  306. } \
  307. } \
  308. } while (0)
  309. static int reduce_formats_on_filter(AVFilterContext *filter)
  310. {
  311. int i, j, k, ret = 0;
  312. REDUCE_FORMATS(int, AVFilterFormats, formats, formats,
  313. format_count, ff_add_format);
  314. REDUCE_FORMATS(int, AVFilterFormats, samplerates, formats,
  315. format_count, ff_add_format);
  316. REDUCE_FORMATS(uint64_t, AVFilterChannelLayouts, channel_layouts,
  317. channel_layouts, nb_channel_layouts, ff_add_channel_layout);
  318. return ret;
  319. }
  320. static void reduce_formats(AVFilterGraph *graph)
  321. {
  322. int i, reduced;
  323. do {
  324. reduced = 0;
  325. for (i = 0; i < graph->filter_count; i++)
  326. reduced |= reduce_formats_on_filter(graph->filters[i]);
  327. } while (reduced);
  328. }
  329. static void swap_samplerates_on_filter(AVFilterContext *filter)
  330. {
  331. AVFilterLink *link = NULL;
  332. int sample_rate;
  333. int i, j;
  334. for (i = 0; i < filter->nb_inputs; i++) {
  335. link = filter->inputs[i];
  336. if (link->type == AVMEDIA_TYPE_AUDIO &&
  337. link->out_samplerates->format_count == 1)
  338. break;
  339. }
  340. if (i == filter->nb_inputs)
  341. return;
  342. sample_rate = link->out_samplerates->formats[0];
  343. for (i = 0; i < filter->nb_outputs; i++) {
  344. AVFilterLink *outlink = filter->outputs[i];
  345. int best_idx, best_diff = INT_MAX;
  346. if (outlink->type != AVMEDIA_TYPE_AUDIO ||
  347. outlink->in_samplerates->format_count < 2)
  348. continue;
  349. for (j = 0; j < outlink->in_samplerates->format_count; j++) {
  350. int diff = abs(sample_rate - outlink->in_samplerates->formats[j]);
  351. if (diff < best_diff) {
  352. best_diff = diff;
  353. best_idx = j;
  354. }
  355. }
  356. FFSWAP(int, outlink->in_samplerates->formats[0],
  357. outlink->in_samplerates->formats[best_idx]);
  358. }
  359. }
  360. static void swap_samplerates(AVFilterGraph *graph)
  361. {
  362. int i;
  363. for (i = 0; i < graph->filter_count; i++)
  364. swap_samplerates_on_filter(graph->filters[i]);
  365. }
  366. #define CH_CENTER_PAIR (AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_RIGHT_OF_CENTER)
  367. #define CH_FRONT_PAIR (AV_CH_FRONT_LEFT | AV_CH_FRONT_RIGHT)
  368. #define CH_STEREO_PAIR (AV_CH_STEREO_LEFT | AV_CH_STEREO_RIGHT)
  369. #define CH_WIDE_PAIR (AV_CH_WIDE_LEFT | AV_CH_WIDE_RIGHT)
  370. #define CH_SIDE_PAIR (AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT)
  371. #define CH_DIRECT_PAIR (AV_CH_SURROUND_DIRECT_LEFT | AV_CH_SURROUND_DIRECT_RIGHT)
  372. #define CH_BACK_PAIR (AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT)
  373. /* allowable substitutions for channel pairs when comparing layouts,
  374. * ordered by priority for both values */
  375. static const uint64_t ch_subst[][2] = {
  376. { CH_FRONT_PAIR, CH_CENTER_PAIR },
  377. { CH_FRONT_PAIR, CH_WIDE_PAIR },
  378. { CH_FRONT_PAIR, AV_CH_FRONT_CENTER },
  379. { CH_CENTER_PAIR, CH_FRONT_PAIR },
  380. { CH_CENTER_PAIR, CH_WIDE_PAIR },
  381. { CH_CENTER_PAIR, AV_CH_FRONT_CENTER },
  382. { CH_WIDE_PAIR, CH_FRONT_PAIR },
  383. { CH_WIDE_PAIR, CH_CENTER_PAIR },
  384. { CH_WIDE_PAIR, AV_CH_FRONT_CENTER },
  385. { AV_CH_FRONT_CENTER, CH_FRONT_PAIR },
  386. { AV_CH_FRONT_CENTER, CH_CENTER_PAIR },
  387. { AV_CH_FRONT_CENTER, CH_WIDE_PAIR },
  388. { CH_SIDE_PAIR, CH_DIRECT_PAIR },
  389. { CH_SIDE_PAIR, CH_BACK_PAIR },
  390. { CH_SIDE_PAIR, AV_CH_BACK_CENTER },
  391. { CH_BACK_PAIR, CH_DIRECT_PAIR },
  392. { CH_BACK_PAIR, CH_SIDE_PAIR },
  393. { CH_BACK_PAIR, AV_CH_BACK_CENTER },
  394. { AV_CH_BACK_CENTER, CH_BACK_PAIR },
  395. { AV_CH_BACK_CENTER, CH_DIRECT_PAIR },
  396. { AV_CH_BACK_CENTER, CH_SIDE_PAIR },
  397. };
  398. static void swap_channel_layouts_on_filter(AVFilterContext *filter)
  399. {
  400. AVFilterLink *link = NULL;
  401. int i, j, k;
  402. for (i = 0; i < filter->nb_inputs; i++) {
  403. link = filter->inputs[i];
  404. if (link->type == AVMEDIA_TYPE_AUDIO &&
  405. link->out_channel_layouts->nb_channel_layouts == 1)
  406. break;
  407. }
  408. if (i == filter->nb_inputs)
  409. return;
  410. for (i = 0; i < filter->nb_outputs; i++) {
  411. AVFilterLink *outlink = filter->outputs[i];
  412. int best_idx = -1, best_score = INT_MIN, best_count_diff = INT_MAX;
  413. if (outlink->type != AVMEDIA_TYPE_AUDIO ||
  414. outlink->in_channel_layouts->nb_channel_layouts < 2)
  415. continue;
  416. for (j = 0; j < outlink->in_channel_layouts->nb_channel_layouts; j++) {
  417. uint64_t in_chlayout = link->out_channel_layouts->channel_layouts[0];
  418. uint64_t out_chlayout = outlink->in_channel_layouts->channel_layouts[j];
  419. int in_channels = av_get_channel_layout_nb_channels(in_chlayout);
  420. int out_channels = av_get_channel_layout_nb_channels(out_chlayout);
  421. int count_diff = out_channels - in_channels;
  422. int matched_channels, extra_channels;
  423. int score = 0;
  424. /* channel substitution */
  425. for (k = 0; k < FF_ARRAY_ELEMS(ch_subst); k++) {
  426. uint64_t cmp0 = ch_subst[k][0];
  427. uint64_t cmp1 = ch_subst[k][1];
  428. if (( in_chlayout & cmp0) && (!(out_chlayout & cmp0)) &&
  429. (out_chlayout & cmp1) && (!( in_chlayout & cmp1))) {
  430. in_chlayout &= ~cmp0;
  431. out_chlayout &= ~cmp1;
  432. /* add score for channel match, minus a deduction for
  433. having to do the substitution */
  434. score += 10 * av_get_channel_layout_nb_channels(cmp1) - 2;
  435. }
  436. }
  437. /* no penalty for LFE channel mismatch */
  438. if ( (in_chlayout & AV_CH_LOW_FREQUENCY) &&
  439. (out_chlayout & AV_CH_LOW_FREQUENCY))
  440. score += 10;
  441. in_chlayout &= ~AV_CH_LOW_FREQUENCY;
  442. out_chlayout &= ~AV_CH_LOW_FREQUENCY;
  443. matched_channels = av_get_channel_layout_nb_channels(in_chlayout &
  444. out_chlayout);
  445. extra_channels = av_get_channel_layout_nb_channels(out_chlayout &
  446. (~in_chlayout));
  447. score += 10 * matched_channels - 5 * extra_channels;
  448. if (score > best_score ||
  449. (count_diff < best_count_diff && score == best_score)) {
  450. best_score = score;
  451. best_idx = j;
  452. best_count_diff = count_diff;
  453. }
  454. }
  455. av_assert0(best_idx >= 0);
  456. FFSWAP(uint64_t, outlink->in_channel_layouts->channel_layouts[0],
  457. outlink->in_channel_layouts->channel_layouts[best_idx]);
  458. }
  459. }
  460. static void swap_channel_layouts(AVFilterGraph *graph)
  461. {
  462. int i;
  463. for (i = 0; i < graph->filter_count; i++)
  464. swap_channel_layouts_on_filter(graph->filters[i]);
  465. }
  466. static void swap_sample_fmts_on_filter(AVFilterContext *filter)
  467. {
  468. AVFilterLink *link = NULL;
  469. int format, bps;
  470. int i, j;
  471. for (i = 0; i < filter->nb_inputs; i++) {
  472. link = filter->inputs[i];
  473. if (link->type == AVMEDIA_TYPE_AUDIO &&
  474. link->out_formats->format_count == 1)
  475. break;
  476. }
  477. if (i == filter->nb_inputs)
  478. return;
  479. format = link->out_formats->formats[0];
  480. bps = av_get_bytes_per_sample(format);
  481. for (i = 0; i < filter->nb_outputs; i++) {
  482. AVFilterLink *outlink = filter->outputs[i];
  483. int best_idx = -1, best_score = INT_MIN;
  484. if (outlink->type != AVMEDIA_TYPE_AUDIO ||
  485. outlink->in_formats->format_count < 2)
  486. continue;
  487. for (j = 0; j < outlink->in_formats->format_count; j++) {
  488. int out_format = outlink->in_formats->formats[j];
  489. int out_bps = av_get_bytes_per_sample(out_format);
  490. int score;
  491. if (av_get_packed_sample_fmt(out_format) == format ||
  492. av_get_planar_sample_fmt(out_format) == format) {
  493. best_idx = j;
  494. break;
  495. }
  496. /* for s32 and float prefer double to prevent loss of information */
  497. if (bps == 4 && out_bps == 8) {
  498. best_idx = j;
  499. break;
  500. }
  501. /* prefer closest higher or equal bps */
  502. score = -abs(out_bps - bps);
  503. if (out_bps >= bps)
  504. score += INT_MAX/2;
  505. if (score > best_score) {
  506. best_score = score;
  507. best_idx = j;
  508. }
  509. }
  510. av_assert0(best_idx >= 0);
  511. FFSWAP(int, outlink->in_formats->formats[0],
  512. outlink->in_formats->formats[best_idx]);
  513. }
  514. }
  515. static void swap_sample_fmts(AVFilterGraph *graph)
  516. {
  517. int i;
  518. for (i = 0; i < graph->filter_count; i++)
  519. swap_sample_fmts_on_filter(graph->filters[i]);
  520. }
  521. static int pick_formats(AVFilterGraph *graph)
  522. {
  523. int i, j, ret;
  524. for (i = 0; i < graph->filter_count; i++) {
  525. AVFilterContext *filter = graph->filters[i];
  526. for (j = 0; j < filter->nb_inputs; j++)
  527. if ((ret = pick_format(filter->inputs[j])) < 0)
  528. return ret;
  529. for (j = 0; j < filter->nb_outputs; j++)
  530. if ((ret = pick_format(filter->outputs[j])) < 0)
  531. return ret;
  532. }
  533. return 0;
  534. }
  535. /**
  536. * Configure the formats of all the links in the graph.
  537. */
  538. static int graph_config_formats(AVFilterGraph *graph, AVClass *log_ctx)
  539. {
  540. int ret;
  541. /* find supported formats from sub-filters, and merge along links */
  542. if ((ret = query_formats(graph, log_ctx)) < 0)
  543. return ret;
  544. /* Once everything is merged, it's possible that we'll still have
  545. * multiple valid media format choices. We try to minimize the amount
  546. * of format conversion inside filters */
  547. reduce_formats(graph);
  548. /* for audio filters, ensure the best format, sample rate and channel layout
  549. * is selected */
  550. swap_sample_fmts(graph);
  551. swap_samplerates(graph);
  552. swap_channel_layouts(graph);
  553. if ((ret = pick_formats(graph)) < 0)
  554. return ret;
  555. return 0;
  556. }
  557. static int graph_insert_fifos(AVFilterGraph *graph, AVClass *log_ctx)
  558. {
  559. AVFilterContext *f;
  560. int i, j, ret;
  561. int fifo_count = 0;
  562. for (i = 0; i < graph->filter_count; i++) {
  563. f = graph->filters[i];
  564. for (j = 0; j < f->nb_inputs; j++) {
  565. AVFilterLink *link = f->inputs[j];
  566. AVFilterContext *fifo_ctx;
  567. AVFilter *fifo;
  568. char name[32];
  569. if (!link->dstpad->needs_fifo)
  570. continue;
  571. fifo = f->inputs[j]->type == AVMEDIA_TYPE_VIDEO ?
  572. avfilter_get_by_name("fifo") :
  573. avfilter_get_by_name("afifo");
  574. snprintf(name, sizeof(name), "auto-inserted fifo %d", fifo_count++);
  575. ret = avfilter_graph_create_filter(&fifo_ctx, fifo, name, NULL,
  576. NULL, graph);
  577. if (ret < 0)
  578. return ret;
  579. ret = avfilter_insert_filter(link, fifo_ctx, 0, 0);
  580. if (ret < 0)
  581. return ret;
  582. }
  583. }
  584. return 0;
  585. }
  586. int avfilter_graph_config(AVFilterGraph *graphctx, void *log_ctx)
  587. {
  588. int ret;
  589. if ((ret = graph_check_validity(graphctx, log_ctx)))
  590. return ret;
  591. if ((ret = graph_insert_fifos(graphctx, log_ctx)) < 0)
  592. return ret;
  593. if ((ret = graph_config_formats(graphctx, log_ctx)))
  594. return ret;
  595. if ((ret = graph_config_links(graphctx, log_ctx)))
  596. return ret;
  597. return 0;
  598. }