avfiltergraph.c 51 KB

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