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.

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