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.

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