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.

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