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.

1083 lines
38KB

  1. /*
  2. * filter graphs
  3. * Copyright (c) 2008 Vitor Sessak
  4. * Copyright (c) 2007 Bobby Bingham
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg 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. * FFmpeg 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 FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #include <ctype.h>
  23. #include <string.h>
  24. #include "libavutil/avassert.h"
  25. #include "libavutil/channel_layout.h"
  26. #include "libavutil/opt.h"
  27. #include "libavutil/pixdesc.h"
  28. #include "libavcodec/avcodec.h" // avcodec_find_best_pix_fmt_of_2()
  29. #include "avfilter.h"
  30. #include "avfiltergraph.h"
  31. #include "formats.h"
  32. #include "internal.h"
  33. #define OFFSET(x) offsetof(AVFilterGraph,x)
  34. static const AVOption options[]={
  35. {"scale_sws_opts" , "default scale filter options" , OFFSET(scale_sws_opts) , AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, 0 },
  36. {"aresample_swr_opts" , "default aresample filter options" , OFFSET(aresample_swr_opts) , AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, 0 },
  37. {0}
  38. };
  39. static const AVClass filtergraph_class = {
  40. .class_name = "AVFilterGraph",
  41. .item_name = av_default_item_name,
  42. .option = options,
  43. .version = LIBAVUTIL_VERSION_INT,
  44. .category = AV_CLASS_CATEGORY_FILTER,
  45. };
  46. AVFilterGraph *avfilter_graph_alloc(void)
  47. {
  48. AVFilterGraph *ret = av_mallocz(sizeof(AVFilterGraph));
  49. if (!ret)
  50. return NULL;
  51. ret->av_class = &filtergraph_class;
  52. return ret;
  53. }
  54. void avfilter_graph_free(AVFilterGraph **graph)
  55. {
  56. if (!*graph)
  57. return;
  58. for (; (*graph)->filter_count > 0; (*graph)->filter_count--)
  59. avfilter_free((*graph)->filters[(*graph)->filter_count - 1]);
  60. av_freep(&(*graph)->sink_links);
  61. av_freep(&(*graph)->scale_sws_opts);
  62. av_freep(&(*graph)->aresample_swr_opts);
  63. av_freep(&(*graph)->filters);
  64. av_freep(graph);
  65. }
  66. int avfilter_graph_add_filter(AVFilterGraph *graph, AVFilterContext *filter)
  67. {
  68. AVFilterContext **filters = av_realloc(graph->filters,
  69. sizeof(AVFilterContext*) * (graph->filter_count+1));
  70. if (!filters)
  71. return AVERROR(ENOMEM);
  72. graph->filters = filters;
  73. graph->filters[graph->filter_count++] = filter;
  74. return 0;
  75. }
  76. int avfilter_graph_create_filter(AVFilterContext **filt_ctx, AVFilter *filt,
  77. const char *name, const char *args, void *opaque,
  78. AVFilterGraph *graph_ctx)
  79. {
  80. int ret;
  81. if ((ret = avfilter_open(filt_ctx, filt, name)) < 0)
  82. goto fail;
  83. if ((ret = avfilter_init_filter(*filt_ctx, args, opaque)) < 0)
  84. goto fail;
  85. if ((ret = avfilter_graph_add_filter(graph_ctx, *filt_ctx)) < 0)
  86. goto fail;
  87. return 0;
  88. fail:
  89. if (*filt_ctx)
  90. avfilter_free(*filt_ctx);
  91. *filt_ctx = NULL;
  92. return ret;
  93. }
  94. void avfilter_graph_set_auto_convert(AVFilterGraph *graph, unsigned flags)
  95. {
  96. graph->disable_auto_convert = flags;
  97. }
  98. /**
  99. * Check for the validity of graph.
  100. *
  101. * A graph is considered valid if all its input and output pads are
  102. * connected.
  103. *
  104. * @return 0 in case of success, a negative value otherwise
  105. */
  106. static int graph_check_validity(AVFilterGraph *graph, AVClass *log_ctx)
  107. {
  108. AVFilterContext *filt;
  109. int i, j;
  110. for (i = 0; i < graph->filter_count; i++) {
  111. const AVFilterPad *pad;
  112. filt = graph->filters[i];
  113. for (j = 0; j < filt->nb_inputs; j++) {
  114. if (!filt->inputs[j] || !filt->inputs[j]->src) {
  115. pad = &filt->input_pads[j];
  116. av_log(log_ctx, AV_LOG_ERROR,
  117. "Input pad \"%s\" with type %s of the filter instance \"%s\" of %s not connected to any source\n",
  118. pad->name, av_get_media_type_string(pad->type), filt->name, filt->filter->name);
  119. return AVERROR(EINVAL);
  120. }
  121. }
  122. for (j = 0; j < filt->nb_outputs; j++) {
  123. if (!filt->outputs[j] || !filt->outputs[j]->dst) {
  124. pad = &filt->output_pads[j];
  125. av_log(log_ctx, AV_LOG_ERROR,
  126. "Output pad \"%s\" with type %s of the filter instance \"%s\" of %s not connected to any destination\n",
  127. pad->name, av_get_media_type_string(pad->type), filt->name, filt->filter->name);
  128. return AVERROR(EINVAL);
  129. }
  130. }
  131. }
  132. return 0;
  133. }
  134. /**
  135. * Configure all the links of graphctx.
  136. *
  137. * @return 0 in case of success, a negative value otherwise
  138. */
  139. static int graph_config_links(AVFilterGraph *graph, AVClass *log_ctx)
  140. {
  141. AVFilterContext *filt;
  142. int i, ret;
  143. for (i=0; i < graph->filter_count; i++) {
  144. filt = graph->filters[i];
  145. if (!filt->nb_outputs) {
  146. if ((ret = avfilter_config_links(filt)))
  147. return ret;
  148. }
  149. }
  150. return 0;
  151. }
  152. AVFilterContext *avfilter_graph_get_filter(AVFilterGraph *graph, char *name)
  153. {
  154. int i;
  155. for (i = 0; i < graph->filter_count; i++)
  156. if (graph->filters[i]->name && !strcmp(name, graph->filters[i]->name))
  157. return graph->filters[i];
  158. return NULL;
  159. }
  160. static int filter_query_formats(AVFilterContext *ctx)
  161. {
  162. int ret;
  163. AVFilterFormats *formats;
  164. AVFilterChannelLayouts *chlayouts;
  165. AVFilterFormats *samplerates;
  166. enum AVMediaType type = ctx->inputs && ctx->inputs [0] ? ctx->inputs [0]->type :
  167. ctx->outputs && ctx->outputs[0] ? ctx->outputs[0]->type :
  168. AVMEDIA_TYPE_VIDEO;
  169. if ((ret = ctx->filter->query_formats(ctx)) < 0) {
  170. av_log(ctx, AV_LOG_ERROR, "Query format failed for '%s': %s\n",
  171. ctx->name, av_err2str(ret));
  172. return ret;
  173. }
  174. formats = ff_all_formats(type);
  175. if (!formats)
  176. return AVERROR(ENOMEM);
  177. ff_set_common_formats(ctx, formats);
  178. if (type == AVMEDIA_TYPE_AUDIO) {
  179. samplerates = ff_all_samplerates();
  180. if (!samplerates)
  181. return AVERROR(ENOMEM);
  182. ff_set_common_samplerates(ctx, samplerates);
  183. chlayouts = ff_all_channel_layouts();
  184. if (!chlayouts)
  185. return AVERROR(ENOMEM);
  186. ff_set_common_channel_layouts(ctx, chlayouts);
  187. }
  188. return 0;
  189. }
  190. static int insert_conv_filter(AVFilterGraph *graph, AVFilterLink *link,
  191. const char *filt_name, const char *filt_args)
  192. {
  193. static int auto_count = 0, ret;
  194. char inst_name[32];
  195. AVFilterContext *filt_ctx;
  196. if (graph->disable_auto_convert) {
  197. av_log(NULL, AV_LOG_ERROR,
  198. "The filters '%s' and '%s' do not have a common format "
  199. "and automatic conversion is disabled.\n",
  200. link->src->name, link->dst->name);
  201. return AVERROR(EINVAL);
  202. }
  203. snprintf(inst_name, sizeof(inst_name), "auto-inserted %s %d",
  204. filt_name, auto_count++);
  205. if ((ret = avfilter_graph_create_filter(&filt_ctx,
  206. avfilter_get_by_name(filt_name),
  207. inst_name, filt_args, NULL, graph)) < 0)
  208. return ret;
  209. if ((ret = avfilter_insert_filter(link, filt_ctx, 0, 0)) < 0)
  210. return ret;
  211. filter_query_formats(filt_ctx);
  212. if ( ((link = filt_ctx-> inputs[0]) &&
  213. !ff_merge_formats(link->in_formats, link->out_formats)) ||
  214. ((link = filt_ctx->outputs[0]) &&
  215. !ff_merge_formats(link->in_formats, link->out_formats))
  216. ) {
  217. av_log(NULL, AV_LOG_ERROR,
  218. "Impossible to convert between the formats supported by the filter "
  219. "'%s' and the filter '%s'\n", link->src->name, link->dst->name);
  220. return AVERROR(EINVAL);
  221. }
  222. if (link->type == AVMEDIA_TYPE_AUDIO &&
  223. (((link = filt_ctx-> inputs[0]) &&
  224. !ff_merge_channel_layouts(link->in_channel_layouts, link->out_channel_layouts)) ||
  225. ((link = filt_ctx->outputs[0]) &&
  226. !ff_merge_channel_layouts(link->in_channel_layouts, link->out_channel_layouts)))
  227. ) {
  228. av_log(NULL, AV_LOG_ERROR,
  229. "Impossible to convert between the channel layouts formats supported by the filter "
  230. "'%s' and the filter '%s'\n", link->src->name, link->dst->name);
  231. return AVERROR(EINVAL);
  232. }
  233. return 0;
  234. }
  235. static int query_formats(AVFilterGraph *graph, AVClass *log_ctx)
  236. {
  237. int i, j, ret;
  238. #if 0
  239. char filt_args[128];
  240. AVFilterFormats *formats;
  241. AVFilterChannelLayouts *chlayouts;
  242. AVFilterFormats *samplerates;
  243. #endif
  244. int scaler_count = 0, resampler_count = 0;
  245. for (j = 0; j < 2; j++) {
  246. /* ask all the sub-filters for their supported media formats */
  247. for (i = 0; i < graph->filter_count; i++) {
  248. /* Call query_formats on sources first.
  249. This is a temporary workaround for amerge,
  250. until format renegociation is implemented. */
  251. if (!graph->filters[i]->nb_inputs == j)
  252. continue;
  253. if (graph->filters[i]->filter->query_formats)
  254. ret = filter_query_formats(graph->filters[i]);
  255. else
  256. ret = ff_default_query_formats(graph->filters[i]);
  257. if (ret < 0)
  258. return ret;
  259. }
  260. }
  261. /* go through and merge as many format lists as possible */
  262. for (i = 0; i < graph->filter_count; i++) {
  263. AVFilterContext *filter = graph->filters[i];
  264. for (j = 0; j < filter->nb_inputs; j++) {
  265. AVFilterLink *link = filter->inputs[j];
  266. #if 0
  267. if (!link) continue;
  268. if (!link->in_formats || !link->out_formats)
  269. return AVERROR(EINVAL);
  270. if (link->type == AVMEDIA_TYPE_VIDEO &&
  271. !ff_merge_formats(link->in_formats, link->out_formats)) {
  272. /* couldn't merge format lists, auto-insert scale filter */
  273. snprintf(filt_args, sizeof(filt_args), "0:0:%s",
  274. graph->scale_sws_opts);
  275. if (ret = insert_conv_filter(graph, link, "scale", filt_args))
  276. return ret;
  277. }
  278. else if (link->type == AVMEDIA_TYPE_AUDIO) {
  279. if (!link->in_channel_layouts || !link->out_channel_layouts)
  280. return AVERROR(EINVAL);
  281. /* Merge all three list before checking: that way, in all
  282. * three categories, aconvert will use a common format
  283. * whenever possible. */
  284. formats = ff_merge_formats(link->in_formats, link->out_formats);
  285. chlayouts = ff_merge_channel_layouts(link->in_channel_layouts , link->out_channel_layouts);
  286. samplerates = ff_merge_samplerates (link->in_samplerates, link->out_samplerates);
  287. if (!formats || !chlayouts || !samplerates)
  288. if (ret = insert_conv_filter(graph, link, "aresample", NULL))
  289. return ret;
  290. #else
  291. int convert_needed = 0;
  292. if (!link)
  293. continue;
  294. if (link->in_formats != link->out_formats &&
  295. !ff_merge_formats(link->in_formats,
  296. link->out_formats))
  297. convert_needed = 1;
  298. if (link->type == AVMEDIA_TYPE_AUDIO) {
  299. if (link->in_channel_layouts != link->out_channel_layouts &&
  300. !ff_merge_channel_layouts(link->in_channel_layouts,
  301. link->out_channel_layouts))
  302. convert_needed = 1;
  303. if (link->in_samplerates != link->out_samplerates &&
  304. !ff_merge_samplerates(link->in_samplerates,
  305. link->out_samplerates))
  306. convert_needed = 1;
  307. }
  308. if (convert_needed) {
  309. AVFilterContext *convert;
  310. AVFilter *filter;
  311. AVFilterLink *inlink, *outlink;
  312. char scale_args[256];
  313. char inst_name[30];
  314. /* couldn't merge format lists. auto-insert conversion filter */
  315. switch (link->type) {
  316. case AVMEDIA_TYPE_VIDEO:
  317. if (!(filter = avfilter_get_by_name("scale"))) {
  318. av_log(log_ctx, AV_LOG_ERROR, "'scale' filter "
  319. "not present, cannot convert pixel formats.\n");
  320. return AVERROR(EINVAL);
  321. }
  322. snprintf(inst_name, sizeof(inst_name), "auto-inserted scaler %d",
  323. scaler_count++);
  324. if (graph->scale_sws_opts)
  325. snprintf(scale_args, sizeof(scale_args), "0:0:%s", graph->scale_sws_opts);
  326. else
  327. snprintf(scale_args, sizeof(scale_args), "0:0");
  328. if ((ret = avfilter_graph_create_filter(&convert, filter,
  329. inst_name, scale_args, NULL,
  330. graph)) < 0)
  331. return ret;
  332. break;
  333. case AVMEDIA_TYPE_AUDIO:
  334. if (!(filter = avfilter_get_by_name("aresample"))) {
  335. av_log(log_ctx, AV_LOG_ERROR, "'aresample' filter "
  336. "not present, cannot convert audio formats.\n");
  337. return AVERROR(EINVAL);
  338. }
  339. snprintf(inst_name, sizeof(inst_name), "auto-inserted resampler %d",
  340. resampler_count++);
  341. if ((ret = avfilter_graph_create_filter(&convert, filter,
  342. inst_name, graph->aresample_swr_opts, NULL, graph)) < 0)
  343. return ret;
  344. break;
  345. default:
  346. return AVERROR(EINVAL);
  347. }
  348. if ((ret = avfilter_insert_filter(link, convert, 0, 0)) < 0)
  349. return ret;
  350. filter_query_formats(convert);
  351. inlink = convert->inputs[0];
  352. outlink = convert->outputs[0];
  353. if (!ff_merge_formats( inlink->in_formats, inlink->out_formats) ||
  354. !ff_merge_formats(outlink->in_formats, outlink->out_formats))
  355. ret |= AVERROR(ENOSYS);
  356. if (inlink->type == AVMEDIA_TYPE_AUDIO &&
  357. (!ff_merge_samplerates(inlink->in_samplerates,
  358. inlink->out_samplerates) ||
  359. !ff_merge_channel_layouts(inlink->in_channel_layouts,
  360. inlink->out_channel_layouts)))
  361. ret |= AVERROR(ENOSYS);
  362. if (outlink->type == AVMEDIA_TYPE_AUDIO &&
  363. (!ff_merge_samplerates(outlink->in_samplerates,
  364. outlink->out_samplerates) ||
  365. !ff_merge_channel_layouts(outlink->in_channel_layouts,
  366. outlink->out_channel_layouts)))
  367. ret |= AVERROR(ENOSYS);
  368. if (ret < 0) {
  369. av_log(log_ctx, AV_LOG_ERROR,
  370. "Impossible to convert between the formats supported by the filter "
  371. "'%s' and the filter '%s'\n", link->src->name, link->dst->name);
  372. return ret;
  373. }
  374. #endif
  375. }
  376. }
  377. }
  378. return 0;
  379. }
  380. static int pick_format(AVFilterLink *link, AVFilterLink *ref)
  381. {
  382. if (!link || !link->in_formats)
  383. return 0;
  384. if (link->type == AVMEDIA_TYPE_VIDEO) {
  385. if(ref && ref->type == AVMEDIA_TYPE_VIDEO){
  386. int has_alpha= av_pix_fmt_desc_get(ref->format)->nb_components % 2 == 0;
  387. enum AVPixelFormat best= AV_PIX_FMT_NONE;
  388. int i;
  389. for (i=0; i<link->in_formats->format_count; i++) {
  390. enum AVPixelFormat p = link->in_formats->formats[i];
  391. best= avcodec_find_best_pix_fmt_of_2(best, p, ref->format, has_alpha, NULL);
  392. }
  393. av_log(link->src,AV_LOG_DEBUG, "picking %s out of %d ref:%s alpha:%d\n",
  394. av_get_pix_fmt_name(best), link->in_formats->format_count,
  395. av_get_pix_fmt_name(ref->format), has_alpha);
  396. link->in_formats->formats[0] = best;
  397. }
  398. }
  399. link->in_formats->format_count = 1;
  400. link->format = link->in_formats->formats[0];
  401. if (link->type == AVMEDIA_TYPE_AUDIO) {
  402. if (!link->in_samplerates->format_count) {
  403. av_log(link->src, AV_LOG_ERROR, "Cannot select sample rate for"
  404. " the link between filters %s and %s.\n", link->src->name,
  405. link->dst->name);
  406. return AVERROR(EINVAL);
  407. }
  408. link->in_samplerates->format_count = 1;
  409. link->sample_rate = link->in_samplerates->formats[0];
  410. if (!link->in_channel_layouts->nb_channel_layouts) {
  411. av_log(link->src, AV_LOG_ERROR, "Cannot select channel layout for"
  412. "the link between filters %s and %s.\n", link->src->name,
  413. link->dst->name);
  414. return AVERROR(EINVAL);
  415. }
  416. link->in_channel_layouts->nb_channel_layouts = 1;
  417. link->channel_layout = link->in_channel_layouts->channel_layouts[0];
  418. link->channels = av_get_channel_layout_nb_channels(link->channel_layout);
  419. }
  420. ff_formats_unref(&link->in_formats);
  421. ff_formats_unref(&link->out_formats);
  422. ff_formats_unref(&link->in_samplerates);
  423. ff_formats_unref(&link->out_samplerates);
  424. ff_channel_layouts_unref(&link->in_channel_layouts);
  425. ff_channel_layouts_unref(&link->out_channel_layouts);
  426. return 0;
  427. }
  428. #define REDUCE_FORMATS(fmt_type, list_type, list, var, nb, add_format) \
  429. do { \
  430. for (i = 0; i < filter->nb_inputs; i++) { \
  431. AVFilterLink *link = filter->inputs[i]; \
  432. fmt_type fmt; \
  433. \
  434. if (!link->out_ ## list || link->out_ ## list->nb != 1) \
  435. continue; \
  436. fmt = link->out_ ## list->var[0]; \
  437. \
  438. for (j = 0; j < filter->nb_outputs; j++) { \
  439. AVFilterLink *out_link = filter->outputs[j]; \
  440. list_type *fmts; \
  441. \
  442. if (link->type != out_link->type || \
  443. out_link->in_ ## list->nb == 1) \
  444. continue; \
  445. fmts = out_link->in_ ## list; \
  446. \
  447. if (!out_link->in_ ## list->nb) { \
  448. add_format(&out_link->in_ ##list, fmt); \
  449. break; \
  450. } \
  451. \
  452. for (k = 0; k < out_link->in_ ## list->nb; k++) \
  453. if (fmts->var[k] == fmt) { \
  454. fmts->var[0] = fmt; \
  455. fmts->nb = 1; \
  456. ret = 1; \
  457. break; \
  458. } \
  459. } \
  460. } \
  461. } while (0)
  462. static int reduce_formats_on_filter(AVFilterContext *filter)
  463. {
  464. int i, j, k, ret = 0;
  465. REDUCE_FORMATS(int, AVFilterFormats, formats, formats,
  466. format_count, ff_add_format);
  467. REDUCE_FORMATS(int, AVFilterFormats, samplerates, formats,
  468. format_count, ff_add_format);
  469. REDUCE_FORMATS(uint64_t, AVFilterChannelLayouts, channel_layouts,
  470. channel_layouts, nb_channel_layouts, ff_add_channel_layout);
  471. return ret;
  472. }
  473. static void reduce_formats(AVFilterGraph *graph)
  474. {
  475. int i, reduced;
  476. do {
  477. reduced = 0;
  478. for (i = 0; i < graph->filter_count; i++)
  479. reduced |= reduce_formats_on_filter(graph->filters[i]);
  480. } while (reduced);
  481. }
  482. static void swap_samplerates_on_filter(AVFilterContext *filter)
  483. {
  484. AVFilterLink *link = NULL;
  485. int sample_rate;
  486. int i, j;
  487. for (i = 0; i < filter->nb_inputs; i++) {
  488. link = filter->inputs[i];
  489. if (link->type == AVMEDIA_TYPE_AUDIO &&
  490. link->out_samplerates->format_count == 1)
  491. break;
  492. }
  493. if (i == filter->nb_inputs)
  494. return;
  495. sample_rate = link->out_samplerates->formats[0];
  496. for (i = 0; i < filter->nb_outputs; i++) {
  497. AVFilterLink *outlink = filter->outputs[i];
  498. int best_idx, best_diff = INT_MAX;
  499. if (outlink->type != AVMEDIA_TYPE_AUDIO ||
  500. outlink->in_samplerates->format_count < 2)
  501. continue;
  502. for (j = 0; j < outlink->in_samplerates->format_count; j++) {
  503. int diff = abs(sample_rate - outlink->in_samplerates->formats[j]);
  504. if (diff < best_diff) {
  505. best_diff = diff;
  506. best_idx = j;
  507. }
  508. }
  509. FFSWAP(int, outlink->in_samplerates->formats[0],
  510. outlink->in_samplerates->formats[best_idx]);
  511. }
  512. }
  513. static void swap_samplerates(AVFilterGraph *graph)
  514. {
  515. int i;
  516. for (i = 0; i < graph->filter_count; i++)
  517. swap_samplerates_on_filter(graph->filters[i]);
  518. }
  519. #define CH_CENTER_PAIR (AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_RIGHT_OF_CENTER)
  520. #define CH_FRONT_PAIR (AV_CH_FRONT_LEFT | AV_CH_FRONT_RIGHT)
  521. #define CH_STEREO_PAIR (AV_CH_STEREO_LEFT | AV_CH_STEREO_RIGHT)
  522. #define CH_WIDE_PAIR (AV_CH_WIDE_LEFT | AV_CH_WIDE_RIGHT)
  523. #define CH_SIDE_PAIR (AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT)
  524. #define CH_DIRECT_PAIR (AV_CH_SURROUND_DIRECT_LEFT | AV_CH_SURROUND_DIRECT_RIGHT)
  525. #define CH_BACK_PAIR (AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT)
  526. /* allowable substitutions for channel pairs when comparing layouts,
  527. * ordered by priority for both values */
  528. static const uint64_t ch_subst[][2] = {
  529. { CH_FRONT_PAIR, CH_CENTER_PAIR },
  530. { CH_FRONT_PAIR, CH_WIDE_PAIR },
  531. { CH_FRONT_PAIR, AV_CH_FRONT_CENTER },
  532. { CH_CENTER_PAIR, CH_FRONT_PAIR },
  533. { CH_CENTER_PAIR, CH_WIDE_PAIR },
  534. { CH_CENTER_PAIR, AV_CH_FRONT_CENTER },
  535. { CH_WIDE_PAIR, CH_FRONT_PAIR },
  536. { CH_WIDE_PAIR, CH_CENTER_PAIR },
  537. { CH_WIDE_PAIR, AV_CH_FRONT_CENTER },
  538. { AV_CH_FRONT_CENTER, CH_FRONT_PAIR },
  539. { AV_CH_FRONT_CENTER, CH_CENTER_PAIR },
  540. { AV_CH_FRONT_CENTER, CH_WIDE_PAIR },
  541. { CH_SIDE_PAIR, CH_DIRECT_PAIR },
  542. { CH_SIDE_PAIR, CH_BACK_PAIR },
  543. { CH_SIDE_PAIR, AV_CH_BACK_CENTER },
  544. { CH_BACK_PAIR, CH_DIRECT_PAIR },
  545. { CH_BACK_PAIR, CH_SIDE_PAIR },
  546. { CH_BACK_PAIR, AV_CH_BACK_CENTER },
  547. { AV_CH_BACK_CENTER, CH_BACK_PAIR },
  548. { AV_CH_BACK_CENTER, CH_DIRECT_PAIR },
  549. { AV_CH_BACK_CENTER, CH_SIDE_PAIR },
  550. };
  551. static void swap_channel_layouts_on_filter(AVFilterContext *filter)
  552. {
  553. AVFilterLink *link = NULL;
  554. int i, j, k;
  555. for (i = 0; i < filter->nb_inputs; i++) {
  556. link = filter->inputs[i];
  557. if (link->type == AVMEDIA_TYPE_AUDIO &&
  558. link->out_channel_layouts->nb_channel_layouts == 1)
  559. break;
  560. }
  561. if (i == filter->nb_inputs)
  562. return;
  563. for (i = 0; i < filter->nb_outputs; i++) {
  564. AVFilterLink *outlink = filter->outputs[i];
  565. int best_idx = -1, best_score = INT_MIN, best_count_diff = INT_MAX;
  566. if (outlink->type != AVMEDIA_TYPE_AUDIO ||
  567. outlink->in_channel_layouts->nb_channel_layouts < 2)
  568. continue;
  569. for (j = 0; j < outlink->in_channel_layouts->nb_channel_layouts; j++) {
  570. uint64_t in_chlayout = link->out_channel_layouts->channel_layouts[0];
  571. uint64_t out_chlayout = outlink->in_channel_layouts->channel_layouts[j];
  572. int in_channels = av_get_channel_layout_nb_channels(in_chlayout);
  573. int out_channels = av_get_channel_layout_nb_channels(out_chlayout);
  574. int count_diff = out_channels - in_channels;
  575. int matched_channels, extra_channels;
  576. int score = 0;
  577. /* channel substitution */
  578. for (k = 0; k < FF_ARRAY_ELEMS(ch_subst); k++) {
  579. uint64_t cmp0 = ch_subst[k][0];
  580. uint64_t cmp1 = ch_subst[k][1];
  581. if (( in_chlayout & cmp0) && (!(out_chlayout & cmp0)) &&
  582. (out_chlayout & cmp1) && (!( in_chlayout & cmp1))) {
  583. in_chlayout &= ~cmp0;
  584. out_chlayout &= ~cmp1;
  585. /* add score for channel match, minus a deduction for
  586. having to do the substitution */
  587. score += 10 * av_get_channel_layout_nb_channels(cmp1) - 2;
  588. }
  589. }
  590. /* no penalty for LFE channel mismatch */
  591. if ( (in_chlayout & AV_CH_LOW_FREQUENCY) &&
  592. (out_chlayout & AV_CH_LOW_FREQUENCY))
  593. score += 10;
  594. in_chlayout &= ~AV_CH_LOW_FREQUENCY;
  595. out_chlayout &= ~AV_CH_LOW_FREQUENCY;
  596. matched_channels = av_get_channel_layout_nb_channels(in_chlayout &
  597. out_chlayout);
  598. extra_channels = av_get_channel_layout_nb_channels(out_chlayout &
  599. (~in_chlayout));
  600. score += 10 * matched_channels - 5 * extra_channels;
  601. if (score > best_score ||
  602. (count_diff < best_count_diff && score == best_score)) {
  603. best_score = score;
  604. best_idx = j;
  605. best_count_diff = count_diff;
  606. }
  607. }
  608. av_assert0(best_idx >= 0);
  609. FFSWAP(uint64_t, outlink->in_channel_layouts->channel_layouts[0],
  610. outlink->in_channel_layouts->channel_layouts[best_idx]);
  611. }
  612. }
  613. static void swap_channel_layouts(AVFilterGraph *graph)
  614. {
  615. int i;
  616. for (i = 0; i < graph->filter_count; i++)
  617. swap_channel_layouts_on_filter(graph->filters[i]);
  618. }
  619. static void swap_sample_fmts_on_filter(AVFilterContext *filter)
  620. {
  621. AVFilterLink *link = NULL;
  622. int format, bps;
  623. int i, j;
  624. for (i = 0; i < filter->nb_inputs; i++) {
  625. link = filter->inputs[i];
  626. if (link->type == AVMEDIA_TYPE_AUDIO &&
  627. link->out_formats->format_count == 1)
  628. break;
  629. }
  630. if (i == filter->nb_inputs)
  631. return;
  632. format = link->out_formats->formats[0];
  633. bps = av_get_bytes_per_sample(format);
  634. for (i = 0; i < filter->nb_outputs; i++) {
  635. AVFilterLink *outlink = filter->outputs[i];
  636. int best_idx = -1, best_score = INT_MIN;
  637. if (outlink->type != AVMEDIA_TYPE_AUDIO ||
  638. outlink->in_formats->format_count < 2)
  639. continue;
  640. for (j = 0; j < outlink->in_formats->format_count; j++) {
  641. int out_format = outlink->in_formats->formats[j];
  642. int out_bps = av_get_bytes_per_sample(out_format);
  643. int score;
  644. if (av_get_packed_sample_fmt(out_format) == format ||
  645. av_get_planar_sample_fmt(out_format) == format) {
  646. best_idx = j;
  647. break;
  648. }
  649. /* for s32 and float prefer double to prevent loss of information */
  650. if (bps == 4 && out_bps == 8) {
  651. best_idx = j;
  652. break;
  653. }
  654. /* prefer closest higher or equal bps */
  655. score = -abs(out_bps - bps);
  656. if (out_bps >= bps)
  657. score += INT_MAX/2;
  658. if (score > best_score) {
  659. best_score = score;
  660. best_idx = j;
  661. }
  662. }
  663. av_assert0(best_idx >= 0);
  664. FFSWAP(int, outlink->in_formats->formats[0],
  665. outlink->in_formats->formats[best_idx]);
  666. }
  667. }
  668. static void swap_sample_fmts(AVFilterGraph *graph)
  669. {
  670. int i;
  671. for (i = 0; i < graph->filter_count; i++)
  672. swap_sample_fmts_on_filter(graph->filters[i]);
  673. }
  674. static int pick_formats(AVFilterGraph *graph)
  675. {
  676. int i, j, ret;
  677. int change;
  678. do{
  679. change = 0;
  680. for (i = 0; i < graph->filter_count; i++) {
  681. AVFilterContext *filter = graph->filters[i];
  682. if (filter->nb_inputs){
  683. for (j = 0; j < filter->nb_inputs; j++){
  684. if(filter->inputs[j]->in_formats && filter->inputs[j]->in_formats->format_count == 1) {
  685. pick_format(filter->inputs[j], NULL);
  686. change = 1;
  687. }
  688. }
  689. }
  690. if (filter->nb_outputs){
  691. for (j = 0; j < filter->nb_outputs; j++){
  692. if(filter->outputs[j]->in_formats && filter->outputs[j]->in_formats->format_count == 1) {
  693. pick_format(filter->outputs[j], NULL);
  694. change = 1;
  695. }
  696. }
  697. }
  698. if (filter->nb_inputs && filter->nb_outputs && filter->inputs[0]->format>=0) {
  699. for (j = 0; j < filter->nb_outputs; j++) {
  700. if(filter->outputs[j]->format<0) {
  701. pick_format(filter->outputs[j], filter->inputs[0]);
  702. change = 1;
  703. }
  704. }
  705. }
  706. }
  707. }while(change);
  708. for (i = 0; i < graph->filter_count; i++) {
  709. AVFilterContext *filter = graph->filters[i];
  710. for (j = 0; j < filter->nb_inputs; j++)
  711. if ((ret = pick_format(filter->inputs[j], NULL)) < 0)
  712. return ret;
  713. for (j = 0; j < filter->nb_outputs; j++)
  714. if ((ret = pick_format(filter->outputs[j], NULL)) < 0)
  715. return ret;
  716. }
  717. return 0;
  718. }
  719. /**
  720. * Configure the formats of all the links in the graph.
  721. */
  722. static int graph_config_formats(AVFilterGraph *graph, AVClass *log_ctx)
  723. {
  724. int ret;
  725. /* find supported formats from sub-filters, and merge along links */
  726. if ((ret = query_formats(graph, log_ctx)) < 0)
  727. return ret;
  728. /* Once everything is merged, it's possible that we'll still have
  729. * multiple valid media format choices. We try to minimize the amount
  730. * of format conversion inside filters */
  731. reduce_formats(graph);
  732. /* for audio filters, ensure the best format, sample rate and channel layout
  733. * is selected */
  734. swap_sample_fmts(graph);
  735. swap_samplerates(graph);
  736. swap_channel_layouts(graph);
  737. if ((ret = pick_formats(graph)) < 0)
  738. return ret;
  739. return 0;
  740. }
  741. static int ff_avfilter_graph_config_pointers(AVFilterGraph *graph,
  742. AVClass *log_ctx)
  743. {
  744. unsigned i, j;
  745. int sink_links_count = 0, n = 0;
  746. AVFilterContext *f;
  747. AVFilterLink **sinks;
  748. for (i = 0; i < graph->filter_count; i++) {
  749. f = graph->filters[i];
  750. for (j = 0; j < f->nb_inputs; j++) {
  751. f->inputs[j]->graph = graph;
  752. f->inputs[j]->age_index = -1;
  753. }
  754. for (j = 0; j < f->nb_outputs; j++) {
  755. f->outputs[j]->graph = graph;
  756. f->outputs[j]->age_index= -1;
  757. }
  758. if (!f->nb_outputs) {
  759. if (f->nb_inputs > INT_MAX - sink_links_count)
  760. return AVERROR(EINVAL);
  761. sink_links_count += f->nb_inputs;
  762. }
  763. }
  764. sinks = av_calloc(sink_links_count, sizeof(*sinks));
  765. if (!sinks)
  766. return AVERROR(ENOMEM);
  767. for (i = 0; i < graph->filter_count; i++) {
  768. f = graph->filters[i];
  769. if (!f->nb_outputs) {
  770. for (j = 0; j < f->nb_inputs; j++) {
  771. sinks[n] = f->inputs[j];
  772. f->inputs[j]->age_index = n++;
  773. }
  774. }
  775. }
  776. av_assert0(n == sink_links_count);
  777. graph->sink_links = sinks;
  778. graph->sink_links_count = sink_links_count;
  779. return 0;
  780. }
  781. static int graph_insert_fifos(AVFilterGraph *graph, AVClass *log_ctx)
  782. {
  783. AVFilterContext *f;
  784. int i, j, ret;
  785. int fifo_count = 0;
  786. for (i = 0; i < graph->filter_count; i++) {
  787. f = graph->filters[i];
  788. for (j = 0; j < f->nb_inputs; j++) {
  789. AVFilterLink *link = f->inputs[j];
  790. AVFilterContext *fifo_ctx;
  791. AVFilter *fifo;
  792. char name[32];
  793. if (!link->dstpad->needs_fifo)
  794. continue;
  795. fifo = f->inputs[j]->type == AVMEDIA_TYPE_VIDEO ?
  796. avfilter_get_by_name("fifo") :
  797. avfilter_get_by_name("afifo");
  798. snprintf(name, sizeof(name), "auto-inserted fifo %d", fifo_count++);
  799. ret = avfilter_graph_create_filter(&fifo_ctx, fifo, name, NULL,
  800. NULL, graph);
  801. if (ret < 0)
  802. return ret;
  803. ret = avfilter_insert_filter(link, fifo_ctx, 0, 0);
  804. if (ret < 0)
  805. return ret;
  806. }
  807. }
  808. return 0;
  809. }
  810. int avfilter_graph_config(AVFilterGraph *graphctx, void *log_ctx)
  811. {
  812. int ret;
  813. if ((ret = graph_check_validity(graphctx, log_ctx)))
  814. return ret;
  815. if ((ret = graph_insert_fifos(graphctx, log_ctx)) < 0)
  816. return ret;
  817. if ((ret = graph_config_formats(graphctx, log_ctx)))
  818. return ret;
  819. if ((ret = graph_config_links(graphctx, log_ctx)))
  820. return ret;
  821. if ((ret = ff_avfilter_graph_config_pointers(graphctx, log_ctx)))
  822. return ret;
  823. return 0;
  824. }
  825. int avfilter_graph_send_command(AVFilterGraph *graph, const char *target, const char *cmd, const char *arg, char *res, int res_len, int flags)
  826. {
  827. int i, r = AVERROR(ENOSYS);
  828. if(!graph)
  829. return r;
  830. if((flags & AVFILTER_CMD_FLAG_ONE) && !(flags & AVFILTER_CMD_FLAG_FAST)) {
  831. r=avfilter_graph_send_command(graph, target, cmd, arg, res, res_len, flags | AVFILTER_CMD_FLAG_FAST);
  832. if(r != AVERROR(ENOSYS))
  833. return r;
  834. }
  835. if(res_len && res)
  836. res[0]= 0;
  837. for (i = 0; i < graph->filter_count; i++) {
  838. AVFilterContext *filter = graph->filters[i];
  839. if(!strcmp(target, "all") || (filter->name && !strcmp(target, filter->name)) || !strcmp(target, filter->filter->name)){
  840. r = avfilter_process_command(filter, cmd, arg, res, res_len, flags);
  841. if(r != AVERROR(ENOSYS)) {
  842. if((flags & AVFILTER_CMD_FLAG_ONE) || r<0)
  843. return r;
  844. }
  845. }
  846. }
  847. return r;
  848. }
  849. int avfilter_graph_queue_command(AVFilterGraph *graph, const char *target, const char *command, const char *arg, int flags, double ts)
  850. {
  851. int i;
  852. if(!graph)
  853. return 0;
  854. for (i = 0; i < graph->filter_count; i++) {
  855. AVFilterContext *filter = graph->filters[i];
  856. if(filter && (!strcmp(target, "all") || !strcmp(target, filter->name) || !strcmp(target, filter->filter->name))){
  857. AVFilterCommand **que = &filter->command_queue, *next;
  858. while(*que && (*que)->time <= ts)
  859. que = &(*que)->next;
  860. next= *que;
  861. *que= av_mallocz(sizeof(AVFilterCommand));
  862. (*que)->command = av_strdup(command);
  863. (*que)->arg = av_strdup(arg);
  864. (*que)->time = ts;
  865. (*que)->flags = flags;
  866. (*que)->next = next;
  867. if(flags & AVFILTER_CMD_FLAG_ONE)
  868. return 0;
  869. }
  870. }
  871. return 0;
  872. }
  873. static void heap_bubble_up(AVFilterGraph *graph,
  874. AVFilterLink *link, int index)
  875. {
  876. AVFilterLink **links = graph->sink_links;
  877. while (index) {
  878. int parent = (index - 1) >> 1;
  879. if (links[parent]->current_pts >= link->current_pts)
  880. break;
  881. links[index] = links[parent];
  882. links[index]->age_index = index;
  883. index = parent;
  884. }
  885. links[index] = link;
  886. link->age_index = index;
  887. }
  888. static void heap_bubble_down(AVFilterGraph *graph,
  889. AVFilterLink *link, int index)
  890. {
  891. AVFilterLink **links = graph->sink_links;
  892. while (1) {
  893. int child = 2 * index + 1;
  894. if (child >= graph->sink_links_count)
  895. break;
  896. if (child + 1 < graph->sink_links_count &&
  897. links[child + 1]->current_pts < links[child]->current_pts)
  898. child++;
  899. if (link->current_pts < links[child]->current_pts)
  900. break;
  901. links[index] = links[child];
  902. links[index]->age_index = index;
  903. index = child;
  904. }
  905. links[index] = link;
  906. link->age_index = index;
  907. }
  908. void ff_avfilter_graph_update_heap(AVFilterGraph *graph, AVFilterLink *link)
  909. {
  910. heap_bubble_up (graph, link, link->age_index);
  911. heap_bubble_down(graph, link, link->age_index);
  912. }
  913. int avfilter_graph_request_oldest(AVFilterGraph *graph)
  914. {
  915. while (graph->sink_links_count) {
  916. AVFilterLink *oldest = graph->sink_links[0];
  917. int r = ff_request_frame(oldest);
  918. if (r != AVERROR_EOF)
  919. return r;
  920. av_log(oldest->dst, AV_LOG_DEBUG, "EOF on sink link %s:%s.\n",
  921. oldest->dst ? oldest->dst->name : "unknown",
  922. oldest->dstpad ? oldest->dstpad->name : "unknown");
  923. /* EOF: remove the link from the heap */
  924. if (oldest->age_index < --graph->sink_links_count)
  925. heap_bubble_down(graph, graph->sink_links[graph->sink_links_count],
  926. oldest->age_index);
  927. oldest->age_index = -1;
  928. }
  929. return AVERROR_EOF;
  930. }