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.

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