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.

1062 lines
37KB

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