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.

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