avfiltergraph.c 50 KB

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