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.

748 lines
26KB

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