avfiltergraph.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341
  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. filter_query_formats(convert);
  486. inlink = convert->inputs[0];
  487. outlink = convert->outputs[0];
  488. av_assert0( inlink-> in_formats->refcount > 0);
  489. av_assert0( inlink->out_formats->refcount > 0);
  490. av_assert0(outlink-> in_formats->refcount > 0);
  491. av_assert0(outlink->out_formats->refcount > 0);
  492. if (outlink->type == AVMEDIA_TYPE_AUDIO) {
  493. av_assert0( inlink-> in_samplerates->refcount > 0);
  494. av_assert0( inlink->out_samplerates->refcount > 0);
  495. av_assert0(outlink-> in_samplerates->refcount > 0);
  496. av_assert0(outlink->out_samplerates->refcount > 0);
  497. av_assert0( inlink-> in_channel_layouts->refcount > 0);
  498. av_assert0( inlink->out_channel_layouts->refcount > 0);
  499. av_assert0(outlink-> in_channel_layouts->refcount > 0);
  500. av_assert0(outlink->out_channel_layouts->refcount > 0);
  501. }
  502. if (!ff_merge_formats( inlink->in_formats, inlink->out_formats, inlink->type) ||
  503. !ff_merge_formats(outlink->in_formats, outlink->out_formats, outlink->type))
  504. ret = AVERROR(ENOSYS);
  505. if (inlink->type == AVMEDIA_TYPE_AUDIO &&
  506. (!ff_merge_samplerates(inlink->in_samplerates,
  507. inlink->out_samplerates) ||
  508. !ff_merge_channel_layouts(inlink->in_channel_layouts,
  509. inlink->out_channel_layouts)))
  510. ret = AVERROR(ENOSYS);
  511. if (outlink->type == AVMEDIA_TYPE_AUDIO &&
  512. (!ff_merge_samplerates(outlink->in_samplerates,
  513. outlink->out_samplerates) ||
  514. !ff_merge_channel_layouts(outlink->in_channel_layouts,
  515. outlink->out_channel_layouts)))
  516. ret = AVERROR(ENOSYS);
  517. if (ret < 0) {
  518. av_log(log_ctx, AV_LOG_ERROR,
  519. "Impossible to convert between the formats supported by the filter "
  520. "'%s' and the filter '%s'\n", link->src->name, link->dst->name);
  521. return ret;
  522. }
  523. }
  524. }
  525. }
  526. av_log(graph, AV_LOG_DEBUG, "query_formats: "
  527. "%d queried, %d merged, %d already done, %d delayed\n",
  528. count_queried, count_merged, count_already_merged, count_delayed);
  529. if (count_delayed) {
  530. AVBPrint bp;
  531. /* if count_queried > 0, one filter at least did set its formats,
  532. that will give additional information to its neighbour;
  533. if count_merged > 0, one pair of formats lists at least was merged,
  534. that will give additional information to all connected filters;
  535. in both cases, progress was made and a new round must be done */
  536. if (count_queried || count_merged)
  537. return AVERROR(EAGAIN);
  538. av_bprint_init(&bp, 0, AV_BPRINT_SIZE_AUTOMATIC);
  539. for (i = 0; i < graph->nb_filters; i++)
  540. if (!formats_declared(graph->filters[i]))
  541. av_bprintf(&bp, "%s%s", bp.len ? ", " : "",
  542. graph->filters[i]->name);
  543. av_log(graph, AV_LOG_ERROR,
  544. "The following filters could not choose their formats: %s\n"
  545. "Consider inserting the (a)format filter near their input or "
  546. "output.\n", bp.str);
  547. return AVERROR(EIO);
  548. }
  549. return 0;
  550. }
  551. static int pick_format(AVFilterLink *link, AVFilterLink *ref)
  552. {
  553. if (!link || !link->in_formats)
  554. return 0;
  555. if (link->type == AVMEDIA_TYPE_VIDEO) {
  556. if(ref && ref->type == AVMEDIA_TYPE_VIDEO){
  557. int has_alpha= av_pix_fmt_desc_get(ref->format)->nb_components % 2 == 0;
  558. enum AVPixelFormat best= AV_PIX_FMT_NONE;
  559. int i;
  560. for (i=0; i<link->in_formats->nb_formats; i++) {
  561. enum AVPixelFormat p = link->in_formats->formats[i];
  562. best= av_find_best_pix_fmt_of_2(best, p, ref->format, has_alpha, NULL);
  563. }
  564. av_log(link->src,AV_LOG_DEBUG, "picking %s out of %d ref:%s alpha:%d\n",
  565. av_get_pix_fmt_name(best), link->in_formats->nb_formats,
  566. av_get_pix_fmt_name(ref->format), has_alpha);
  567. link->in_formats->formats[0] = best;
  568. }
  569. }
  570. link->in_formats->nb_formats = 1;
  571. link->format = link->in_formats->formats[0];
  572. if (link->type == AVMEDIA_TYPE_AUDIO) {
  573. if (!link->in_samplerates->nb_formats) {
  574. av_log(link->src, AV_LOG_ERROR, "Cannot select sample rate for"
  575. " the link between filters %s and %s.\n", link->src->name,
  576. link->dst->name);
  577. return AVERROR(EINVAL);
  578. }
  579. link->in_samplerates->nb_formats = 1;
  580. link->sample_rate = link->in_samplerates->formats[0];
  581. if (link->in_channel_layouts->all_layouts) {
  582. av_log(link->src, AV_LOG_ERROR, "Cannot select channel layout for"
  583. " the link between filters %s and %s.\n", link->src->name,
  584. link->dst->name);
  585. if (!link->in_channel_layouts->all_counts)
  586. av_log(link->src, AV_LOG_ERROR, "Unknown channel layouts not "
  587. "supported, try specifying a channel layout using "
  588. "'aformat=channel_layouts=something'.\n");
  589. return AVERROR(EINVAL);
  590. }
  591. link->in_channel_layouts->nb_channel_layouts = 1;
  592. link->channel_layout = link->in_channel_layouts->channel_layouts[0];
  593. if ((link->channels = FF_LAYOUT2COUNT(link->channel_layout)))
  594. link->channel_layout = 0;
  595. else
  596. link->channels = av_get_channel_layout_nb_channels(link->channel_layout);
  597. }
  598. ff_formats_unref(&link->in_formats);
  599. ff_formats_unref(&link->out_formats);
  600. ff_formats_unref(&link->in_samplerates);
  601. ff_formats_unref(&link->out_samplerates);
  602. ff_channel_layouts_unref(&link->in_channel_layouts);
  603. ff_channel_layouts_unref(&link->out_channel_layouts);
  604. return 0;
  605. }
  606. #define REDUCE_FORMATS(fmt_type, list_type, list, var, nb, add_format) \
  607. do { \
  608. for (i = 0; i < filter->nb_inputs; i++) { \
  609. AVFilterLink *link = filter->inputs[i]; \
  610. fmt_type fmt; \
  611. \
  612. if (!link->out_ ## list || link->out_ ## list->nb != 1) \
  613. continue; \
  614. fmt = link->out_ ## list->var[0]; \
  615. \
  616. for (j = 0; j < filter->nb_outputs; j++) { \
  617. AVFilterLink *out_link = filter->outputs[j]; \
  618. list_type *fmts; \
  619. \
  620. if (link->type != out_link->type || \
  621. out_link->in_ ## list->nb == 1) \
  622. continue; \
  623. fmts = out_link->in_ ## list; \
  624. \
  625. if (!out_link->in_ ## list->nb) { \
  626. add_format(&out_link->in_ ##list, fmt); \
  627. ret = 1; \
  628. break; \
  629. } \
  630. \
  631. for (k = 0; k < out_link->in_ ## list->nb; k++) \
  632. if (fmts->var[k] == fmt) { \
  633. fmts->var[0] = fmt; \
  634. fmts->nb = 1; \
  635. ret = 1; \
  636. break; \
  637. } \
  638. } \
  639. } \
  640. } while (0)
  641. static int reduce_formats_on_filter(AVFilterContext *filter)
  642. {
  643. int i, j, k, ret = 0;
  644. REDUCE_FORMATS(int, AVFilterFormats, formats, formats,
  645. nb_formats, ff_add_format);
  646. REDUCE_FORMATS(int, AVFilterFormats, samplerates, formats,
  647. nb_formats, ff_add_format);
  648. /* reduce channel layouts */
  649. for (i = 0; i < filter->nb_inputs; i++) {
  650. AVFilterLink *inlink = filter->inputs[i];
  651. uint64_t fmt;
  652. if (!inlink->out_channel_layouts ||
  653. inlink->out_channel_layouts->nb_channel_layouts != 1)
  654. continue;
  655. fmt = inlink->out_channel_layouts->channel_layouts[0];
  656. for (j = 0; j < filter->nb_outputs; j++) {
  657. AVFilterLink *outlink = filter->outputs[j];
  658. AVFilterChannelLayouts *fmts;
  659. fmts = outlink->in_channel_layouts;
  660. if (inlink->type != outlink->type || fmts->nb_channel_layouts == 1)
  661. continue;
  662. if (fmts->all_layouts &&
  663. (!FF_LAYOUT2COUNT(fmt) || fmts->all_counts)) {
  664. /* Turn the infinite list into a singleton */
  665. fmts->all_layouts = fmts->all_counts = 0;
  666. ff_add_channel_layout(&outlink->in_channel_layouts, fmt);
  667. break;
  668. }
  669. for (k = 0; k < outlink->in_channel_layouts->nb_channel_layouts; k++) {
  670. if (fmts->channel_layouts[k] == fmt) {
  671. fmts->channel_layouts[0] = fmt;
  672. fmts->nb_channel_layouts = 1;
  673. ret = 1;
  674. break;
  675. }
  676. }
  677. }
  678. }
  679. return ret;
  680. }
  681. static void reduce_formats(AVFilterGraph *graph)
  682. {
  683. int i, reduced;
  684. do {
  685. reduced = 0;
  686. for (i = 0; i < graph->nb_filters; i++)
  687. reduced |= reduce_formats_on_filter(graph->filters[i]);
  688. } while (reduced);
  689. }
  690. static void swap_samplerates_on_filter(AVFilterContext *filter)
  691. {
  692. AVFilterLink *link = NULL;
  693. int sample_rate;
  694. int i, j;
  695. for (i = 0; i < filter->nb_inputs; i++) {
  696. link = filter->inputs[i];
  697. if (link->type == AVMEDIA_TYPE_AUDIO &&
  698. link->out_samplerates->nb_formats== 1)
  699. break;
  700. }
  701. if (i == filter->nb_inputs)
  702. return;
  703. sample_rate = link->out_samplerates->formats[0];
  704. for (i = 0; i < filter->nb_outputs; i++) {
  705. AVFilterLink *outlink = filter->outputs[i];
  706. int best_idx, best_diff = INT_MAX;
  707. if (outlink->type != AVMEDIA_TYPE_AUDIO ||
  708. outlink->in_samplerates->nb_formats < 2)
  709. continue;
  710. for (j = 0; j < outlink->in_samplerates->nb_formats; j++) {
  711. int diff = abs(sample_rate - outlink->in_samplerates->formats[j]);
  712. av_assert0(diff < INT_MAX); // This would lead to the use of uninitialized best_diff but is only possible with invalid sample rates
  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. while (index) {
  1107. int parent = (index - 1) >> 1;
  1108. if (links[parent]->current_pts >= link->current_pts)
  1109. break;
  1110. links[index] = links[parent];
  1111. links[index]->age_index = index;
  1112. index = parent;
  1113. }
  1114. links[index] = link;
  1115. link->age_index = index;
  1116. }
  1117. static void heap_bubble_down(AVFilterGraph *graph,
  1118. AVFilterLink *link, int index)
  1119. {
  1120. AVFilterLink **links = graph->sink_links;
  1121. while (1) {
  1122. int child = 2 * index + 1;
  1123. if (child >= graph->sink_links_count)
  1124. break;
  1125. if (child + 1 < graph->sink_links_count &&
  1126. links[child + 1]->current_pts < links[child]->current_pts)
  1127. child++;
  1128. if (link->current_pts < links[child]->current_pts)
  1129. break;
  1130. links[index] = links[child];
  1131. links[index]->age_index = index;
  1132. index = child;
  1133. }
  1134. links[index] = link;
  1135. link->age_index = index;
  1136. }
  1137. void ff_avfilter_graph_update_heap(AVFilterGraph *graph, AVFilterLink *link)
  1138. {
  1139. heap_bubble_up (graph, link, link->age_index);
  1140. heap_bubble_down(graph, link, link->age_index);
  1141. }
  1142. int avfilter_graph_request_oldest(AVFilterGraph *graph)
  1143. {
  1144. while (graph->sink_links_count) {
  1145. AVFilterLink *oldest = graph->sink_links[0];
  1146. int r = ff_request_frame(oldest);
  1147. if (r != AVERROR_EOF)
  1148. return r;
  1149. av_log(oldest->dst, AV_LOG_DEBUG, "EOF on sink link %s:%s.\n",
  1150. oldest->dst ? oldest->dst->name : "unknown",
  1151. oldest->dstpad ? oldest->dstpad->name : "unknown");
  1152. /* EOF: remove the link from the heap */
  1153. if (oldest->age_index < --graph->sink_links_count)
  1154. heap_bubble_down(graph, graph->sink_links[graph->sink_links_count],
  1155. oldest->age_index);
  1156. oldest->age_index = -1;
  1157. }
  1158. return AVERROR_EOF;
  1159. }