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.

1061 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/audioconvert.h"
  25. #include "libavutil/avassert.h"
  26. #include "libavutil/pixdesc.h"
  27. #include "libavcodec/avcodec.h" // avcodec_find_best_pix_fmt2()
  28. #include "avfilter.h"
  29. #include "avfiltergraph.h"
  30. #include "formats.h"
  31. #include "internal.h"
  32. #include "libavutil/audioconvert.h"
  33. #include "libavutil/avassert.h"
  34. #include "libavutil/log.h"
  35. static const AVClass filtergraph_class = {
  36. .class_name = "AVFilterGraph",
  37. .item_name = av_default_item_name,
  38. .version = LIBAVUTIL_VERSION_INT,
  39. .category = AV_CLASS_CATEGORY_FILTER,
  40. };
  41. AVFilterGraph *avfilter_graph_alloc(void)
  42. {
  43. AVFilterGraph *ret = av_mallocz(sizeof(AVFilterGraph));
  44. if (!ret)
  45. return NULL;
  46. ret->av_class = &filtergraph_class;
  47. return ret;
  48. }
  49. void avfilter_graph_free(AVFilterGraph **graph)
  50. {
  51. if (!*graph)
  52. return;
  53. for (; (*graph)->filter_count > 0; (*graph)->filter_count--)
  54. avfilter_free((*graph)->filters[(*graph)->filter_count - 1]);
  55. av_freep(&(*graph)->sink_links);
  56. av_freep(&(*graph)->scale_sws_opts);
  57. av_freep(&(*graph)->filters);
  58. av_freep(graph);
  59. }
  60. int avfilter_graph_add_filter(AVFilterGraph *graph, AVFilterContext *filter)
  61. {
  62. AVFilterContext **filters = av_realloc(graph->filters,
  63. sizeof(AVFilterContext*) * (graph->filter_count+1));
  64. if (!filters)
  65. return AVERROR(ENOMEM);
  66. graph->filters = filters;
  67. graph->filters[graph->filter_count++] = filter;
  68. return 0;
  69. }
  70. int avfilter_graph_create_filter(AVFilterContext **filt_ctx, AVFilter *filt,
  71. const char *name, const char *args, void *opaque,
  72. AVFilterGraph *graph_ctx)
  73. {
  74. int ret;
  75. if ((ret = avfilter_open(filt_ctx, filt, name)) < 0)
  76. goto fail;
  77. if ((ret = avfilter_init_filter(*filt_ctx, args, opaque)) < 0)
  78. goto fail;
  79. if ((ret = avfilter_graph_add_filter(graph_ctx, *filt_ctx)) < 0)
  80. goto fail;
  81. return 0;
  82. fail:
  83. if (*filt_ctx)
  84. avfilter_free(*filt_ctx);
  85. *filt_ctx = NULL;
  86. return ret;
  87. }
  88. void avfilter_graph_set_auto_convert(AVFilterGraph *graph, unsigned flags)
  89. {
  90. graph->disable_auto_convert = flags;
  91. }
  92. /**
  93. * Check for the validity of graph.
  94. *
  95. * A graph is considered valid if all its input and output pads are
  96. * connected.
  97. *
  98. * @return 0 in case of success, a negative value otherwise
  99. */
  100. static int graph_check_validity(AVFilterGraph *graph, AVClass *log_ctx)
  101. {
  102. AVFilterContext *filt;
  103. int i, j;
  104. for (i = 0; i < graph->filter_count; i++) {
  105. filt = graph->filters[i];
  106. for (j = 0; j < filt->nb_inputs; j++) {
  107. if (!filt->inputs[j] || !filt->inputs[j]->src) {
  108. av_log(log_ctx, AV_LOG_ERROR,
  109. "Input pad \"%s\" for the filter \"%s\" of type \"%s\" not connected to any source\n",
  110. filt->input_pads[j].name, filt->name, filt->filter->name);
  111. return AVERROR(EINVAL);
  112. }
  113. }
  114. for (j = 0; j < filt->nb_outputs; j++) {
  115. if (!filt->outputs[j] || !filt->outputs[j]->dst) {
  116. av_log(log_ctx, AV_LOG_ERROR,
  117. "Output pad \"%s\" for the filter \"%s\" of type \"%s\" not connected to any destination\n",
  118. filt->output_pads[j].name, filt->name, filt->filter->name);
  119. return AVERROR(EINVAL);
  120. }
  121. }
  122. }
  123. return 0;
  124. }
  125. /**
  126. * Configure all the links of graphctx.
  127. *
  128. * @return 0 in case of success, a negative value otherwise
  129. */
  130. static int graph_config_links(AVFilterGraph *graph, AVClass *log_ctx)
  131. {
  132. AVFilterContext *filt;
  133. int i, ret;
  134. for (i=0; i < graph->filter_count; i++) {
  135. filt = graph->filters[i];
  136. if (!filt->nb_outputs) {
  137. if ((ret = avfilter_config_links(filt)))
  138. return ret;
  139. }
  140. }
  141. return 0;
  142. }
  143. AVFilterContext *avfilter_graph_get_filter(AVFilterGraph *graph, char *name)
  144. {
  145. int i;
  146. for (i = 0; i < graph->filter_count; i++)
  147. if (graph->filters[i]->name && !strcmp(name, graph->filters[i]->name))
  148. return graph->filters[i];
  149. return NULL;
  150. }
  151. static int filter_query_formats(AVFilterContext *ctx)
  152. {
  153. int ret;
  154. AVFilterFormats *formats;
  155. AVFilterChannelLayouts *chlayouts;
  156. AVFilterFormats *samplerates;
  157. enum AVMediaType type = ctx->inputs && ctx->inputs [0] ? ctx->inputs [0]->type :
  158. ctx->outputs && ctx->outputs[0] ? ctx->outputs[0]->type :
  159. AVMEDIA_TYPE_VIDEO;
  160. if ((ret = ctx->filter->query_formats(ctx)) < 0)
  161. return ret;
  162. formats = ff_all_formats(type);
  163. if (!formats)
  164. return AVERROR(ENOMEM);
  165. ff_set_common_formats(ctx, formats);
  166. if (type == AVMEDIA_TYPE_AUDIO) {
  167. samplerates = ff_all_samplerates();
  168. if (!samplerates)
  169. return AVERROR(ENOMEM);
  170. ff_set_common_samplerates(ctx, samplerates);
  171. chlayouts = ff_all_channel_layouts();
  172. if (!chlayouts)
  173. return AVERROR(ENOMEM);
  174. ff_set_common_channel_layouts(ctx, chlayouts);
  175. }
  176. return 0;
  177. }
  178. static int insert_conv_filter(AVFilterGraph *graph, AVFilterLink *link,
  179. const char *filt_name, const char *filt_args)
  180. {
  181. static int auto_count = 0, ret;
  182. char inst_name[32];
  183. AVFilterContext *filt_ctx;
  184. if (graph->disable_auto_convert) {
  185. av_log(NULL, AV_LOG_ERROR,
  186. "The filters '%s' and '%s' do not have a common format "
  187. "and automatic conversion is disabled.\n",
  188. link->src->name, link->dst->name);
  189. return AVERROR(EINVAL);
  190. }
  191. snprintf(inst_name, sizeof(inst_name), "auto-inserted %s %d",
  192. filt_name, auto_count++);
  193. if ((ret = avfilter_graph_create_filter(&filt_ctx,
  194. avfilter_get_by_name(filt_name),
  195. inst_name, filt_args, NULL, graph)) < 0)
  196. return ret;
  197. if ((ret = avfilter_insert_filter(link, filt_ctx, 0, 0)) < 0)
  198. return ret;
  199. filter_query_formats(filt_ctx);
  200. if ( ((link = filt_ctx-> inputs[0]) &&
  201. !ff_merge_formats(link->in_formats, link->out_formats)) ||
  202. ((link = filt_ctx->outputs[0]) &&
  203. !ff_merge_formats(link->in_formats, link->out_formats))
  204. ) {
  205. av_log(NULL, AV_LOG_ERROR,
  206. "Impossible to convert between the formats supported by the filter "
  207. "'%s' and the filter '%s'\n", link->src->name, link->dst->name);
  208. return AVERROR(EINVAL);
  209. }
  210. if (link->type == AVMEDIA_TYPE_AUDIO &&
  211. (((link = filt_ctx-> inputs[0]) &&
  212. !ff_merge_channel_layouts(link->in_channel_layouts, link->out_channel_layouts)) ||
  213. ((link = filt_ctx->outputs[0]) &&
  214. !ff_merge_channel_layouts(link->in_channel_layouts, link->out_channel_layouts)))
  215. ) {
  216. av_log(NULL, AV_LOG_ERROR,
  217. "Impossible to convert between the channel layouts formats supported by the filter "
  218. "'%s' and the filter '%s'\n", link->src->name, link->dst->name);
  219. return AVERROR(EINVAL);
  220. }
  221. return 0;
  222. }
  223. static int query_formats(AVFilterGraph *graph, AVClass *log_ctx)
  224. {
  225. int i, j, ret;
  226. char filt_args[128];
  227. AVFilterFormats *formats;
  228. AVFilterChannelLayouts *chlayouts;
  229. AVFilterFormats *samplerates;
  230. int scaler_count = 0, resampler_count = 0;
  231. for (j = 0; j < 2; j++) {
  232. /* ask all the sub-filters for their supported media formats */
  233. for (i = 0; i < graph->filter_count; i++) {
  234. /* Call query_formats on sources first.
  235. This is a temporary workaround for amerge,
  236. until format renegociation is implemented. */
  237. if (!graph->filters[i]->nb_inputs == j)
  238. continue;
  239. if (graph->filters[i]->filter->query_formats)
  240. ret = filter_query_formats(graph->filters[i]);
  241. else
  242. ret = ff_default_query_formats(graph->filters[i]);
  243. if (ret < 0)
  244. return ret;
  245. }
  246. }
  247. /* go through and merge as many format lists as possible */
  248. for (i = 0; i < graph->filter_count; i++) {
  249. AVFilterContext *filter = graph->filters[i];
  250. for (j = 0; j < filter->nb_inputs; j++) {
  251. AVFilterLink *link = filter->inputs[j];
  252. #if 0
  253. if (!link) continue;
  254. if (!link->in_formats || !link->out_formats)
  255. return AVERROR(EINVAL);
  256. if (link->type == AVMEDIA_TYPE_VIDEO &&
  257. !ff_merge_formats(link->in_formats, link->out_formats)) {
  258. /* couldn't merge format lists, auto-insert scale filter */
  259. snprintf(filt_args, sizeof(filt_args), "0:0:%s",
  260. graph->scale_sws_opts);
  261. if (ret = insert_conv_filter(graph, link, "scale", filt_args))
  262. return ret;
  263. }
  264. else if (link->type == AVMEDIA_TYPE_AUDIO) {
  265. if (!link->in_channel_layouts || !link->out_channel_layouts)
  266. return AVERROR(EINVAL);
  267. /* Merge all three list before checking: that way, in all
  268. * three categories, aconvert will use a common format
  269. * whenever possible. */
  270. formats = ff_merge_formats(link->in_formats, link->out_formats);
  271. chlayouts = ff_merge_channel_layouts(link->in_channel_layouts , link->out_channel_layouts);
  272. samplerates = ff_merge_samplerates (link->in_samplerates, link->out_samplerates);
  273. if (!formats || !chlayouts || !samplerates)
  274. if (ret = insert_conv_filter(graph, link, "aresample", NULL))
  275. return ret;
  276. #else
  277. int convert_needed = 0;
  278. if (!link)
  279. continue;
  280. if (link->in_formats != link->out_formats &&
  281. !ff_merge_formats(link->in_formats,
  282. link->out_formats))
  283. convert_needed = 1;
  284. if (link->type == AVMEDIA_TYPE_AUDIO) {
  285. if (link->in_channel_layouts != link->out_channel_layouts &&
  286. !ff_merge_channel_layouts(link->in_channel_layouts,
  287. link->out_channel_layouts))
  288. convert_needed = 1;
  289. if (link->in_samplerates != link->out_samplerates &&
  290. !ff_merge_samplerates(link->in_samplerates,
  291. link->out_samplerates))
  292. convert_needed = 1;
  293. }
  294. if (convert_needed) {
  295. AVFilterContext *convert;
  296. AVFilter *filter;
  297. AVFilterLink *inlink, *outlink;
  298. char scale_args[256];
  299. char inst_name[30];
  300. /* couldn't merge format lists. auto-insert conversion filter */
  301. switch (link->type) {
  302. case AVMEDIA_TYPE_VIDEO:
  303. if (!(filter = avfilter_get_by_name("scale"))) {
  304. av_log(log_ctx, AV_LOG_ERROR, "'scale' filter "
  305. "not present, cannot convert pixel formats.\n");
  306. return AVERROR(EINVAL);
  307. }
  308. snprintf(inst_name, sizeof(inst_name), "auto-inserted scaler %d",
  309. scaler_count++);
  310. snprintf(scale_args, sizeof(scale_args), "0:0:%s", graph->scale_sws_opts);
  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_descriptors[ref->format].nb_components % 2 == 0;
  370. enum PixelFormat best= PIX_FMT_NONE;
  371. int i;
  372. for (i=0; i<link->in_formats->format_count; i++) {
  373. enum PixelFormat p = link->in_formats->formats[i];
  374. best= avcodec_find_best_pix_fmt2(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, 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. FFSWAP(uint64_t, outlink->in_channel_layouts->channel_layouts[0],
  591. outlink->in_channel_layouts->channel_layouts[best_idx]);
  592. }
  593. }
  594. static void swap_channel_layouts(AVFilterGraph *graph)
  595. {
  596. int i;
  597. for (i = 0; i < graph->filter_count; i++)
  598. swap_channel_layouts_on_filter(graph->filters[i]);
  599. }
  600. static void swap_sample_fmts_on_filter(AVFilterContext *filter)
  601. {
  602. AVFilterLink *link = NULL;
  603. int format, bps;
  604. int i, j;
  605. for (i = 0; i < filter->nb_inputs; i++) {
  606. link = filter->inputs[i];
  607. if (link->type == AVMEDIA_TYPE_AUDIO &&
  608. link->out_formats->format_count == 1)
  609. break;
  610. }
  611. if (i == filter->nb_inputs)
  612. return;
  613. format = link->out_formats->formats[0];
  614. bps = av_get_bytes_per_sample(format);
  615. for (i = 0; i < filter->nb_outputs; i++) {
  616. AVFilterLink *outlink = filter->outputs[i];
  617. int best_idx = -1, best_score = INT_MIN;
  618. if (outlink->type != AVMEDIA_TYPE_AUDIO ||
  619. outlink->in_formats->format_count < 2)
  620. continue;
  621. for (j = 0; j < outlink->in_formats->format_count; j++) {
  622. int out_format = outlink->in_formats->formats[j];
  623. int out_bps = av_get_bytes_per_sample(out_format);
  624. int score;
  625. if (av_get_packed_sample_fmt(out_format) == format ||
  626. av_get_planar_sample_fmt(out_format) == format) {
  627. best_idx = j;
  628. break;
  629. }
  630. /* for s32 and float prefer double to prevent loss of information */
  631. if (bps == 4 && out_bps == 8) {
  632. best_idx = j;
  633. break;
  634. }
  635. /* prefer closest higher or equal bps */
  636. score = -abs(out_bps - bps);
  637. if (out_bps >= bps)
  638. score += INT_MAX/2;
  639. if (score > best_score) {
  640. best_score = score;
  641. best_idx = j;
  642. }
  643. }
  644. av_assert0(best_idx >= 0);
  645. FFSWAP(int, outlink->in_formats->formats[0],
  646. outlink->in_formats->formats[best_idx]);
  647. }
  648. }
  649. static void swap_sample_fmts(AVFilterGraph *graph)
  650. {
  651. int i;
  652. for (i = 0; i < graph->filter_count; i++)
  653. swap_sample_fmts_on_filter(graph->filters[i]);
  654. }
  655. static int pick_formats(AVFilterGraph *graph)
  656. {
  657. int i, j, ret;
  658. int change;
  659. do{
  660. change = 0;
  661. for (i = 0; i < graph->filter_count; i++) {
  662. AVFilterContext *filter = graph->filters[i];
  663. if (filter->nb_inputs){
  664. for (j = 0; j < filter->nb_inputs; j++){
  665. if(filter->inputs[j]->in_formats && filter->inputs[j]->in_formats->format_count == 1) {
  666. pick_format(filter->inputs[j], NULL);
  667. change = 1;
  668. }
  669. }
  670. }
  671. if (filter->nb_outputs){
  672. for (j = 0; j < filter->nb_outputs; j++){
  673. if(filter->outputs[j]->in_formats && filter->outputs[j]->in_formats->format_count == 1) {
  674. pick_format(filter->outputs[j], NULL);
  675. change = 1;
  676. }
  677. }
  678. }
  679. if (filter->nb_inputs && filter->nb_outputs && filter->inputs[0]->format>=0) {
  680. for (j = 0; j < filter->nb_outputs; j++) {
  681. if(filter->outputs[j]->format<0) {
  682. pick_format(filter->outputs[j], filter->inputs[0]);
  683. change = 1;
  684. }
  685. }
  686. }
  687. }
  688. }while(change);
  689. for (i = 0; i < graph->filter_count; i++) {
  690. AVFilterContext *filter = graph->filters[i];
  691. for (j = 0; j < filter->nb_inputs; j++)
  692. if ((ret = pick_format(filter->inputs[j], NULL)) < 0)
  693. return ret;
  694. for (j = 0; j < filter->nb_outputs; j++)
  695. if ((ret = pick_format(filter->outputs[j], NULL)) < 0)
  696. return ret;
  697. }
  698. return 0;
  699. }
  700. /**
  701. * Configure the formats of all the links in the graph.
  702. */
  703. static int graph_config_formats(AVFilterGraph *graph, AVClass *log_ctx)
  704. {
  705. int ret;
  706. /* find supported formats from sub-filters, and merge along links */
  707. if ((ret = query_formats(graph, log_ctx)) < 0)
  708. return ret;
  709. /* Once everything is merged, it's possible that we'll still have
  710. * multiple valid media format choices. We try to minimize the amount
  711. * of format conversion inside filters */
  712. reduce_formats(graph);
  713. /* for audio filters, ensure the best format, sample rate and channel layout
  714. * is selected */
  715. swap_sample_fmts(graph);
  716. swap_samplerates(graph);
  717. swap_channel_layouts(graph);
  718. if ((ret = pick_formats(graph)) < 0)
  719. return ret;
  720. return 0;
  721. }
  722. static int ff_avfilter_graph_config_pointers(AVFilterGraph *graph,
  723. AVClass *log_ctx)
  724. {
  725. unsigned i, j;
  726. int sink_links_count = 0, n = 0;
  727. AVFilterContext *f;
  728. AVFilterLink **sinks;
  729. for (i = 0; i < graph->filter_count; i++) {
  730. f = graph->filters[i];
  731. for (j = 0; j < f->nb_inputs; j++) {
  732. f->inputs[j]->graph = graph;
  733. f->inputs[j]->age_index = -1;
  734. }
  735. for (j = 0; j < f->nb_outputs; j++) {
  736. f->outputs[j]->graph = graph;
  737. f->outputs[j]->age_index= -1;
  738. }
  739. if (!f->nb_outputs) {
  740. if (f->nb_inputs > INT_MAX - sink_links_count)
  741. return AVERROR(EINVAL);
  742. sink_links_count += f->nb_inputs;
  743. }
  744. }
  745. sinks = av_calloc(sink_links_count, sizeof(*sinks));
  746. if (!sinks)
  747. return AVERROR(ENOMEM);
  748. for (i = 0; i < graph->filter_count; i++) {
  749. f = graph->filters[i];
  750. if (!f->nb_outputs) {
  751. for (j = 0; j < f->nb_inputs; j++) {
  752. sinks[n] = f->inputs[j];
  753. f->inputs[j]->age_index = n++;
  754. }
  755. }
  756. }
  757. av_assert0(n == sink_links_count);
  758. graph->sink_links = sinks;
  759. graph->sink_links_count = sink_links_count;
  760. return 0;
  761. }
  762. static int graph_insert_fifos(AVFilterGraph *graph, AVClass *log_ctx)
  763. {
  764. AVFilterContext *f;
  765. int i, j, ret;
  766. int fifo_count = 0;
  767. for (i = 0; i < graph->filter_count; i++) {
  768. f = graph->filters[i];
  769. for (j = 0; j < f->nb_inputs; j++) {
  770. AVFilterLink *link = f->inputs[j];
  771. AVFilterContext *fifo_ctx;
  772. AVFilter *fifo;
  773. char name[32];
  774. if (!link->dstpad->needs_fifo)
  775. continue;
  776. fifo = f->inputs[j]->type == AVMEDIA_TYPE_VIDEO ?
  777. avfilter_get_by_name("fifo") :
  778. avfilter_get_by_name("afifo");
  779. snprintf(name, sizeof(name), "auto-inserted fifo %d", fifo_count++);
  780. ret = avfilter_graph_create_filter(&fifo_ctx, fifo, name, NULL,
  781. NULL, graph);
  782. if (ret < 0)
  783. return ret;
  784. ret = avfilter_insert_filter(link, fifo_ctx, 0, 0);
  785. if (ret < 0)
  786. return ret;
  787. }
  788. }
  789. return 0;
  790. }
  791. int avfilter_graph_config(AVFilterGraph *graphctx, void *log_ctx)
  792. {
  793. int ret;
  794. if ((ret = graph_check_validity(graphctx, log_ctx)))
  795. return ret;
  796. if ((ret = graph_insert_fifos(graphctx, log_ctx)) < 0)
  797. return ret;
  798. if ((ret = graph_config_formats(graphctx, log_ctx)))
  799. return ret;
  800. if ((ret = graph_config_links(graphctx, log_ctx)))
  801. return ret;
  802. if ((ret = ff_avfilter_graph_config_pointers(graphctx, log_ctx)))
  803. return ret;
  804. return 0;
  805. }
  806. int avfilter_graph_send_command(AVFilterGraph *graph, const char *target, const char *cmd, const char *arg, char *res, int res_len, int flags)
  807. {
  808. int i, r = AVERROR(ENOSYS);
  809. if(!graph)
  810. return r;
  811. if((flags & AVFILTER_CMD_FLAG_ONE) && !(flags & AVFILTER_CMD_FLAG_FAST)) {
  812. r=avfilter_graph_send_command(graph, target, cmd, arg, res, res_len, flags | AVFILTER_CMD_FLAG_FAST);
  813. if(r != AVERROR(ENOSYS))
  814. return r;
  815. }
  816. if(res_len && res)
  817. res[0]= 0;
  818. for (i = 0; i < graph->filter_count; i++) {
  819. AVFilterContext *filter = graph->filters[i];
  820. if(!strcmp(target, "all") || (filter->name && !strcmp(target, filter->name)) || !strcmp(target, filter->filter->name)){
  821. r = avfilter_process_command(filter, cmd, arg, res, res_len, flags);
  822. if(r != AVERROR(ENOSYS)) {
  823. if((flags & AVFILTER_CMD_FLAG_ONE) || r<0)
  824. return r;
  825. }
  826. }
  827. }
  828. return r;
  829. }
  830. int avfilter_graph_queue_command(AVFilterGraph *graph, const char *target, const char *command, const char *arg, int flags, double ts)
  831. {
  832. int i;
  833. if(!graph)
  834. return 0;
  835. for (i = 0; i < graph->filter_count; i++) {
  836. AVFilterContext *filter = graph->filters[i];
  837. if(filter && (!strcmp(target, "all") || !strcmp(target, filter->name) || !strcmp(target, filter->filter->name))){
  838. AVFilterCommand **que = &filter->command_queue, *next;
  839. while(*que && (*que)->time <= ts)
  840. que = &(*que)->next;
  841. next= *que;
  842. *que= av_mallocz(sizeof(AVFilterCommand));
  843. (*que)->command = av_strdup(command);
  844. (*que)->arg = av_strdup(arg);
  845. (*que)->time = ts;
  846. (*que)->flags = flags;
  847. (*que)->next = next;
  848. if(flags & AVFILTER_CMD_FLAG_ONE)
  849. return 0;
  850. }
  851. }
  852. return 0;
  853. }
  854. static void heap_bubble_up(AVFilterGraph *graph,
  855. AVFilterLink *link, int index)
  856. {
  857. AVFilterLink **links = graph->sink_links;
  858. while (index) {
  859. int parent = (index - 1) >> 1;
  860. if (links[parent]->current_pts >= link->current_pts)
  861. break;
  862. links[index] = links[parent];
  863. links[index]->age_index = index;
  864. index = parent;
  865. }
  866. links[index] = link;
  867. link->age_index = index;
  868. }
  869. static void heap_bubble_down(AVFilterGraph *graph,
  870. AVFilterLink *link, int index)
  871. {
  872. AVFilterLink **links = graph->sink_links;
  873. while (1) {
  874. int child = 2 * index + 1;
  875. if (child >= graph->sink_links_count)
  876. break;
  877. if (child + 1 < graph->sink_links_count &&
  878. links[child + 1]->current_pts < links[child]->current_pts)
  879. child++;
  880. if (link->current_pts < links[child]->current_pts)
  881. break;
  882. links[index] = links[child];
  883. links[index]->age_index = index;
  884. index = child;
  885. }
  886. links[index] = link;
  887. link->age_index = index;
  888. }
  889. void ff_avfilter_graph_update_heap(AVFilterGraph *graph, AVFilterLink *link)
  890. {
  891. heap_bubble_up (graph, link, link->age_index);
  892. heap_bubble_down(graph, link, link->age_index);
  893. }
  894. int avfilter_graph_request_oldest(AVFilterGraph *graph)
  895. {
  896. while (graph->sink_links_count) {
  897. AVFilterLink *oldest = graph->sink_links[0];
  898. int r = ff_request_frame(oldest);
  899. if (r != AVERROR_EOF)
  900. return r;
  901. av_log(oldest->dst, AV_LOG_DEBUG, "EOF on sink link %s:%s.\n",
  902. oldest->dst ? oldest->dst->name : "unknown",
  903. oldest->dstpad ? oldest->dstpad->name : "unknown");
  904. /* EOF: remove the link from the heap */
  905. if (oldest->age_index < --graph->sink_links_count)
  906. heap_bubble_down(graph, graph->sink_links[graph->sink_links_count],
  907. oldest->age_index);
  908. oldest->age_index = -1;
  909. }
  910. return AVERROR_EOF;
  911. }