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.

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