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.

1159 lines
41KB

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