avfiltergraph.c 46 KB

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