avfiltergraph.c 47 KB

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