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.

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