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.

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