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.

826 lines
28KB

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