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.

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