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.

714 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 <string.h>
  23. #include "libavutil/avassert.h"
  24. #include "libavutil/channel_layout.h"
  25. #include "libavutil/common.h"
  26. #include "libavutil/log.h"
  27. #include "avfilter.h"
  28. #include "avfiltergraph.h"
  29. #include "formats.h"
  30. #include "internal.h"
  31. static const AVClass filtergraph_class = {
  32. .class_name = "AVFilterGraph",
  33. .item_name = av_default_item_name,
  34. .version = LIBAVUTIL_VERSION_INT,
  35. };
  36. AVFilterGraph *avfilter_graph_alloc(void)
  37. {
  38. AVFilterGraph *ret = av_mallocz(sizeof(*ret));
  39. if (!ret)
  40. return NULL;
  41. ret->av_class = &filtergraph_class;
  42. return ret;
  43. }
  44. void avfilter_graph_free(AVFilterGraph **graph)
  45. {
  46. if (!*graph)
  47. return;
  48. for (; (*graph)->nb_filters > 0; (*graph)->nb_filters--)
  49. avfilter_free((*graph)->filters[(*graph)->nb_filters - 1]);
  50. av_freep(&(*graph)->scale_sws_opts);
  51. av_freep(&(*graph)->resample_lavr_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(*filters) * (graph->nb_filters + 1));
  59. if (!filters)
  60. return AVERROR(ENOMEM);
  61. graph->filters = filters;
  62. graph->filters[graph->nb_filters++] = 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->nb_filters; 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->nb_filters; 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->nb_filters; 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->nb_filters; 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->nb_filters; 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. scale_args[0] = '\0';
  206. if (graph->resample_lavr_opts)
  207. snprintf(scale_args, sizeof(scale_args), "%s",
  208. graph->resample_lavr_opts);
  209. if ((ret = avfilter_graph_create_filter(&convert, filter,
  210. inst_name, scale_args,
  211. NULL, graph)) < 0)
  212. return ret;
  213. break;
  214. default:
  215. return AVERROR(EINVAL);
  216. }
  217. if ((ret = avfilter_insert_filter(link, convert, 0, 0)) < 0)
  218. return ret;
  219. convert->filter->query_formats(convert);
  220. inlink = convert->inputs[0];
  221. outlink = convert->outputs[0];
  222. if (!ff_merge_formats( inlink->in_formats, inlink->out_formats) ||
  223. !ff_merge_formats(outlink->in_formats, outlink->out_formats))
  224. ret |= AVERROR(ENOSYS);
  225. if (inlink->type == AVMEDIA_TYPE_AUDIO &&
  226. (!ff_merge_samplerates(inlink->in_samplerates,
  227. inlink->out_samplerates) ||
  228. !ff_merge_channel_layouts(inlink->in_channel_layouts,
  229. inlink->out_channel_layouts)))
  230. ret |= AVERROR(ENOSYS);
  231. if (outlink->type == AVMEDIA_TYPE_AUDIO &&
  232. (!ff_merge_samplerates(outlink->in_samplerates,
  233. outlink->out_samplerates) ||
  234. !ff_merge_channel_layouts(outlink->in_channel_layouts,
  235. outlink->out_channel_layouts)))
  236. ret |= AVERROR(ENOSYS);
  237. if (ret < 0) {
  238. av_log(log_ctx, AV_LOG_ERROR,
  239. "Impossible to convert between the formats supported by the filter "
  240. "'%s' and the filter '%s'\n", link->src->name, link->dst->name);
  241. return ret;
  242. }
  243. }
  244. }
  245. }
  246. return 0;
  247. }
  248. static int pick_format(AVFilterLink *link)
  249. {
  250. if (!link || !link->in_formats)
  251. return 0;
  252. link->in_formats->format_count = 1;
  253. link->format = link->in_formats->formats[0];
  254. if (link->type == AVMEDIA_TYPE_AUDIO) {
  255. if (!link->in_samplerates->format_count) {
  256. av_log(link->src, AV_LOG_ERROR, "Cannot select sample rate for"
  257. " the link between filters %s and %s.\n", link->src->name,
  258. link->dst->name);
  259. return AVERROR(EINVAL);
  260. }
  261. link->in_samplerates->format_count = 1;
  262. link->sample_rate = link->in_samplerates->formats[0];
  263. if (!link->in_channel_layouts->nb_channel_layouts) {
  264. av_log(link->src, AV_LOG_ERROR, "Cannot select channel layout for"
  265. "the link between filters %s and %s.\n", link->src->name,
  266. link->dst->name);
  267. return AVERROR(EINVAL);
  268. }
  269. link->in_channel_layouts->nb_channel_layouts = 1;
  270. link->channel_layout = link->in_channel_layouts->channel_layouts[0];
  271. }
  272. ff_formats_unref(&link->in_formats);
  273. ff_formats_unref(&link->out_formats);
  274. ff_formats_unref(&link->in_samplerates);
  275. ff_formats_unref(&link->out_samplerates);
  276. ff_channel_layouts_unref(&link->in_channel_layouts);
  277. ff_channel_layouts_unref(&link->out_channel_layouts);
  278. return 0;
  279. }
  280. #define REDUCE_FORMATS(fmt_type, list_type, list, var, nb, add_format) \
  281. do { \
  282. for (i = 0; i < filter->nb_inputs; i++) { \
  283. AVFilterLink *link = filter->inputs[i]; \
  284. fmt_type fmt; \
  285. \
  286. if (!link->out_ ## list || link->out_ ## list->nb != 1) \
  287. continue; \
  288. fmt = link->out_ ## list->var[0]; \
  289. \
  290. for (j = 0; j < filter->nb_outputs; j++) { \
  291. AVFilterLink *out_link = filter->outputs[j]; \
  292. list_type *fmts; \
  293. \
  294. if (link->type != out_link->type || \
  295. out_link->in_ ## list->nb == 1) \
  296. continue; \
  297. fmts = out_link->in_ ## list; \
  298. \
  299. if (!out_link->in_ ## list->nb) { \
  300. add_format(&out_link->in_ ##list, fmt); \
  301. break; \
  302. } \
  303. \
  304. for (k = 0; k < out_link->in_ ## list->nb; k++) \
  305. if (fmts->var[k] == fmt) { \
  306. fmts->var[0] = fmt; \
  307. fmts->nb = 1; \
  308. ret = 1; \
  309. break; \
  310. } \
  311. } \
  312. } \
  313. } while (0)
  314. static int reduce_formats_on_filter(AVFilterContext *filter)
  315. {
  316. int i, j, k, ret = 0;
  317. REDUCE_FORMATS(int, AVFilterFormats, formats, formats,
  318. format_count, ff_add_format);
  319. REDUCE_FORMATS(int, AVFilterFormats, samplerates, formats,
  320. format_count, ff_add_format);
  321. REDUCE_FORMATS(uint64_t, AVFilterChannelLayouts, channel_layouts,
  322. channel_layouts, nb_channel_layouts, ff_add_channel_layout);
  323. return ret;
  324. }
  325. static void reduce_formats(AVFilterGraph *graph)
  326. {
  327. int i, reduced;
  328. do {
  329. reduced = 0;
  330. for (i = 0; i < graph->nb_filters; i++)
  331. reduced |= reduce_formats_on_filter(graph->filters[i]);
  332. } while (reduced);
  333. }
  334. static void swap_samplerates_on_filter(AVFilterContext *filter)
  335. {
  336. AVFilterLink *link = NULL;
  337. int sample_rate;
  338. int i, j;
  339. for (i = 0; i < filter->nb_inputs; i++) {
  340. link = filter->inputs[i];
  341. if (link->type == AVMEDIA_TYPE_AUDIO &&
  342. link->out_samplerates->format_count == 1)
  343. break;
  344. }
  345. if (i == filter->nb_inputs)
  346. return;
  347. sample_rate = link->out_samplerates->formats[0];
  348. for (i = 0; i < filter->nb_outputs; i++) {
  349. AVFilterLink *outlink = filter->outputs[i];
  350. int best_idx, best_diff = INT_MAX;
  351. if (outlink->type != AVMEDIA_TYPE_AUDIO ||
  352. outlink->in_samplerates->format_count < 2)
  353. continue;
  354. for (j = 0; j < outlink->in_samplerates->format_count; j++) {
  355. int diff = abs(sample_rate - outlink->in_samplerates->formats[j]);
  356. if (diff < best_diff) {
  357. best_diff = diff;
  358. best_idx = j;
  359. }
  360. }
  361. FFSWAP(int, outlink->in_samplerates->formats[0],
  362. outlink->in_samplerates->formats[best_idx]);
  363. }
  364. }
  365. static void swap_samplerates(AVFilterGraph *graph)
  366. {
  367. int i;
  368. for (i = 0; i < graph->nb_filters; i++)
  369. swap_samplerates_on_filter(graph->filters[i]);
  370. }
  371. #define CH_CENTER_PAIR (AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_RIGHT_OF_CENTER)
  372. #define CH_FRONT_PAIR (AV_CH_FRONT_LEFT | AV_CH_FRONT_RIGHT)
  373. #define CH_STEREO_PAIR (AV_CH_STEREO_LEFT | AV_CH_STEREO_RIGHT)
  374. #define CH_WIDE_PAIR (AV_CH_WIDE_LEFT | AV_CH_WIDE_RIGHT)
  375. #define CH_SIDE_PAIR (AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT)
  376. #define CH_DIRECT_PAIR (AV_CH_SURROUND_DIRECT_LEFT | AV_CH_SURROUND_DIRECT_RIGHT)
  377. #define CH_BACK_PAIR (AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT)
  378. /* allowable substitutions for channel pairs when comparing layouts,
  379. * ordered by priority for both values */
  380. static const uint64_t ch_subst[][2] = {
  381. { CH_FRONT_PAIR, CH_CENTER_PAIR },
  382. { CH_FRONT_PAIR, CH_WIDE_PAIR },
  383. { CH_FRONT_PAIR, AV_CH_FRONT_CENTER },
  384. { CH_CENTER_PAIR, CH_FRONT_PAIR },
  385. { CH_CENTER_PAIR, CH_WIDE_PAIR },
  386. { CH_CENTER_PAIR, AV_CH_FRONT_CENTER },
  387. { CH_WIDE_PAIR, CH_FRONT_PAIR },
  388. { CH_WIDE_PAIR, CH_CENTER_PAIR },
  389. { CH_WIDE_PAIR, AV_CH_FRONT_CENTER },
  390. { AV_CH_FRONT_CENTER, CH_FRONT_PAIR },
  391. { AV_CH_FRONT_CENTER, CH_CENTER_PAIR },
  392. { AV_CH_FRONT_CENTER, CH_WIDE_PAIR },
  393. { CH_SIDE_PAIR, CH_DIRECT_PAIR },
  394. { CH_SIDE_PAIR, CH_BACK_PAIR },
  395. { CH_SIDE_PAIR, AV_CH_BACK_CENTER },
  396. { CH_BACK_PAIR, CH_DIRECT_PAIR },
  397. { CH_BACK_PAIR, CH_SIDE_PAIR },
  398. { CH_BACK_PAIR, AV_CH_BACK_CENTER },
  399. { AV_CH_BACK_CENTER, CH_BACK_PAIR },
  400. { AV_CH_BACK_CENTER, CH_DIRECT_PAIR },
  401. { AV_CH_BACK_CENTER, CH_SIDE_PAIR },
  402. };
  403. static void swap_channel_layouts_on_filter(AVFilterContext *filter)
  404. {
  405. AVFilterLink *link = NULL;
  406. int i, j, k;
  407. for (i = 0; i < filter->nb_inputs; i++) {
  408. link = filter->inputs[i];
  409. if (link->type == AVMEDIA_TYPE_AUDIO &&
  410. link->out_channel_layouts->nb_channel_layouts == 1)
  411. break;
  412. }
  413. if (i == filter->nb_inputs)
  414. return;
  415. for (i = 0; i < filter->nb_outputs; i++) {
  416. AVFilterLink *outlink = filter->outputs[i];
  417. int best_idx = -1, best_score = INT_MIN, best_count_diff = INT_MAX;
  418. if (outlink->type != AVMEDIA_TYPE_AUDIO ||
  419. outlink->in_channel_layouts->nb_channel_layouts < 2)
  420. continue;
  421. for (j = 0; j < outlink->in_channel_layouts->nb_channel_layouts; j++) {
  422. uint64_t in_chlayout = link->out_channel_layouts->channel_layouts[0];
  423. uint64_t out_chlayout = outlink->in_channel_layouts->channel_layouts[j];
  424. int in_channels = av_get_channel_layout_nb_channels(in_chlayout);
  425. int out_channels = av_get_channel_layout_nb_channels(out_chlayout);
  426. int count_diff = out_channels - in_channels;
  427. int matched_channels, extra_channels;
  428. int score = 0;
  429. /* channel substitution */
  430. for (k = 0; k < FF_ARRAY_ELEMS(ch_subst); k++) {
  431. uint64_t cmp0 = ch_subst[k][0];
  432. uint64_t cmp1 = ch_subst[k][1];
  433. if (( in_chlayout & cmp0) && (!(out_chlayout & cmp0)) &&
  434. (out_chlayout & cmp1) && (!( in_chlayout & cmp1))) {
  435. in_chlayout &= ~cmp0;
  436. out_chlayout &= ~cmp1;
  437. /* add score for channel match, minus a deduction for
  438. having to do the substitution */
  439. score += 10 * av_get_channel_layout_nb_channels(cmp1) - 2;
  440. }
  441. }
  442. /* no penalty for LFE channel mismatch */
  443. if ( (in_chlayout & AV_CH_LOW_FREQUENCY) &&
  444. (out_chlayout & AV_CH_LOW_FREQUENCY))
  445. score += 10;
  446. in_chlayout &= ~AV_CH_LOW_FREQUENCY;
  447. out_chlayout &= ~AV_CH_LOW_FREQUENCY;
  448. matched_channels = av_get_channel_layout_nb_channels(in_chlayout &
  449. out_chlayout);
  450. extra_channels = av_get_channel_layout_nb_channels(out_chlayout &
  451. (~in_chlayout));
  452. score += 10 * matched_channels - 5 * extra_channels;
  453. if (score > best_score ||
  454. (count_diff < best_count_diff && score == best_score)) {
  455. best_score = score;
  456. best_idx = j;
  457. best_count_diff = count_diff;
  458. }
  459. }
  460. av_assert0(best_idx >= 0);
  461. FFSWAP(uint64_t, outlink->in_channel_layouts->channel_layouts[0],
  462. outlink->in_channel_layouts->channel_layouts[best_idx]);
  463. }
  464. }
  465. static void swap_channel_layouts(AVFilterGraph *graph)
  466. {
  467. int i;
  468. for (i = 0; i < graph->nb_filters; i++)
  469. swap_channel_layouts_on_filter(graph->filters[i]);
  470. }
  471. static void swap_sample_fmts_on_filter(AVFilterContext *filter)
  472. {
  473. AVFilterLink *link = NULL;
  474. int format, bps;
  475. int i, j;
  476. for (i = 0; i < filter->nb_inputs; i++) {
  477. link = filter->inputs[i];
  478. if (link->type == AVMEDIA_TYPE_AUDIO &&
  479. link->out_formats->format_count == 1)
  480. break;
  481. }
  482. if (i == filter->nb_inputs)
  483. return;
  484. format = link->out_formats->formats[0];
  485. bps = av_get_bytes_per_sample(format);
  486. for (i = 0; i < filter->nb_outputs; i++) {
  487. AVFilterLink *outlink = filter->outputs[i];
  488. int best_idx = -1, best_score = INT_MIN;
  489. if (outlink->type != AVMEDIA_TYPE_AUDIO ||
  490. outlink->in_formats->format_count < 2)
  491. continue;
  492. for (j = 0; j < outlink->in_formats->format_count; j++) {
  493. int out_format = outlink->in_formats->formats[j];
  494. int out_bps = av_get_bytes_per_sample(out_format);
  495. int score;
  496. if (av_get_packed_sample_fmt(out_format) == format ||
  497. av_get_planar_sample_fmt(out_format) == format) {
  498. best_idx = j;
  499. break;
  500. }
  501. /* for s32 and float prefer double to prevent loss of information */
  502. if (bps == 4 && out_bps == 8) {
  503. best_idx = j;
  504. break;
  505. }
  506. /* prefer closest higher or equal bps */
  507. score = -abs(out_bps - bps);
  508. if (out_bps >= bps)
  509. score += INT_MAX/2;
  510. if (score > best_score) {
  511. best_score = score;
  512. best_idx = j;
  513. }
  514. }
  515. av_assert0(best_idx >= 0);
  516. FFSWAP(int, outlink->in_formats->formats[0],
  517. outlink->in_formats->formats[best_idx]);
  518. }
  519. }
  520. static void swap_sample_fmts(AVFilterGraph *graph)
  521. {
  522. int i;
  523. for (i = 0; i < graph->nb_filters; i++)
  524. swap_sample_fmts_on_filter(graph->filters[i]);
  525. }
  526. static int pick_formats(AVFilterGraph *graph)
  527. {
  528. int i, j, ret;
  529. for (i = 0; i < graph->nb_filters; i++) {
  530. AVFilterContext *filter = graph->filters[i];
  531. for (j = 0; j < filter->nb_inputs; j++)
  532. if ((ret = pick_format(filter->inputs[j])) < 0)
  533. return ret;
  534. for (j = 0; j < filter->nb_outputs; j++)
  535. if ((ret = pick_format(filter->outputs[j])) < 0)
  536. return ret;
  537. }
  538. return 0;
  539. }
  540. /**
  541. * Configure the formats of all the links in the graph.
  542. */
  543. static int graph_config_formats(AVFilterGraph *graph, AVClass *log_ctx)
  544. {
  545. int ret;
  546. /* find supported formats from sub-filters, and merge along links */
  547. if ((ret = query_formats(graph, log_ctx)) < 0)
  548. return ret;
  549. /* Once everything is merged, it's possible that we'll still have
  550. * multiple valid media format choices. We try to minimize the amount
  551. * of format conversion inside filters */
  552. reduce_formats(graph);
  553. /* for audio filters, ensure the best format, sample rate and channel layout
  554. * is selected */
  555. swap_sample_fmts(graph);
  556. swap_samplerates(graph);
  557. swap_channel_layouts(graph);
  558. if ((ret = pick_formats(graph)) < 0)
  559. return ret;
  560. return 0;
  561. }
  562. static int graph_insert_fifos(AVFilterGraph *graph, AVClass *log_ctx)
  563. {
  564. AVFilterContext *f;
  565. int i, j, ret;
  566. int fifo_count = 0;
  567. for (i = 0; i < graph->nb_filters; i++) {
  568. f = graph->filters[i];
  569. for (j = 0; j < f->nb_inputs; j++) {
  570. AVFilterLink *link = f->inputs[j];
  571. AVFilterContext *fifo_ctx;
  572. AVFilter *fifo;
  573. char name[32];
  574. if (!link->dstpad->needs_fifo)
  575. continue;
  576. fifo = f->inputs[j]->type == AVMEDIA_TYPE_VIDEO ?
  577. avfilter_get_by_name("fifo") :
  578. avfilter_get_by_name("afifo");
  579. snprintf(name, sizeof(name), "auto-inserted fifo %d", fifo_count++);
  580. ret = avfilter_graph_create_filter(&fifo_ctx, fifo, name, NULL,
  581. NULL, graph);
  582. if (ret < 0)
  583. return ret;
  584. ret = avfilter_insert_filter(link, fifo_ctx, 0, 0);
  585. if (ret < 0)
  586. return ret;
  587. }
  588. }
  589. return 0;
  590. }
  591. int avfilter_graph_config(AVFilterGraph *graphctx, void *log_ctx)
  592. {
  593. int ret;
  594. if ((ret = graph_check_validity(graphctx, log_ctx)))
  595. return ret;
  596. if ((ret = graph_insert_fifos(graphctx, log_ctx)) < 0)
  597. return ret;
  598. if ((ret = graph_config_formats(graphctx, log_ctx)))
  599. return ret;
  600. if ((ret = graph_config_links(graphctx, log_ctx)))
  601. return ret;
  602. return 0;
  603. }