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.

1381 lines
48KB

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