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.

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