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.

814 lines
27KB

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