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.

1449 lines
51KB

  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 "config.h"
  23. #include <string.h>
  24. #include "libavutil/avassert.h"
  25. #include "libavutil/avstring.h"
  26. #include "libavutil/bprint.h"
  27. #include "libavutil/channel_layout.h"
  28. #include "libavutil/internal.h"
  29. #include "libavutil/opt.h"
  30. #include "libavutil/pixdesc.h"
  31. #include "avfilter.h"
  32. #include "formats.h"
  33. #include "internal.h"
  34. #include "thread.h"
  35. #define OFFSET(x) offsetof(AVFilterGraph, x)
  36. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  37. static const AVOption filtergraph_options[] = {
  38. { "thread_type", "Allowed thread types", OFFSET(thread_type), AV_OPT_TYPE_FLAGS,
  39. { .i64 = AVFILTER_THREAD_SLICE }, 0, INT_MAX, FLAGS, "thread_type" },
  40. { "slice", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AVFILTER_THREAD_SLICE }, .flags = FLAGS, .unit = "thread_type" },
  41. { "threads", "Maximum number of threads", OFFSET(nb_threads),
  42. AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS },
  43. {"scale_sws_opts" , "default scale filter options" , OFFSET(scale_sws_opts) ,
  44. AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS },
  45. {"aresample_swr_opts" , "default aresample filter options" , OFFSET(aresample_swr_opts) ,
  46. AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS },
  47. { NULL },
  48. };
  49. static const AVClass filtergraph_class = {
  50. .class_name = "AVFilterGraph",
  51. .item_name = av_default_item_name,
  52. .version = LIBAVUTIL_VERSION_INT,
  53. .option = filtergraph_options,
  54. .category = AV_CLASS_CATEGORY_FILTER,
  55. };
  56. #if !HAVE_THREADS
  57. void ff_graph_thread_free(AVFilterGraph *graph)
  58. {
  59. }
  60. int ff_graph_thread_init(AVFilterGraph *graph)
  61. {
  62. graph->thread_type = 0;
  63. graph->nb_threads = 1;
  64. return 0;
  65. }
  66. #endif
  67. AVFilterGraph *avfilter_graph_alloc(void)
  68. {
  69. AVFilterGraph *ret = av_mallocz(sizeof(*ret));
  70. if (!ret)
  71. return NULL;
  72. ret->internal = av_mallocz(sizeof(*ret->internal));
  73. if (!ret->internal) {
  74. av_freep(&ret);
  75. return NULL;
  76. }
  77. ret->av_class = &filtergraph_class;
  78. av_opt_set_defaults(ret);
  79. return ret;
  80. }
  81. void ff_filter_graph_remove_filter(AVFilterGraph *graph, AVFilterContext *filter)
  82. {
  83. int i, j;
  84. for (i = 0; i < graph->nb_filters; i++) {
  85. if (graph->filters[i] == filter) {
  86. FFSWAP(AVFilterContext*, graph->filters[i],
  87. graph->filters[graph->nb_filters - 1]);
  88. graph->nb_filters--;
  89. filter->graph = NULL;
  90. for (j = 0; j<filter->nb_outputs; j++)
  91. if (filter->outputs[j])
  92. filter->outputs[j]->graph = NULL;
  93. return;
  94. }
  95. }
  96. }
  97. void avfilter_graph_free(AVFilterGraph **graph)
  98. {
  99. if (!*graph)
  100. return;
  101. while ((*graph)->nb_filters)
  102. avfilter_free((*graph)->filters[0]);
  103. ff_graph_thread_free(*graph);
  104. av_freep(&(*graph)->sink_links);
  105. av_freep(&(*graph)->scale_sws_opts);
  106. av_freep(&(*graph)->aresample_swr_opts);
  107. av_freep(&(*graph)->resample_lavr_opts);
  108. av_freep(&(*graph)->filters);
  109. av_freep(&(*graph)->internal);
  110. av_freep(graph);
  111. }
  112. #if FF_API_AVFILTER_OPEN
  113. int avfilter_graph_add_filter(AVFilterGraph *graph, AVFilterContext *filter)
  114. {
  115. AVFilterContext **filters = av_realloc(graph->filters,
  116. sizeof(*filters) * (graph->nb_filters + 1));
  117. if (!filters)
  118. return AVERROR(ENOMEM);
  119. graph->filters = filters;
  120. graph->filters[graph->nb_filters++] = filter;
  121. filter->graph = graph;
  122. return 0;
  123. }
  124. #endif
  125. int avfilter_graph_create_filter(AVFilterContext **filt_ctx, const AVFilter *filt,
  126. const char *name, const char *args, void *opaque,
  127. AVFilterGraph *graph_ctx)
  128. {
  129. int ret;
  130. *filt_ctx = avfilter_graph_alloc_filter(graph_ctx, filt, name);
  131. if (!*filt_ctx)
  132. return AVERROR(ENOMEM);
  133. ret = avfilter_init_str(*filt_ctx, args);
  134. if (ret < 0)
  135. goto fail;
  136. return 0;
  137. fail:
  138. if (*filt_ctx)
  139. avfilter_free(*filt_ctx);
  140. *filt_ctx = NULL;
  141. return ret;
  142. }
  143. void avfilter_graph_set_auto_convert(AVFilterGraph *graph, unsigned flags)
  144. {
  145. graph->disable_auto_convert = flags;
  146. }
  147. AVFilterContext *avfilter_graph_alloc_filter(AVFilterGraph *graph,
  148. const AVFilter *filter,
  149. const char *name)
  150. {
  151. AVFilterContext **filters, *s;
  152. if (graph->thread_type && !graph->internal->thread_execute) {
  153. if (graph->execute) {
  154. graph->internal->thread_execute = graph->execute;
  155. } else {
  156. int ret = ff_graph_thread_init(graph);
  157. if (ret < 0) {
  158. av_log(graph, AV_LOG_ERROR, "Error initializing threading.\n");
  159. return NULL;
  160. }
  161. }
  162. }
  163. s = ff_filter_alloc(filter, name);
  164. if (!s)
  165. return NULL;
  166. filters = av_realloc(graph->filters, sizeof(*filters) * (graph->nb_filters + 1));
  167. if (!filters) {
  168. avfilter_free(s);
  169. return NULL;
  170. }
  171. graph->filters = filters;
  172. graph->filters[graph->nb_filters++] = s;
  173. s->graph = graph;
  174. return s;
  175. }
  176. /**
  177. * Check for the validity of graph.
  178. *
  179. * A graph is considered valid if all its input and output pads are
  180. * connected.
  181. *
  182. * @return >= 0 in case of success, a negative value otherwise
  183. */
  184. static int graph_check_validity(AVFilterGraph *graph, AVClass *log_ctx)
  185. {
  186. AVFilterContext *filt;
  187. int i, j;
  188. for (i = 0; i < graph->nb_filters; i++) {
  189. const AVFilterPad *pad;
  190. filt = graph->filters[i];
  191. for (j = 0; j < filt->nb_inputs; j++) {
  192. if (!filt->inputs[j] || !filt->inputs[j]->src) {
  193. pad = &filt->input_pads[j];
  194. av_log(log_ctx, AV_LOG_ERROR,
  195. "Input pad \"%s\" with type %s of the filter instance \"%s\" of %s not connected to any source\n",
  196. pad->name, av_get_media_type_string(pad->type), filt->name, filt->filter->name);
  197. return AVERROR(EINVAL);
  198. }
  199. }
  200. for (j = 0; j < filt->nb_outputs; j++) {
  201. if (!filt->outputs[j] || !filt->outputs[j]->dst) {
  202. pad = &filt->output_pads[j];
  203. av_log(log_ctx, AV_LOG_ERROR,
  204. "Output pad \"%s\" with type %s of the filter instance \"%s\" of %s not connected to any destination\n",
  205. pad->name, av_get_media_type_string(pad->type), filt->name, filt->filter->name);
  206. return AVERROR(EINVAL);
  207. }
  208. }
  209. }
  210. return 0;
  211. }
  212. /**
  213. * Configure all the links of graphctx.
  214. *
  215. * @return >= 0 in case of success, a negative value otherwise
  216. */
  217. static int graph_config_links(AVFilterGraph *graph, AVClass *log_ctx)
  218. {
  219. AVFilterContext *filt;
  220. int i, ret;
  221. for (i = 0; i < graph->nb_filters; i++) {
  222. filt = graph->filters[i];
  223. if (!filt->nb_outputs) {
  224. if ((ret = avfilter_config_links(filt)))
  225. return ret;
  226. }
  227. }
  228. return 0;
  229. }
  230. AVFilterContext *avfilter_graph_get_filter(AVFilterGraph *graph, const char *name)
  231. {
  232. int i;
  233. for (i = 0; i < graph->nb_filters; i++)
  234. if (graph->filters[i]->name && !strcmp(name, graph->filters[i]->name))
  235. return graph->filters[i];
  236. return NULL;
  237. }
  238. static void sanitize_channel_layouts(void *log, AVFilterChannelLayouts *l)
  239. {
  240. if (!l)
  241. return;
  242. if (l->nb_channel_layouts) {
  243. if (l->all_layouts || l->all_counts)
  244. av_log(log, AV_LOG_WARNING, "All layouts set on non-empty list\n");
  245. l->all_layouts = l->all_counts = 0;
  246. } else {
  247. if (l->all_counts && !l->all_layouts)
  248. av_log(log, AV_LOG_WARNING, "All counts without all layouts\n");
  249. l->all_layouts = 1;
  250. }
  251. }
  252. static int filter_query_formats(AVFilterContext *ctx)
  253. {
  254. int ret, i;
  255. AVFilterFormats *formats;
  256. AVFilterChannelLayouts *chlayouts;
  257. AVFilterFormats *samplerates;
  258. enum AVMediaType type = ctx->inputs && ctx->inputs [0] ? ctx->inputs [0]->type :
  259. ctx->outputs && ctx->outputs[0] ? ctx->outputs[0]->type :
  260. AVMEDIA_TYPE_VIDEO;
  261. if ((ret = ctx->filter->query_formats(ctx)) < 0) {
  262. if (ret != AVERROR(EAGAIN))
  263. av_log(ctx, AV_LOG_ERROR, "Query format failed for '%s': %s\n",
  264. ctx->name, av_err2str(ret));
  265. return ret;
  266. }
  267. for (i = 0; i < ctx->nb_inputs; i++)
  268. sanitize_channel_layouts(ctx, ctx->inputs[i]->out_channel_layouts);
  269. for (i = 0; i < ctx->nb_outputs; i++)
  270. sanitize_channel_layouts(ctx, ctx->outputs[i]->in_channel_layouts);
  271. formats = ff_all_formats(type);
  272. if ((ret = ff_set_common_formats(ctx, formats)) < 0)
  273. return ret;
  274. if (type == AVMEDIA_TYPE_AUDIO) {
  275. samplerates = ff_all_samplerates();
  276. if ((ret = ff_set_common_samplerates(ctx, samplerates)) < 0)
  277. return ret;
  278. chlayouts = ff_all_channel_layouts();
  279. if ((ret = ff_set_common_channel_layouts(ctx, chlayouts)) < 0)
  280. return ret;
  281. }
  282. return 0;
  283. }
  284. static int formats_declared(AVFilterContext *f)
  285. {
  286. int i;
  287. for (i = 0; i < f->nb_inputs; i++) {
  288. if (!f->inputs[i]->out_formats)
  289. return 0;
  290. if (f->inputs[i]->type == AVMEDIA_TYPE_AUDIO &&
  291. !(f->inputs[i]->out_samplerates &&
  292. f->inputs[i]->out_channel_layouts))
  293. return 0;
  294. }
  295. for (i = 0; i < f->nb_outputs; i++) {
  296. if (!f->outputs[i]->in_formats)
  297. return 0;
  298. if (f->outputs[i]->type == AVMEDIA_TYPE_AUDIO &&
  299. !(f->outputs[i]->in_samplerates &&
  300. f->outputs[i]->in_channel_layouts))
  301. return 0;
  302. }
  303. return 1;
  304. }
  305. static AVFilterFormats *clone_filter_formats(AVFilterFormats *arg)
  306. {
  307. AVFilterFormats *a = av_memdup(arg, sizeof(*arg));
  308. if (a) {
  309. a->refcount = 0;
  310. a->refs = NULL;
  311. a->formats = av_memdup(a->formats, sizeof(*a->formats) * a->nb_formats);
  312. if (!a->formats && arg->formats)
  313. av_freep(&a);
  314. }
  315. return a;
  316. }
  317. static int can_merge_formats(AVFilterFormats *a_arg,
  318. AVFilterFormats *b_arg,
  319. enum AVMediaType type,
  320. int is_sample_rate)
  321. {
  322. AVFilterFormats *a, *b, *ret;
  323. if (a_arg == b_arg)
  324. return 1;
  325. a = clone_filter_formats(a_arg);
  326. b = clone_filter_formats(b_arg);
  327. if (!a || !b) {
  328. if (a)
  329. av_freep(&a->formats);
  330. if (b)
  331. av_freep(&b->formats);
  332. av_freep(&a);
  333. av_freep(&b);
  334. return 0;
  335. }
  336. if (is_sample_rate) {
  337. ret = ff_merge_samplerates(a, b);
  338. } else {
  339. ret = ff_merge_formats(a, b, type);
  340. }
  341. if (ret) {
  342. av_freep(&ret->formats);
  343. av_freep(&ret->refs);
  344. av_freep(&ret);
  345. return 1;
  346. } else {
  347. av_freep(&a->formats);
  348. av_freep(&b->formats);
  349. av_freep(&a);
  350. av_freep(&b);
  351. return 0;
  352. }
  353. }
  354. /**
  355. * Perform one round of query_formats() and merging formats lists on the
  356. * filter graph.
  357. * @return >=0 if all links formats lists could be queried and merged;
  358. * AVERROR(EAGAIN) some progress was made in the queries or merging
  359. * and a later call may succeed;
  360. * AVERROR(EIO) (may be changed) plus a log message if no progress
  361. * was made and the negotiation is stuck;
  362. * a negative error code if some other error happened
  363. */
  364. static int query_formats(AVFilterGraph *graph, AVClass *log_ctx)
  365. {
  366. int i, j, ret;
  367. int scaler_count = 0, resampler_count = 0;
  368. int count_queried = 0; /* successful calls to query_formats() */
  369. int count_merged = 0; /* successful merge of formats lists */
  370. int count_already_merged = 0; /* lists already merged */
  371. int count_delayed = 0; /* lists that need to be merged later */
  372. for (i = 0; i < graph->nb_filters; i++) {
  373. AVFilterContext *f = graph->filters[i];
  374. if (formats_declared(f))
  375. continue;
  376. if (f->filter->query_formats)
  377. ret = filter_query_formats(f);
  378. else
  379. ret = ff_default_query_formats(f);
  380. if (ret < 0 && ret != AVERROR(EAGAIN))
  381. return ret;
  382. /* note: EAGAIN could indicate a partial success, not counted yet */
  383. count_queried += ret >= 0;
  384. }
  385. /* go through and merge as many format lists as possible */
  386. for (i = 0; i < graph->nb_filters; i++) {
  387. AVFilterContext *filter = graph->filters[i];
  388. for (j = 0; j < filter->nb_inputs; j++) {
  389. AVFilterLink *link = filter->inputs[j];
  390. int convert_needed = 0;
  391. if (!link)
  392. continue;
  393. if (link->in_formats != link->out_formats
  394. && link->in_formats && link->out_formats)
  395. if (!can_merge_formats(link->in_formats, link->out_formats,
  396. link->type, 0))
  397. convert_needed = 1;
  398. if (link->type == AVMEDIA_TYPE_AUDIO) {
  399. if (link->in_samplerates != link->out_samplerates
  400. && link->in_samplerates && link->out_samplerates)
  401. if (!can_merge_formats(link->in_samplerates,
  402. link->out_samplerates,
  403. 0, 1))
  404. convert_needed = 1;
  405. }
  406. #define MERGE_DISPATCH(field, statement) \
  407. if (!(link->in_ ## field && link->out_ ## field)) { \
  408. count_delayed++; \
  409. } else if (link->in_ ## field == link->out_ ## field) { \
  410. count_already_merged++; \
  411. } else if (!convert_needed) { \
  412. count_merged++; \
  413. statement \
  414. }
  415. if (link->type == AVMEDIA_TYPE_AUDIO) {
  416. MERGE_DISPATCH(channel_layouts,
  417. if (!ff_merge_channel_layouts(link->in_channel_layouts,
  418. link->out_channel_layouts))
  419. convert_needed = 1;
  420. )
  421. MERGE_DISPATCH(samplerates,
  422. if (!ff_merge_samplerates(link->in_samplerates,
  423. link->out_samplerates))
  424. convert_needed = 1;
  425. )
  426. }
  427. MERGE_DISPATCH(formats,
  428. if (!ff_merge_formats(link->in_formats, link->out_formats,
  429. link->type))
  430. convert_needed = 1;
  431. )
  432. #undef MERGE_DISPATCH
  433. if (convert_needed) {
  434. AVFilterContext *convert;
  435. AVFilter *filter;
  436. AVFilterLink *inlink, *outlink;
  437. char scale_args[256];
  438. char inst_name[30];
  439. if (graph->disable_auto_convert) {
  440. av_log(log_ctx, AV_LOG_ERROR,
  441. "The filters '%s' and '%s' do not have a common format "
  442. "and automatic conversion is disabled.\n",
  443. link->src->name, link->dst->name);
  444. return AVERROR(EINVAL);
  445. }
  446. /* couldn't merge format lists. auto-insert conversion filter */
  447. switch (link->type) {
  448. case AVMEDIA_TYPE_VIDEO:
  449. if (!(filter = avfilter_get_by_name("scale"))) {
  450. av_log(log_ctx, AV_LOG_ERROR, "'scale' filter "
  451. "not present, cannot convert pixel formats.\n");
  452. return AVERROR(EINVAL);
  453. }
  454. snprintf(inst_name, sizeof(inst_name), "auto-inserted scaler %d",
  455. scaler_count++);
  456. if ((ret = avfilter_graph_create_filter(&convert, filter,
  457. inst_name, graph->scale_sws_opts, NULL,
  458. graph)) < 0)
  459. return ret;
  460. break;
  461. case AVMEDIA_TYPE_AUDIO:
  462. if (!(filter = avfilter_get_by_name("aresample"))) {
  463. av_log(log_ctx, AV_LOG_ERROR, "'aresample' filter "
  464. "not present, cannot convert audio formats.\n");
  465. return AVERROR(EINVAL);
  466. }
  467. snprintf(inst_name, sizeof(inst_name), "auto-inserted resampler %d",
  468. resampler_count++);
  469. scale_args[0] = '\0';
  470. if (graph->aresample_swr_opts)
  471. snprintf(scale_args, sizeof(scale_args), "%s",
  472. graph->aresample_swr_opts);
  473. if ((ret = avfilter_graph_create_filter(&convert, filter,
  474. inst_name, graph->aresample_swr_opts,
  475. NULL, graph)) < 0)
  476. return ret;
  477. break;
  478. default:
  479. return AVERROR(EINVAL);
  480. }
  481. if ((ret = avfilter_insert_filter(link, convert, 0, 0)) < 0)
  482. return ret;
  483. if ((ret = filter_query_formats(convert)) < 0)
  484. return ret;
  485. inlink = convert->inputs[0];
  486. outlink = convert->outputs[0];
  487. av_assert0( inlink-> in_formats->refcount > 0);
  488. av_assert0( inlink->out_formats->refcount > 0);
  489. av_assert0(outlink-> in_formats->refcount > 0);
  490. av_assert0(outlink->out_formats->refcount > 0);
  491. if (outlink->type == AVMEDIA_TYPE_AUDIO) {
  492. av_assert0( inlink-> in_samplerates->refcount > 0);
  493. av_assert0( inlink->out_samplerates->refcount > 0);
  494. av_assert0(outlink-> in_samplerates->refcount > 0);
  495. av_assert0(outlink->out_samplerates->refcount > 0);
  496. av_assert0( inlink-> in_channel_layouts->refcount > 0);
  497. av_assert0( inlink->out_channel_layouts->refcount > 0);
  498. av_assert0(outlink-> in_channel_layouts->refcount > 0);
  499. av_assert0(outlink->out_channel_layouts->refcount > 0);
  500. }
  501. if (!ff_merge_formats( inlink->in_formats, inlink->out_formats, inlink->type) ||
  502. !ff_merge_formats(outlink->in_formats, outlink->out_formats, outlink->type))
  503. ret = AVERROR(ENOSYS);
  504. if (inlink->type == AVMEDIA_TYPE_AUDIO &&
  505. (!ff_merge_samplerates(inlink->in_samplerates,
  506. inlink->out_samplerates) ||
  507. !ff_merge_channel_layouts(inlink->in_channel_layouts,
  508. inlink->out_channel_layouts)))
  509. ret = AVERROR(ENOSYS);
  510. if (outlink->type == AVMEDIA_TYPE_AUDIO &&
  511. (!ff_merge_samplerates(outlink->in_samplerates,
  512. outlink->out_samplerates) ||
  513. !ff_merge_channel_layouts(outlink->in_channel_layouts,
  514. outlink->out_channel_layouts)))
  515. ret = AVERROR(ENOSYS);
  516. if (ret < 0) {
  517. av_log(log_ctx, AV_LOG_ERROR,
  518. "Impossible to convert between the formats supported by the filter "
  519. "'%s' and the filter '%s'\n", link->src->name, link->dst->name);
  520. return ret;
  521. }
  522. }
  523. }
  524. }
  525. av_log(graph, AV_LOG_DEBUG, "query_formats: "
  526. "%d queried, %d merged, %d already done, %d delayed\n",
  527. count_queried, count_merged, count_already_merged, count_delayed);
  528. if (count_delayed) {
  529. AVBPrint bp;
  530. /* if count_queried > 0, one filter at least did set its formats,
  531. that will give additional information to its neighbour;
  532. if count_merged > 0, one pair of formats lists at least was merged,
  533. that will give additional information to all connected filters;
  534. in both cases, progress was made and a new round must be done */
  535. if (count_queried || count_merged)
  536. return AVERROR(EAGAIN);
  537. av_bprint_init(&bp, 0, AV_BPRINT_SIZE_AUTOMATIC);
  538. for (i = 0; i < graph->nb_filters; i++)
  539. if (!formats_declared(graph->filters[i]))
  540. av_bprintf(&bp, "%s%s", bp.len ? ", " : "",
  541. graph->filters[i]->name);
  542. av_log(graph, AV_LOG_ERROR,
  543. "The following filters could not choose their formats: %s\n"
  544. "Consider inserting the (a)format filter near their input or "
  545. "output.\n", bp.str);
  546. return AVERROR(EIO);
  547. }
  548. return 0;
  549. }
  550. static int get_fmt_score(enum AVSampleFormat dst_fmt, enum AVSampleFormat src_fmt)
  551. {
  552. int score = 0;
  553. if (av_sample_fmt_is_planar(dst_fmt) != av_sample_fmt_is_planar(src_fmt))
  554. score ++;
  555. if (av_get_bytes_per_sample(dst_fmt) < av_get_bytes_per_sample(src_fmt)) {
  556. score += 100 * (av_get_bytes_per_sample(src_fmt) - av_get_bytes_per_sample(dst_fmt));
  557. }else
  558. score += 10 * (av_get_bytes_per_sample(dst_fmt) - av_get_bytes_per_sample(src_fmt));
  559. if (av_get_packed_sample_fmt(dst_fmt) == AV_SAMPLE_FMT_S32 &&
  560. av_get_packed_sample_fmt(src_fmt) == AV_SAMPLE_FMT_FLT)
  561. score += 20;
  562. if (av_get_packed_sample_fmt(dst_fmt) == AV_SAMPLE_FMT_FLT &&
  563. av_get_packed_sample_fmt(src_fmt) == AV_SAMPLE_FMT_S32)
  564. score += 2;
  565. return score;
  566. }
  567. static enum AVSampleFormat find_best_sample_fmt_of_2(enum AVSampleFormat dst_fmt1, enum AVSampleFormat dst_fmt2,
  568. enum AVSampleFormat src_fmt)
  569. {
  570. int score1, score2;
  571. score1 = get_fmt_score(dst_fmt1, src_fmt);
  572. score2 = get_fmt_score(dst_fmt2, src_fmt);
  573. return score1 < score2 ? dst_fmt1 : dst_fmt2;
  574. }
  575. static int pick_format(AVFilterLink *link, AVFilterLink *ref)
  576. {
  577. if (!link || !link->in_formats)
  578. return 0;
  579. if (link->type == AVMEDIA_TYPE_VIDEO) {
  580. if(ref && ref->type == AVMEDIA_TYPE_VIDEO){
  581. int has_alpha= av_pix_fmt_desc_get(ref->format)->nb_components % 2 == 0;
  582. enum AVPixelFormat best= AV_PIX_FMT_NONE;
  583. int i;
  584. for (i=0; i<link->in_formats->nb_formats; i++) {
  585. enum AVPixelFormat p = link->in_formats->formats[i];
  586. best= av_find_best_pix_fmt_of_2(best, p, ref->format, has_alpha, NULL);
  587. }
  588. av_log(link->src,AV_LOG_DEBUG, "picking %s out of %d ref:%s alpha:%d\n",
  589. av_get_pix_fmt_name(best), link->in_formats->nb_formats,
  590. av_get_pix_fmt_name(ref->format), has_alpha);
  591. link->in_formats->formats[0] = best;
  592. }
  593. } else if (link->type == AVMEDIA_TYPE_AUDIO) {
  594. if(ref && ref->type == AVMEDIA_TYPE_AUDIO){
  595. enum AVSampleFormat best= AV_SAMPLE_FMT_NONE;
  596. int i;
  597. for (i=0; i<link->in_formats->nb_formats; i++) {
  598. enum AVSampleFormat p = link->in_formats->formats[i];
  599. best = find_best_sample_fmt_of_2(best, p, ref->format);
  600. }
  601. av_log(link->src,AV_LOG_DEBUG, "picking %s out of %d ref:%s\n",
  602. av_get_sample_fmt_name(best), link->in_formats->nb_formats,
  603. av_get_sample_fmt_name(ref->format));
  604. link->in_formats->formats[0] = best;
  605. }
  606. }
  607. link->in_formats->nb_formats = 1;
  608. link->format = link->in_formats->formats[0];
  609. if (link->type == AVMEDIA_TYPE_AUDIO) {
  610. if (!link->in_samplerates->nb_formats) {
  611. av_log(link->src, AV_LOG_ERROR, "Cannot select sample rate for"
  612. " the link between filters %s and %s.\n", link->src->name,
  613. link->dst->name);
  614. return AVERROR(EINVAL);
  615. }
  616. link->in_samplerates->nb_formats = 1;
  617. link->sample_rate = link->in_samplerates->formats[0];
  618. if (link->in_channel_layouts->all_layouts) {
  619. av_log(link->src, AV_LOG_ERROR, "Cannot select channel layout for"
  620. " the link between filters %s and %s.\n", link->src->name,
  621. link->dst->name);
  622. if (!link->in_channel_layouts->all_counts)
  623. av_log(link->src, AV_LOG_ERROR, "Unknown channel layouts not "
  624. "supported, try specifying a channel layout using "
  625. "'aformat=channel_layouts=something'.\n");
  626. return AVERROR(EINVAL);
  627. }
  628. link->in_channel_layouts->nb_channel_layouts = 1;
  629. link->channel_layout = link->in_channel_layouts->channel_layouts[0];
  630. if ((link->channels = FF_LAYOUT2COUNT(link->channel_layout)))
  631. link->channel_layout = 0;
  632. else
  633. link->channels = av_get_channel_layout_nb_channels(link->channel_layout);
  634. }
  635. ff_formats_unref(&link->in_formats);
  636. ff_formats_unref(&link->out_formats);
  637. ff_formats_unref(&link->in_samplerates);
  638. ff_formats_unref(&link->out_samplerates);
  639. ff_channel_layouts_unref(&link->in_channel_layouts);
  640. ff_channel_layouts_unref(&link->out_channel_layouts);
  641. return 0;
  642. }
  643. #define REDUCE_FORMATS(fmt_type, list_type, list, var, nb, add_format, unref_format) \
  644. do { \
  645. for (i = 0; i < filter->nb_inputs; i++) { \
  646. AVFilterLink *link = filter->inputs[i]; \
  647. fmt_type fmt; \
  648. \
  649. if (!link->out_ ## list || link->out_ ## list->nb != 1) \
  650. continue; \
  651. fmt = link->out_ ## list->var[0]; \
  652. \
  653. for (j = 0; j < filter->nb_outputs; j++) { \
  654. AVFilterLink *out_link = filter->outputs[j]; \
  655. list_type *fmts; \
  656. \
  657. if (link->type != out_link->type || \
  658. out_link->in_ ## list->nb == 1) \
  659. continue; \
  660. fmts = out_link->in_ ## list; \
  661. \
  662. if (!out_link->in_ ## list->nb) { \
  663. if ((ret = add_format(&out_link->in_ ##list, fmt)) < 0)\
  664. return ret; \
  665. ret = 1; \
  666. break; \
  667. } \
  668. \
  669. for (k = 0; k < out_link->in_ ## list->nb; k++) \
  670. if (fmts->var[k] == fmt) { \
  671. fmts->var[0] = fmt; \
  672. fmts->nb = 1; \
  673. ret = 1; \
  674. break; \
  675. } \
  676. } \
  677. } \
  678. } while (0)
  679. static int reduce_formats_on_filter(AVFilterContext *filter)
  680. {
  681. int i, j, k, ret = 0;
  682. REDUCE_FORMATS(int, AVFilterFormats, formats, formats,
  683. nb_formats, ff_add_format, ff_formats_unref);
  684. REDUCE_FORMATS(int, AVFilterFormats, samplerates, formats,
  685. nb_formats, ff_add_format, ff_formats_unref);
  686. /* reduce channel layouts */
  687. for (i = 0; i < filter->nb_inputs; i++) {
  688. AVFilterLink *inlink = filter->inputs[i];
  689. uint64_t fmt;
  690. if (!inlink->out_channel_layouts ||
  691. inlink->out_channel_layouts->nb_channel_layouts != 1)
  692. continue;
  693. fmt = inlink->out_channel_layouts->channel_layouts[0];
  694. for (j = 0; j < filter->nb_outputs; j++) {
  695. AVFilterLink *outlink = filter->outputs[j];
  696. AVFilterChannelLayouts *fmts;
  697. fmts = outlink->in_channel_layouts;
  698. if (inlink->type != outlink->type || fmts->nb_channel_layouts == 1)
  699. continue;
  700. if (fmts->all_layouts &&
  701. (!FF_LAYOUT2COUNT(fmt) || fmts->all_counts)) {
  702. /* Turn the infinite list into a singleton */
  703. fmts->all_layouts = fmts->all_counts = 0;
  704. if (ff_add_channel_layout(&outlink->in_channel_layouts, fmt) < 0)
  705. ret = 1;
  706. break;
  707. }
  708. for (k = 0; k < outlink->in_channel_layouts->nb_channel_layouts; k++) {
  709. if (fmts->channel_layouts[k] == fmt) {
  710. fmts->channel_layouts[0] = fmt;
  711. fmts->nb_channel_layouts = 1;
  712. ret = 1;
  713. break;
  714. }
  715. }
  716. }
  717. }
  718. return ret;
  719. }
  720. static int reduce_formats(AVFilterGraph *graph)
  721. {
  722. int i, reduced, ret;
  723. do {
  724. reduced = 0;
  725. for (i = 0; i < graph->nb_filters; i++) {
  726. if ((ret = reduce_formats_on_filter(graph->filters[i])) < 0)
  727. return ret;
  728. reduced |= ret;
  729. }
  730. } while (reduced);
  731. return 0;
  732. }
  733. static void swap_samplerates_on_filter(AVFilterContext *filter)
  734. {
  735. AVFilterLink *link = NULL;
  736. int sample_rate;
  737. int i, j;
  738. for (i = 0; i < filter->nb_inputs; i++) {
  739. link = filter->inputs[i];
  740. if (link->type == AVMEDIA_TYPE_AUDIO &&
  741. link->out_samplerates->nb_formats== 1)
  742. break;
  743. }
  744. if (i == filter->nb_inputs)
  745. return;
  746. sample_rate = link->out_samplerates->formats[0];
  747. for (i = 0; i < filter->nb_outputs; i++) {
  748. AVFilterLink *outlink = filter->outputs[i];
  749. int best_idx, best_diff = INT_MAX;
  750. if (outlink->type != AVMEDIA_TYPE_AUDIO ||
  751. outlink->in_samplerates->nb_formats < 2)
  752. continue;
  753. for (j = 0; j < outlink->in_samplerates->nb_formats; j++) {
  754. int diff = abs(sample_rate - outlink->in_samplerates->formats[j]);
  755. if (diff < best_diff) {
  756. best_diff = diff;
  757. best_idx = j;
  758. }
  759. }
  760. FFSWAP(int, outlink->in_samplerates->formats[0],
  761. outlink->in_samplerates->formats[best_idx]);
  762. }
  763. }
  764. static void swap_samplerates(AVFilterGraph *graph)
  765. {
  766. int i;
  767. for (i = 0; i < graph->nb_filters; i++)
  768. swap_samplerates_on_filter(graph->filters[i]);
  769. }
  770. #define CH_CENTER_PAIR (AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_RIGHT_OF_CENTER)
  771. #define CH_FRONT_PAIR (AV_CH_FRONT_LEFT | AV_CH_FRONT_RIGHT)
  772. #define CH_STEREO_PAIR (AV_CH_STEREO_LEFT | AV_CH_STEREO_RIGHT)
  773. #define CH_WIDE_PAIR (AV_CH_WIDE_LEFT | AV_CH_WIDE_RIGHT)
  774. #define CH_SIDE_PAIR (AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT)
  775. #define CH_DIRECT_PAIR (AV_CH_SURROUND_DIRECT_LEFT | AV_CH_SURROUND_DIRECT_RIGHT)
  776. #define CH_BACK_PAIR (AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT)
  777. /* allowable substitutions for channel pairs when comparing layouts,
  778. * ordered by priority for both values */
  779. static const uint64_t ch_subst[][2] = {
  780. { CH_FRONT_PAIR, CH_CENTER_PAIR },
  781. { CH_FRONT_PAIR, CH_WIDE_PAIR },
  782. { CH_FRONT_PAIR, AV_CH_FRONT_CENTER },
  783. { CH_CENTER_PAIR, CH_FRONT_PAIR },
  784. { CH_CENTER_PAIR, CH_WIDE_PAIR },
  785. { CH_CENTER_PAIR, AV_CH_FRONT_CENTER },
  786. { CH_WIDE_PAIR, CH_FRONT_PAIR },
  787. { CH_WIDE_PAIR, CH_CENTER_PAIR },
  788. { CH_WIDE_PAIR, AV_CH_FRONT_CENTER },
  789. { AV_CH_FRONT_CENTER, CH_FRONT_PAIR },
  790. { AV_CH_FRONT_CENTER, CH_CENTER_PAIR },
  791. { AV_CH_FRONT_CENTER, CH_WIDE_PAIR },
  792. { CH_SIDE_PAIR, CH_DIRECT_PAIR },
  793. { CH_SIDE_PAIR, CH_BACK_PAIR },
  794. { CH_SIDE_PAIR, AV_CH_BACK_CENTER },
  795. { CH_BACK_PAIR, CH_DIRECT_PAIR },
  796. { CH_BACK_PAIR, CH_SIDE_PAIR },
  797. { CH_BACK_PAIR, AV_CH_BACK_CENTER },
  798. { AV_CH_BACK_CENTER, CH_BACK_PAIR },
  799. { AV_CH_BACK_CENTER, CH_DIRECT_PAIR },
  800. { AV_CH_BACK_CENTER, CH_SIDE_PAIR },
  801. };
  802. static void swap_channel_layouts_on_filter(AVFilterContext *filter)
  803. {
  804. AVFilterLink *link = NULL;
  805. int i, j, k;
  806. for (i = 0; i < filter->nb_inputs; i++) {
  807. link = filter->inputs[i];
  808. if (link->type == AVMEDIA_TYPE_AUDIO &&
  809. link->out_channel_layouts->nb_channel_layouts == 1)
  810. break;
  811. }
  812. if (i == filter->nb_inputs)
  813. return;
  814. for (i = 0; i < filter->nb_outputs; i++) {
  815. AVFilterLink *outlink = filter->outputs[i];
  816. int best_idx = -1, best_score = INT_MIN, best_count_diff = INT_MAX;
  817. if (outlink->type != AVMEDIA_TYPE_AUDIO ||
  818. outlink->in_channel_layouts->nb_channel_layouts < 2)
  819. continue;
  820. for (j = 0; j < outlink->in_channel_layouts->nb_channel_layouts; j++) {
  821. uint64_t in_chlayout = link->out_channel_layouts->channel_layouts[0];
  822. uint64_t out_chlayout = outlink->in_channel_layouts->channel_layouts[j];
  823. int in_channels = av_get_channel_layout_nb_channels(in_chlayout);
  824. int out_channels = av_get_channel_layout_nb_channels(out_chlayout);
  825. int count_diff = out_channels - in_channels;
  826. int matched_channels, extra_channels;
  827. int score = 100000;
  828. if (FF_LAYOUT2COUNT(in_chlayout) || FF_LAYOUT2COUNT(out_chlayout)) {
  829. /* Compute score in case the input or output layout encodes
  830. a channel count; in this case the score is not altered by
  831. the computation afterwards, as in_chlayout and
  832. out_chlayout have both been set to 0 */
  833. if (FF_LAYOUT2COUNT(in_chlayout))
  834. in_channels = FF_LAYOUT2COUNT(in_chlayout);
  835. if (FF_LAYOUT2COUNT(out_chlayout))
  836. out_channels = FF_LAYOUT2COUNT(out_chlayout);
  837. score -= 10000 + FFABS(out_channels - in_channels) +
  838. (in_channels > out_channels ? 10000 : 0);
  839. in_chlayout = out_chlayout = 0;
  840. /* Let the remaining computation run, even if the score
  841. value is not altered */
  842. }
  843. /* channel substitution */
  844. for (k = 0; k < FF_ARRAY_ELEMS(ch_subst); k++) {
  845. uint64_t cmp0 = ch_subst[k][0];
  846. uint64_t cmp1 = ch_subst[k][1];
  847. if (( in_chlayout & cmp0) && (!(out_chlayout & cmp0)) &&
  848. (out_chlayout & cmp1) && (!( in_chlayout & cmp1))) {
  849. in_chlayout &= ~cmp0;
  850. out_chlayout &= ~cmp1;
  851. /* add score for channel match, minus a deduction for
  852. having to do the substitution */
  853. score += 10 * av_get_channel_layout_nb_channels(cmp1) - 2;
  854. }
  855. }
  856. /* no penalty for LFE channel mismatch */
  857. if ( (in_chlayout & AV_CH_LOW_FREQUENCY) &&
  858. (out_chlayout & AV_CH_LOW_FREQUENCY))
  859. score += 10;
  860. in_chlayout &= ~AV_CH_LOW_FREQUENCY;
  861. out_chlayout &= ~AV_CH_LOW_FREQUENCY;
  862. matched_channels = av_get_channel_layout_nb_channels(in_chlayout &
  863. out_chlayout);
  864. extra_channels = av_get_channel_layout_nb_channels(out_chlayout &
  865. (~in_chlayout));
  866. score += 10 * matched_channels - 5 * extra_channels;
  867. if (score > best_score ||
  868. (count_diff < best_count_diff && score == best_score)) {
  869. best_score = score;
  870. best_idx = j;
  871. best_count_diff = count_diff;
  872. }
  873. }
  874. av_assert0(best_idx >= 0);
  875. FFSWAP(uint64_t, outlink->in_channel_layouts->channel_layouts[0],
  876. outlink->in_channel_layouts->channel_layouts[best_idx]);
  877. }
  878. }
  879. static void swap_channel_layouts(AVFilterGraph *graph)
  880. {
  881. int i;
  882. for (i = 0; i < graph->nb_filters; i++)
  883. swap_channel_layouts_on_filter(graph->filters[i]);
  884. }
  885. static void swap_sample_fmts_on_filter(AVFilterContext *filter)
  886. {
  887. AVFilterLink *link = NULL;
  888. int format, bps;
  889. int i, j;
  890. for (i = 0; i < filter->nb_inputs; i++) {
  891. link = filter->inputs[i];
  892. if (link->type == AVMEDIA_TYPE_AUDIO &&
  893. link->out_formats->nb_formats == 1)
  894. break;
  895. }
  896. if (i == filter->nb_inputs)
  897. return;
  898. format = link->out_formats->formats[0];
  899. bps = av_get_bytes_per_sample(format);
  900. for (i = 0; i < filter->nb_outputs; i++) {
  901. AVFilterLink *outlink = filter->outputs[i];
  902. int best_idx = -1, best_score = INT_MIN;
  903. if (outlink->type != AVMEDIA_TYPE_AUDIO ||
  904. outlink->in_formats->nb_formats < 2)
  905. continue;
  906. for (j = 0; j < outlink->in_formats->nb_formats; j++) {
  907. int out_format = outlink->in_formats->formats[j];
  908. int out_bps = av_get_bytes_per_sample(out_format);
  909. int score;
  910. if (av_get_packed_sample_fmt(out_format) == format ||
  911. av_get_planar_sample_fmt(out_format) == format) {
  912. best_idx = j;
  913. break;
  914. }
  915. /* for s32 and float prefer double to prevent loss of information */
  916. if (bps == 4 && out_bps == 8) {
  917. best_idx = j;
  918. break;
  919. }
  920. /* prefer closest higher or equal bps */
  921. score = -abs(out_bps - bps);
  922. if (out_bps >= bps)
  923. score += INT_MAX/2;
  924. if (score > best_score) {
  925. best_score = score;
  926. best_idx = j;
  927. }
  928. }
  929. av_assert0(best_idx >= 0);
  930. FFSWAP(int, outlink->in_formats->formats[0],
  931. outlink->in_formats->formats[best_idx]);
  932. }
  933. }
  934. static void swap_sample_fmts(AVFilterGraph *graph)
  935. {
  936. int i;
  937. for (i = 0; i < graph->nb_filters; i++)
  938. swap_sample_fmts_on_filter(graph->filters[i]);
  939. }
  940. static int pick_formats(AVFilterGraph *graph)
  941. {
  942. int i, j, ret;
  943. int change;
  944. do{
  945. change = 0;
  946. for (i = 0; i < graph->nb_filters; i++) {
  947. AVFilterContext *filter = graph->filters[i];
  948. if (filter->nb_inputs){
  949. for (j = 0; j < filter->nb_inputs; j++){
  950. if(filter->inputs[j]->in_formats && filter->inputs[j]->in_formats->nb_formats == 1) {
  951. if ((ret = pick_format(filter->inputs[j], NULL)) < 0)
  952. return ret;
  953. change = 1;
  954. }
  955. }
  956. }
  957. if (filter->nb_outputs){
  958. for (j = 0; j < filter->nb_outputs; j++){
  959. if(filter->outputs[j]->in_formats && filter->outputs[j]->in_formats->nb_formats == 1) {
  960. if ((ret = pick_format(filter->outputs[j], NULL)) < 0)
  961. return ret;
  962. change = 1;
  963. }
  964. }
  965. }
  966. if (filter->nb_inputs && filter->nb_outputs && filter->inputs[0]->format>=0) {
  967. for (j = 0; j < filter->nb_outputs; j++) {
  968. if(filter->outputs[j]->format<0) {
  969. if ((ret = pick_format(filter->outputs[j], filter->inputs[0])) < 0)
  970. return ret;
  971. change = 1;
  972. }
  973. }
  974. }
  975. }
  976. }while(change);
  977. for (i = 0; i < graph->nb_filters; i++) {
  978. AVFilterContext *filter = graph->filters[i];
  979. for (j = 0; j < filter->nb_inputs; j++)
  980. if ((ret = pick_format(filter->inputs[j], NULL)) < 0)
  981. return ret;
  982. for (j = 0; j < filter->nb_outputs; j++)
  983. if ((ret = pick_format(filter->outputs[j], NULL)) < 0)
  984. return ret;
  985. }
  986. return 0;
  987. }
  988. /**
  989. * Configure the formats of all the links in the graph.
  990. */
  991. static int graph_config_formats(AVFilterGraph *graph, AVClass *log_ctx)
  992. {
  993. int ret;
  994. /* find supported formats from sub-filters, and merge along links */
  995. while ((ret = query_formats(graph, log_ctx)) == AVERROR(EAGAIN))
  996. av_log(graph, AV_LOG_DEBUG, "query_formats not finished\n");
  997. if (ret < 0)
  998. return ret;
  999. /* Once everything is merged, it's possible that we'll still have
  1000. * multiple valid media format choices. We try to minimize the amount
  1001. * of format conversion inside filters */
  1002. if ((ret = reduce_formats(graph)) < 0)
  1003. return ret;
  1004. /* for audio filters, ensure the best format, sample rate and channel layout
  1005. * is selected */
  1006. swap_sample_fmts(graph);
  1007. swap_samplerates(graph);
  1008. swap_channel_layouts(graph);
  1009. if ((ret = pick_formats(graph)) < 0)
  1010. return ret;
  1011. return 0;
  1012. }
  1013. static int graph_config_pointers(AVFilterGraph *graph,
  1014. AVClass *log_ctx)
  1015. {
  1016. unsigned i, j;
  1017. int sink_links_count = 0, n = 0;
  1018. AVFilterContext *f;
  1019. AVFilterLink **sinks;
  1020. for (i = 0; i < graph->nb_filters; i++) {
  1021. f = graph->filters[i];
  1022. for (j = 0; j < f->nb_inputs; j++) {
  1023. f->inputs[j]->graph = graph;
  1024. f->inputs[j]->age_index = -1;
  1025. }
  1026. for (j = 0; j < f->nb_outputs; j++) {
  1027. f->outputs[j]->graph = graph;
  1028. f->outputs[j]->age_index= -1;
  1029. }
  1030. if (!f->nb_outputs) {
  1031. if (f->nb_inputs > INT_MAX - sink_links_count)
  1032. return AVERROR(EINVAL);
  1033. sink_links_count += f->nb_inputs;
  1034. }
  1035. }
  1036. sinks = av_calloc(sink_links_count, sizeof(*sinks));
  1037. if (!sinks)
  1038. return AVERROR(ENOMEM);
  1039. for (i = 0; i < graph->nb_filters; i++) {
  1040. f = graph->filters[i];
  1041. if (!f->nb_outputs) {
  1042. for (j = 0; j < f->nb_inputs; j++) {
  1043. sinks[n] = f->inputs[j];
  1044. f->inputs[j]->age_index = n++;
  1045. }
  1046. }
  1047. }
  1048. av_assert0(n == sink_links_count);
  1049. graph->sink_links = sinks;
  1050. graph->sink_links_count = sink_links_count;
  1051. return 0;
  1052. }
  1053. static int graph_insert_fifos(AVFilterGraph *graph, AVClass *log_ctx)
  1054. {
  1055. AVFilterContext *f;
  1056. int i, j, ret;
  1057. int fifo_count = 0;
  1058. for (i = 0; i < graph->nb_filters; i++) {
  1059. f = graph->filters[i];
  1060. for (j = 0; j < f->nb_inputs; j++) {
  1061. AVFilterLink *link = f->inputs[j];
  1062. AVFilterContext *fifo_ctx;
  1063. AVFilter *fifo;
  1064. char name[32];
  1065. if (!link->dstpad->needs_fifo)
  1066. continue;
  1067. fifo = f->inputs[j]->type == AVMEDIA_TYPE_VIDEO ?
  1068. avfilter_get_by_name("fifo") :
  1069. avfilter_get_by_name("afifo");
  1070. snprintf(name, sizeof(name), "auto-inserted fifo %d", fifo_count++);
  1071. ret = avfilter_graph_create_filter(&fifo_ctx, fifo, name, NULL,
  1072. NULL, graph);
  1073. if (ret < 0)
  1074. return ret;
  1075. ret = avfilter_insert_filter(link, fifo_ctx, 0, 0);
  1076. if (ret < 0)
  1077. return ret;
  1078. }
  1079. }
  1080. return 0;
  1081. }
  1082. int avfilter_graph_config(AVFilterGraph *graphctx, void *log_ctx)
  1083. {
  1084. int ret;
  1085. if ((ret = graph_check_validity(graphctx, log_ctx)))
  1086. return ret;
  1087. if ((ret = graph_insert_fifos(graphctx, log_ctx)) < 0)
  1088. return ret;
  1089. if ((ret = graph_config_formats(graphctx, log_ctx)))
  1090. return ret;
  1091. if ((ret = graph_config_links(graphctx, log_ctx)))
  1092. return ret;
  1093. if ((ret = graph_config_pointers(graphctx, log_ctx)))
  1094. return ret;
  1095. return 0;
  1096. }
  1097. int avfilter_graph_send_command(AVFilterGraph *graph, const char *target, const char *cmd, const char *arg, char *res, int res_len, int flags)
  1098. {
  1099. int i, r = AVERROR(ENOSYS);
  1100. if (!graph)
  1101. return r;
  1102. if ((flags & AVFILTER_CMD_FLAG_ONE) && !(flags & AVFILTER_CMD_FLAG_FAST)) {
  1103. r = avfilter_graph_send_command(graph, target, cmd, arg, res, res_len, flags | AVFILTER_CMD_FLAG_FAST);
  1104. if (r != AVERROR(ENOSYS))
  1105. return r;
  1106. }
  1107. if (res_len && res)
  1108. res[0] = 0;
  1109. for (i = 0; i < graph->nb_filters; i++) {
  1110. AVFilterContext *filter = graph->filters[i];
  1111. if (!strcmp(target, "all") || (filter->name && !strcmp(target, filter->name)) || !strcmp(target, filter->filter->name)) {
  1112. r = avfilter_process_command(filter, cmd, arg, res, res_len, flags);
  1113. if (r != AVERROR(ENOSYS)) {
  1114. if ((flags & AVFILTER_CMD_FLAG_ONE) || r < 0)
  1115. return r;
  1116. }
  1117. }
  1118. }
  1119. return r;
  1120. }
  1121. int avfilter_graph_queue_command(AVFilterGraph *graph, const char *target, const char *command, const char *arg, int flags, double ts)
  1122. {
  1123. int i;
  1124. if(!graph)
  1125. return 0;
  1126. for (i = 0; i < graph->nb_filters; i++) {
  1127. AVFilterContext *filter = graph->filters[i];
  1128. if(filter && (!strcmp(target, "all") || !strcmp(target, filter->name) || !strcmp(target, filter->filter->name))){
  1129. AVFilterCommand **queue = &filter->command_queue, *next;
  1130. while (*queue && (*queue)->time <= ts)
  1131. queue = &(*queue)->next;
  1132. next = *queue;
  1133. *queue = av_mallocz(sizeof(AVFilterCommand));
  1134. (*queue)->command = av_strdup(command);
  1135. (*queue)->arg = av_strdup(arg);
  1136. (*queue)->time = ts;
  1137. (*queue)->flags = flags;
  1138. (*queue)->next = next;
  1139. if(flags & AVFILTER_CMD_FLAG_ONE)
  1140. return 0;
  1141. }
  1142. }
  1143. return 0;
  1144. }
  1145. static void heap_bubble_up(AVFilterGraph *graph,
  1146. AVFilterLink *link, int index)
  1147. {
  1148. AVFilterLink **links = graph->sink_links;
  1149. av_assert0(index >= 0);
  1150. while (index) {
  1151. int parent = (index - 1) >> 1;
  1152. if (links[parent]->current_pts_us >= link->current_pts_us)
  1153. break;
  1154. links[index] = links[parent];
  1155. links[index]->age_index = index;
  1156. index = parent;
  1157. }
  1158. links[index] = link;
  1159. link->age_index = index;
  1160. }
  1161. static void heap_bubble_down(AVFilterGraph *graph,
  1162. AVFilterLink *link, int index)
  1163. {
  1164. AVFilterLink **links = graph->sink_links;
  1165. av_assert0(index >= 0);
  1166. while (1) {
  1167. int child = 2 * index + 1;
  1168. if (child >= graph->sink_links_count)
  1169. break;
  1170. if (child + 1 < graph->sink_links_count &&
  1171. links[child + 1]->current_pts_us < links[child]->current_pts_us)
  1172. child++;
  1173. if (link->current_pts_us < links[child]->current_pts_us)
  1174. break;
  1175. links[index] = links[child];
  1176. links[index]->age_index = index;
  1177. index = child;
  1178. }
  1179. links[index] = link;
  1180. link->age_index = index;
  1181. }
  1182. void ff_avfilter_graph_update_heap(AVFilterGraph *graph, AVFilterLink *link)
  1183. {
  1184. heap_bubble_up (graph, link, link->age_index);
  1185. heap_bubble_down(graph, link, link->age_index);
  1186. }
  1187. int avfilter_graph_request_oldest(AVFilterGraph *graph)
  1188. {
  1189. AVFilterLink *oldest = graph->sink_links[0];
  1190. int r;
  1191. while (graph->sink_links_count) {
  1192. oldest = graph->sink_links[0];
  1193. r = ff_request_frame(oldest);
  1194. if (r != AVERROR_EOF)
  1195. break;
  1196. av_log(oldest->dst, AV_LOG_DEBUG, "EOF on sink link %s:%s.\n",
  1197. oldest->dst ? oldest->dst->name : "unknown",
  1198. oldest->dstpad ? oldest->dstpad->name : "unknown");
  1199. /* EOF: remove the link from the heap */
  1200. if (oldest->age_index < --graph->sink_links_count)
  1201. heap_bubble_down(graph, graph->sink_links[graph->sink_links_count],
  1202. oldest->age_index);
  1203. oldest->age_index = -1;
  1204. }
  1205. if (!graph->sink_links_count)
  1206. return AVERROR_EOF;
  1207. av_assert1(oldest->age_index >= 0);
  1208. while (oldest->frame_wanted_out) {
  1209. r = ff_filter_graph_run_once(graph);
  1210. if (r < 0)
  1211. return r;
  1212. }
  1213. return 0;
  1214. }
  1215. static AVFilterLink *graph_run_once_find_filter(AVFilterGraph *graph)
  1216. {
  1217. unsigned i, j;
  1218. AVFilterContext *f;
  1219. /* TODO: replace scanning the graph with a priority list */
  1220. for (i = 0; i < graph->nb_filters; i++) {
  1221. f = graph->filters[i];
  1222. for (j = 0; j < f->nb_outputs; j++)
  1223. if (f->outputs[j]->frame_wanted_in)
  1224. return f->outputs[j];
  1225. }
  1226. for (i = 0; i < graph->nb_filters; i++) {
  1227. f = graph->filters[i];
  1228. for (j = 0; j < f->nb_outputs; j++)
  1229. if (f->outputs[j]->frame_wanted_out)
  1230. return f->outputs[j];
  1231. }
  1232. return NULL;
  1233. }
  1234. int ff_filter_graph_run_once(AVFilterGraph *graph)
  1235. {
  1236. AVFilterLink *link;
  1237. int ret;
  1238. link = graph_run_once_find_filter(graph);
  1239. if (!link) {
  1240. av_log(NULL, AV_LOG_WARNING, "Useless run of a filter graph\n");
  1241. return AVERROR(EAGAIN);
  1242. }
  1243. ret = ff_request_frame_to_filter(link);
  1244. if (ret == AVERROR_EOF)
  1245. /* local EOF will be forwarded through request_frame() /
  1246. set_status() until it reaches the sink */
  1247. ret = 0;
  1248. return ret < 0 ? ret : 1;
  1249. }