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.

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