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