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.

1434 lines
50KB

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