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.

1455 lines
51KB

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