avfiltergraph.c 28 KB

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