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.

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