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.

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