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.

1386 lines
49KB

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