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.

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