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 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. if (a)
  363. av_freep(&a->formats);
  364. if (b)
  365. av_freep(&b->formats);
  366. av_freep(&a);
  367. av_freep(&b);
  368. return 0;
  369. }
  370. }
  371. /**
  372. * Perform one round of query_formats() and merging formats lists on the
  373. * filter graph.
  374. * @return >=0 if all links formats lists could be queried and merged;
  375. * AVERROR(EAGAIN) some progress was made in the queries or merging
  376. * and a later call may succeed;
  377. * AVERROR(EIO) (may be changed) plus a log message if no progress
  378. * was made and the negotiation is stuck;
  379. * a negative error code if some other error happened
  380. */
  381. static int query_formats(AVFilterGraph *graph, AVClass *log_ctx)
  382. {
  383. int i, j, ret;
  384. int scaler_count = 0, resampler_count = 0;
  385. int count_queried = 0; /* successful calls to query_formats() */
  386. int count_merged = 0; /* successful merge of formats lists */
  387. int count_already_merged = 0; /* lists already merged */
  388. int count_delayed = 0; /* lists that need to be merged later */
  389. for (i = 0; i < graph->nb_filters; i++) {
  390. AVFilterContext *f = graph->filters[i];
  391. if (formats_declared(f))
  392. continue;
  393. if (f->filter->query_formats)
  394. ret = filter_query_formats(f);
  395. else
  396. ret = ff_default_query_formats(f);
  397. if (ret < 0 && ret != AVERROR(EAGAIN))
  398. return ret;
  399. /* note: EAGAIN could indicate a partial success, not counted yet */
  400. count_queried += ret >= 0;
  401. }
  402. /* go through and merge as many format lists as possible */
  403. for (i = 0; i < graph->nb_filters; i++) {
  404. AVFilterContext *filter = graph->filters[i];
  405. for (j = 0; j < filter->nb_inputs; j++) {
  406. AVFilterLink *link = filter->inputs[j];
  407. int convert_needed = 0;
  408. if (!link)
  409. continue;
  410. if (link->in_formats != link->out_formats
  411. && link->in_formats && link->out_formats)
  412. if (!can_merge_formats(link->in_formats, link->out_formats,
  413. link->type, 0))
  414. convert_needed = 1;
  415. if (link->type == AVMEDIA_TYPE_AUDIO) {
  416. if (link->in_samplerates != link->out_samplerates
  417. && link->in_samplerates && link->out_samplerates)
  418. if (!can_merge_formats(link->in_samplerates,
  419. link->out_samplerates,
  420. 0, 1))
  421. convert_needed = 1;
  422. }
  423. #define MERGE_DISPATCH(field, statement) \
  424. if (!(link->in_ ## field && link->out_ ## field)) { \
  425. count_delayed++; \
  426. } else if (link->in_ ## field == link->out_ ## field) { \
  427. count_already_merged++; \
  428. } else if (!convert_needed) { \
  429. count_merged++; \
  430. statement \
  431. }
  432. if (link->type == AVMEDIA_TYPE_AUDIO) {
  433. MERGE_DISPATCH(channel_layouts,
  434. if (!ff_merge_channel_layouts(link->in_channel_layouts,
  435. link->out_channel_layouts))
  436. convert_needed = 1;
  437. )
  438. MERGE_DISPATCH(samplerates,
  439. if (!ff_merge_samplerates(link->in_samplerates,
  440. link->out_samplerates))
  441. convert_needed = 1;
  442. )
  443. }
  444. MERGE_DISPATCH(formats,
  445. if (!ff_merge_formats(link->in_formats, link->out_formats,
  446. link->type))
  447. convert_needed = 1;
  448. )
  449. #undef MERGE_DISPATCH
  450. if (convert_needed) {
  451. AVFilterContext *convert;
  452. const AVFilter *filter;
  453. AVFilterLink *inlink, *outlink;
  454. char inst_name[30];
  455. if (graph->disable_auto_convert) {
  456. av_log(log_ctx, AV_LOG_ERROR,
  457. "The filters '%s' and '%s' do not have a common format "
  458. "and automatic conversion is disabled.\n",
  459. link->src->name, link->dst->name);
  460. return AVERROR(EINVAL);
  461. }
  462. /* couldn't merge format lists. auto-insert conversion filter */
  463. switch (link->type) {
  464. case AVMEDIA_TYPE_VIDEO:
  465. if (!(filter = avfilter_get_by_name("scale"))) {
  466. av_log(log_ctx, AV_LOG_ERROR, "'scale' filter "
  467. "not present, cannot convert pixel formats.\n");
  468. return AVERROR(EINVAL);
  469. }
  470. snprintf(inst_name, sizeof(inst_name), "auto_scaler_%d",
  471. scaler_count++);
  472. if ((ret = avfilter_graph_create_filter(&convert, filter,
  473. inst_name, graph->scale_sws_opts, NULL,
  474. graph)) < 0)
  475. return ret;
  476. break;
  477. case AVMEDIA_TYPE_AUDIO:
  478. if (!(filter = avfilter_get_by_name("aresample"))) {
  479. av_log(log_ctx, AV_LOG_ERROR, "'aresample' filter "
  480. "not present, cannot convert audio formats.\n");
  481. return AVERROR(EINVAL);
  482. }
  483. snprintf(inst_name, sizeof(inst_name), "auto_resampler_%d",
  484. resampler_count++);
  485. if ((ret = avfilter_graph_create_filter(&convert, filter,
  486. inst_name, graph->aresample_swr_opts,
  487. NULL, graph)) < 0)
  488. return ret;
  489. break;
  490. default:
  491. return AVERROR(EINVAL);
  492. }
  493. if ((ret = avfilter_insert_filter(link, convert, 0, 0)) < 0)
  494. return ret;
  495. if ((ret = filter_query_formats(convert)) < 0)
  496. return ret;
  497. inlink = convert->inputs[0];
  498. outlink = convert->outputs[0];
  499. av_assert0( inlink-> in_formats->refcount > 0);
  500. av_assert0( inlink->out_formats->refcount > 0);
  501. av_assert0(outlink-> in_formats->refcount > 0);
  502. av_assert0(outlink->out_formats->refcount > 0);
  503. if (outlink->type == AVMEDIA_TYPE_AUDIO) {
  504. av_assert0( inlink-> in_samplerates->refcount > 0);
  505. av_assert0( inlink->out_samplerates->refcount > 0);
  506. av_assert0(outlink-> in_samplerates->refcount > 0);
  507. av_assert0(outlink->out_samplerates->refcount > 0);
  508. av_assert0( inlink-> in_channel_layouts->refcount > 0);
  509. av_assert0( inlink->out_channel_layouts->refcount > 0);
  510. av_assert0(outlink-> in_channel_layouts->refcount > 0);
  511. av_assert0(outlink->out_channel_layouts->refcount > 0);
  512. }
  513. if (!ff_merge_formats( inlink->in_formats, inlink->out_formats, inlink->type) ||
  514. !ff_merge_formats(outlink->in_formats, outlink->out_formats, outlink->type))
  515. ret = AVERROR(ENOSYS);
  516. if (inlink->type == AVMEDIA_TYPE_AUDIO &&
  517. (!ff_merge_samplerates(inlink->in_samplerates,
  518. inlink->out_samplerates) ||
  519. !ff_merge_channel_layouts(inlink->in_channel_layouts,
  520. inlink->out_channel_layouts)))
  521. ret = AVERROR(ENOSYS);
  522. if (outlink->type == AVMEDIA_TYPE_AUDIO &&
  523. (!ff_merge_samplerates(outlink->in_samplerates,
  524. outlink->out_samplerates) ||
  525. !ff_merge_channel_layouts(outlink->in_channel_layouts,
  526. outlink->out_channel_layouts)))
  527. ret = AVERROR(ENOSYS);
  528. if (ret < 0) {
  529. av_log(log_ctx, AV_LOG_ERROR,
  530. "Impossible to convert between the formats supported by the filter "
  531. "'%s' and the filter '%s'\n", link->src->name, link->dst->name);
  532. return ret;
  533. }
  534. }
  535. }
  536. }
  537. av_log(graph, AV_LOG_DEBUG, "query_formats: "
  538. "%d queried, %d merged, %d already done, %d delayed\n",
  539. count_queried, count_merged, count_already_merged, count_delayed);
  540. if (count_delayed) {
  541. AVBPrint bp;
  542. /* if count_queried > 0, one filter at least did set its formats,
  543. that will give additional information to its neighbour;
  544. if count_merged > 0, one pair of formats lists at least was merged,
  545. that will give additional information to all connected filters;
  546. in both cases, progress was made and a new round must be done */
  547. if (count_queried || count_merged)
  548. return AVERROR(EAGAIN);
  549. av_bprint_init(&bp, 0, AV_BPRINT_SIZE_AUTOMATIC);
  550. for (i = 0; i < graph->nb_filters; i++)
  551. if (!formats_declared(graph->filters[i]))
  552. av_bprintf(&bp, "%s%s", bp.len ? ", " : "",
  553. graph->filters[i]->name);
  554. av_log(graph, AV_LOG_ERROR,
  555. "The following filters could not choose their formats: %s\n"
  556. "Consider inserting the (a)format filter near their input or "
  557. "output.\n", bp.str);
  558. return AVERROR(EIO);
  559. }
  560. return 0;
  561. }
  562. static int get_fmt_score(enum AVSampleFormat dst_fmt, enum AVSampleFormat src_fmt)
  563. {
  564. int score = 0;
  565. if (av_sample_fmt_is_planar(dst_fmt) != av_sample_fmt_is_planar(src_fmt))
  566. score ++;
  567. if (av_get_bytes_per_sample(dst_fmt) < av_get_bytes_per_sample(src_fmt)) {
  568. score += 100 * (av_get_bytes_per_sample(src_fmt) - av_get_bytes_per_sample(dst_fmt));
  569. }else
  570. score += 10 * (av_get_bytes_per_sample(dst_fmt) - av_get_bytes_per_sample(src_fmt));
  571. if (av_get_packed_sample_fmt(dst_fmt) == AV_SAMPLE_FMT_S32 &&
  572. av_get_packed_sample_fmt(src_fmt) == AV_SAMPLE_FMT_FLT)
  573. score += 20;
  574. if (av_get_packed_sample_fmt(dst_fmt) == AV_SAMPLE_FMT_FLT &&
  575. av_get_packed_sample_fmt(src_fmt) == AV_SAMPLE_FMT_S32)
  576. score += 2;
  577. return score;
  578. }
  579. static enum AVSampleFormat find_best_sample_fmt_of_2(enum AVSampleFormat dst_fmt1, enum AVSampleFormat dst_fmt2,
  580. enum AVSampleFormat src_fmt)
  581. {
  582. int score1, score2;
  583. score1 = get_fmt_score(dst_fmt1, src_fmt);
  584. score2 = get_fmt_score(dst_fmt2, src_fmt);
  585. return score1 < score2 ? dst_fmt1 : dst_fmt2;
  586. }
  587. static int pick_format(AVFilterLink *link, AVFilterLink *ref)
  588. {
  589. if (!link || !link->in_formats)
  590. return 0;
  591. if (link->type == AVMEDIA_TYPE_VIDEO) {
  592. if(ref && ref->type == AVMEDIA_TYPE_VIDEO){
  593. //FIXME: This should check for AV_PIX_FMT_FLAG_ALPHA after PAL8 pixel format without alpha is implemented
  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. }