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.

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