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.

1078 lines
38KB

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