avfiltergraph.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993
  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 <ctype.h>
  23. #include <string.h>
  24. #include "libavutil/audioconvert.h"
  25. #include "libavutil/avassert.h"
  26. #include "libavutil/pixdesc.h"
  27. #include "libavcodec/avcodec.h" // avcodec_find_best_pix_fmt2()
  28. #include "avfilter.h"
  29. #include "avfiltergraph.h"
  30. #include "formats.h"
  31. #include "internal.h"
  32. #include "libavutil/audioconvert.h"
  33. #include "libavutil/avassert.h"
  34. #include "libavutil/log.h"
  35. static const AVClass filtergraph_class = {
  36. .class_name = "AVFilterGraph",
  37. .item_name = av_default_item_name,
  38. .version = LIBAVUTIL_VERSION_INT,
  39. .category = AV_CLASS_CATEGORY_FILTER,
  40. };
  41. AVFilterGraph *avfilter_graph_alloc(void)
  42. {
  43. AVFilterGraph *ret = av_mallocz(sizeof(AVFilterGraph));
  44. if (!ret)
  45. return NULL;
  46. ret->av_class = &filtergraph_class;
  47. return ret;
  48. }
  49. void avfilter_graph_free(AVFilterGraph **graph)
  50. {
  51. if (!*graph)
  52. return;
  53. for (; (*graph)->filter_count > 0; (*graph)->filter_count--)
  54. avfilter_free((*graph)->filters[(*graph)->filter_count - 1]);
  55. av_freep(&(*graph)->sink_links);
  56. av_freep(&(*graph)->scale_sws_opts);
  57. av_freep(&(*graph)->filters);
  58. av_freep(graph);
  59. }
  60. int avfilter_graph_add_filter(AVFilterGraph *graph, AVFilterContext *filter)
  61. {
  62. AVFilterContext **filters = av_realloc(graph->filters,
  63. sizeof(AVFilterContext*) * (graph->filter_count+1));
  64. if (!filters)
  65. return AVERROR(ENOMEM);
  66. graph->filters = filters;
  67. graph->filters[graph->filter_count++] = filter;
  68. return 0;
  69. }
  70. int avfilter_graph_create_filter(AVFilterContext **filt_ctx, AVFilter *filt,
  71. const char *name, const char *args, void *opaque,
  72. AVFilterGraph *graph_ctx)
  73. {
  74. int ret;
  75. if ((ret = avfilter_open(filt_ctx, filt, name)) < 0)
  76. goto fail;
  77. if ((ret = avfilter_init_filter(*filt_ctx, args, opaque)) < 0)
  78. goto fail;
  79. if ((ret = avfilter_graph_add_filter(graph_ctx, *filt_ctx)) < 0)
  80. goto fail;
  81. return 0;
  82. fail:
  83. if (*filt_ctx)
  84. avfilter_free(*filt_ctx);
  85. *filt_ctx = NULL;
  86. return ret;
  87. }
  88. void avfilter_graph_set_auto_convert(AVFilterGraph *graph, unsigned flags)
  89. {
  90. graph->disable_auto_convert = flags;
  91. }
  92. /**
  93. * Check for the validity of graph.
  94. *
  95. * A graph is considered valid if all its input and output pads are
  96. * connected.
  97. *
  98. * @return 0 in case of success, a negative value otherwise
  99. */
  100. static int graph_check_validity(AVFilterGraph *graph, AVClass *log_ctx)
  101. {
  102. AVFilterContext *filt;
  103. int i, j;
  104. for (i = 0; i < graph->filter_count; i++) {
  105. filt = graph->filters[i];
  106. for (j = 0; j < filt->nb_inputs; j++) {
  107. if (!filt->inputs[j] || !filt->inputs[j]->src) {
  108. av_log(log_ctx, AV_LOG_ERROR,
  109. "Input pad \"%s\" for the filter \"%s\" of type \"%s\" not connected to any source\n",
  110. filt->input_pads[j].name, filt->name, filt->filter->name);
  111. return AVERROR(EINVAL);
  112. }
  113. }
  114. for (j = 0; j < filt->nb_outputs; j++) {
  115. if (!filt->outputs[j] || !filt->outputs[j]->dst) {
  116. av_log(log_ctx, AV_LOG_ERROR,
  117. "Output pad \"%s\" for the filter \"%s\" of type \"%s\" not connected to any destination\n",
  118. filt->output_pads[j].name, filt->name, filt->filter->name);
  119. return AVERROR(EINVAL);
  120. }
  121. }
  122. }
  123. return 0;
  124. }
  125. /**
  126. * Configure all the links of graphctx.
  127. *
  128. * @return 0 in case of success, a negative value otherwise
  129. */
  130. static int graph_config_links(AVFilterGraph *graph, AVClass *log_ctx)
  131. {
  132. AVFilterContext *filt;
  133. int i, ret;
  134. for (i=0; i < graph->filter_count; i++) {
  135. filt = graph->filters[i];
  136. if (!filt->nb_outputs) {
  137. if ((ret = avfilter_config_links(filt)))
  138. return ret;
  139. }
  140. }
  141. return 0;
  142. }
  143. AVFilterContext *avfilter_graph_get_filter(AVFilterGraph *graph, char *name)
  144. {
  145. int i;
  146. for (i = 0; i < graph->filter_count; i++)
  147. if (graph->filters[i]->name && !strcmp(name, graph->filters[i]->name))
  148. return graph->filters[i];
  149. return NULL;
  150. }
  151. static int filter_query_formats(AVFilterContext *ctx)
  152. {
  153. int ret;
  154. AVFilterFormats *formats;
  155. AVFilterChannelLayouts *chlayouts;
  156. AVFilterFormats *samplerates;
  157. enum AVMediaType type = ctx->inputs && ctx->inputs [0] ? ctx->inputs [0]->type :
  158. ctx->outputs && ctx->outputs[0] ? ctx->outputs[0]->type :
  159. AVMEDIA_TYPE_VIDEO;
  160. if ((ret = ctx->filter->query_formats(ctx)) < 0)
  161. return ret;
  162. formats = ff_all_formats(type);
  163. if (!formats)
  164. return AVERROR(ENOMEM);
  165. ff_set_common_formats(ctx, formats);
  166. if (type == AVMEDIA_TYPE_AUDIO) {
  167. samplerates = ff_all_samplerates();
  168. if (!samplerates)
  169. return AVERROR(ENOMEM);
  170. ff_set_common_samplerates(ctx, samplerates);
  171. chlayouts = ff_all_channel_layouts();
  172. if (!chlayouts)
  173. return AVERROR(ENOMEM);
  174. ff_set_common_channel_layouts(ctx, chlayouts);
  175. }
  176. return 0;
  177. }
  178. static int insert_conv_filter(AVFilterGraph *graph, AVFilterLink *link,
  179. const char *filt_name, const char *filt_args)
  180. {
  181. static int auto_count = 0, ret;
  182. char inst_name[32];
  183. AVFilterContext *filt_ctx;
  184. if (graph->disable_auto_convert) {
  185. av_log(NULL, AV_LOG_ERROR,
  186. "The filters '%s' and '%s' do not have a common format "
  187. "and automatic conversion is disabled.\n",
  188. link->src->name, link->dst->name);
  189. return AVERROR(EINVAL);
  190. }
  191. snprintf(inst_name, sizeof(inst_name), "auto-inserted %s %d",
  192. filt_name, auto_count++);
  193. if ((ret = avfilter_graph_create_filter(&filt_ctx,
  194. avfilter_get_by_name(filt_name),
  195. inst_name, filt_args, NULL, graph)) < 0)
  196. return ret;
  197. if ((ret = avfilter_insert_filter(link, filt_ctx, 0, 0)) < 0)
  198. return ret;
  199. filter_query_formats(filt_ctx);
  200. if ( ((link = filt_ctx-> inputs[0]) &&
  201. !ff_merge_formats(link->in_formats, link->out_formats)) ||
  202. ((link = filt_ctx->outputs[0]) &&
  203. !ff_merge_formats(link->in_formats, link->out_formats))
  204. ) {
  205. av_log(NULL, AV_LOG_ERROR,
  206. "Impossible to convert between the formats supported by the filter "
  207. "'%s' and the filter '%s'\n", link->src->name, link->dst->name);
  208. return AVERROR(EINVAL);
  209. }
  210. if (link->type == AVMEDIA_TYPE_AUDIO &&
  211. (((link = filt_ctx-> inputs[0]) &&
  212. !ff_merge_channel_layouts(link->in_channel_layouts, link->out_channel_layouts)) ||
  213. ((link = filt_ctx->outputs[0]) &&
  214. !ff_merge_channel_layouts(link->in_channel_layouts, link->out_channel_layouts)))
  215. ) {
  216. av_log(NULL, AV_LOG_ERROR,
  217. "Impossible to convert between the channel layouts formats supported by the filter "
  218. "'%s' and the filter '%s'\n", link->src->name, link->dst->name);
  219. return AVERROR(EINVAL);
  220. }
  221. return 0;
  222. }
  223. static int query_formats(AVFilterGraph *graph, AVClass *log_ctx)
  224. {
  225. int i, j, ret;
  226. char filt_args[128];
  227. AVFilterFormats *formats;
  228. AVFilterChannelLayouts *chlayouts;
  229. AVFilterFormats *samplerates;
  230. int scaler_count = 0, resampler_count = 0;
  231. for (j = 0; j < 2; j++) {
  232. /* ask all the sub-filters for their supported media formats */
  233. for (i = 0; i < graph->filter_count; i++) {
  234. /* Call query_formats on sources first.
  235. This is a temporary workaround for amerge,
  236. until format renegociation is implemented. */
  237. if (!graph->filters[i]->nb_inputs == j)
  238. continue;
  239. if (graph->filters[i]->filter->query_formats)
  240. ret = filter_query_formats(graph->filters[i]);
  241. else
  242. ret = ff_default_query_formats(graph->filters[i]);
  243. if (ret < 0)
  244. return ret;
  245. }
  246. }
  247. /* go through and merge as many format lists as possible */
  248. for (i = 0; i < graph->filter_count; i++) {
  249. AVFilterContext *filter = graph->filters[i];
  250. for (j = 0; j < filter->nb_inputs; j++) {
  251. AVFilterLink *link = filter->inputs[j];
  252. #if 0
  253. if (!link) continue;
  254. if (!link->in_formats || !link->out_formats)
  255. return AVERROR(EINVAL);
  256. if (link->type == AVMEDIA_TYPE_VIDEO &&
  257. !ff_merge_formats(link->in_formats, link->out_formats)) {
  258. /* couldn't merge format lists, auto-insert scale filter */
  259. snprintf(filt_args, sizeof(filt_args), "0:0:%s",
  260. graph->scale_sws_opts);
  261. if (ret = insert_conv_filter(graph, link, "scale", filt_args))
  262. return ret;
  263. }
  264. else if (link->type == AVMEDIA_TYPE_AUDIO) {
  265. if (!link->in_channel_layouts || !link->out_channel_layouts)
  266. return AVERROR(EINVAL);
  267. /* Merge all three list before checking: that way, in all
  268. * three categories, aconvert will use a common format
  269. * whenever possible. */
  270. formats = ff_merge_formats(link->in_formats, link->out_formats);
  271. chlayouts = ff_merge_channel_layouts(link->in_channel_layouts , link->out_channel_layouts);
  272. samplerates = ff_merge_samplerates (link->in_samplerates, link->out_samplerates);
  273. if (!formats || !chlayouts || !samplerates)
  274. if (ret = insert_conv_filter(graph, link, "aresample", NULL))
  275. return ret;
  276. #else
  277. int convert_needed = 0;
  278. if (!link)
  279. continue;
  280. if (link->in_formats != link->out_formats &&
  281. !ff_merge_formats(link->in_formats,
  282. link->out_formats))
  283. convert_needed = 1;
  284. if (link->type == AVMEDIA_TYPE_AUDIO) {
  285. if (link->in_channel_layouts != link->out_channel_layouts &&
  286. !ff_merge_channel_layouts(link->in_channel_layouts,
  287. link->out_channel_layouts))
  288. convert_needed = 1;
  289. if (link->in_samplerates != link->out_samplerates &&
  290. !ff_merge_samplerates(link->in_samplerates,
  291. link->out_samplerates))
  292. convert_needed = 1;
  293. }
  294. if (convert_needed) {
  295. AVFilterContext *convert;
  296. AVFilter *filter;
  297. AVFilterLink *inlink, *outlink;
  298. char scale_args[256];
  299. char inst_name[30];
  300. /* couldn't merge format lists. auto-insert conversion filter */
  301. switch (link->type) {
  302. case AVMEDIA_TYPE_VIDEO:
  303. if (!(filter = avfilter_get_by_name("scale"))) {
  304. av_log(log_ctx, AV_LOG_ERROR, "'scale' filter "
  305. "not present, cannot convert pixel formats.\n");
  306. return AVERROR(EINVAL);
  307. }
  308. snprintf(inst_name, sizeof(inst_name), "auto-inserted scaler %d",
  309. scaler_count++);
  310. snprintf(scale_args, sizeof(scale_args), "0:0:%s", graph->scale_sws_opts);
  311. if ((ret = avfilter_graph_create_filter(&convert, filter,
  312. inst_name, scale_args, NULL,
  313. graph)) < 0)
  314. return ret;
  315. break;
  316. case AVMEDIA_TYPE_AUDIO:
  317. if (!(filter = avfilter_get_by_name("aresample"))) {
  318. av_log(log_ctx, AV_LOG_ERROR, "'resample' filter "
  319. "not present, cannot convert audio formats.\n");
  320. return AVERROR(EINVAL);
  321. }
  322. snprintf(inst_name, sizeof(inst_name), "auto-inserted resampler %d",
  323. resampler_count++);
  324. if ((ret = avfilter_graph_create_filter(&convert, filter,
  325. inst_name, NULL, NULL, graph)) < 0)
  326. return ret;
  327. break;
  328. default:
  329. return AVERROR(EINVAL);
  330. }
  331. if ((ret = avfilter_insert_filter(link, convert, 0, 0)) < 0)
  332. return ret;
  333. filter_query_formats(convert);
  334. inlink = convert->inputs[0];
  335. outlink = convert->outputs[0];
  336. if (!ff_merge_formats( inlink->in_formats, inlink->out_formats) ||
  337. !ff_merge_formats(outlink->in_formats, outlink->out_formats))
  338. ret |= AVERROR(ENOSYS);
  339. if (inlink->type == AVMEDIA_TYPE_AUDIO &&
  340. (!ff_merge_samplerates(inlink->in_samplerates,
  341. inlink->out_samplerates) ||
  342. !ff_merge_channel_layouts(inlink->in_channel_layouts,
  343. inlink->out_channel_layouts)))
  344. ret |= AVERROR(ENOSYS);
  345. if (outlink->type == AVMEDIA_TYPE_AUDIO &&
  346. (!ff_merge_samplerates(outlink->in_samplerates,
  347. outlink->out_samplerates) ||
  348. !ff_merge_channel_layouts(outlink->in_channel_layouts,
  349. outlink->out_channel_layouts)))
  350. ret |= AVERROR(ENOSYS);
  351. if (ret < 0) {
  352. av_log(log_ctx, AV_LOG_ERROR,
  353. "Impossible to convert between the formats supported by the filter "
  354. "'%s' and the filter '%s'\n", link->src->name, link->dst->name);
  355. return ret;
  356. }
  357. #endif
  358. }
  359. }
  360. }
  361. return 0;
  362. }
  363. static int pick_format(AVFilterLink *link, AVFilterLink *ref)
  364. {
  365. if (!link || !link->in_formats)
  366. return 0;
  367. if (link->type == AVMEDIA_TYPE_VIDEO) {
  368. if(ref && ref->type == AVMEDIA_TYPE_VIDEO){
  369. int has_alpha= av_pix_fmt_descriptors[ref->format].nb_components % 2 == 0;
  370. enum PixelFormat best= PIX_FMT_NONE;
  371. int i;
  372. for (i=0; i<link->in_formats->format_count; i++) {
  373. enum PixelFormat p = link->in_formats->formats[i];
  374. best= avcodec_find_best_pix_fmt2(best, p, ref->format, has_alpha, NULL);
  375. }
  376. link->in_formats->formats[0] = best;
  377. }
  378. }
  379. link->in_formats->format_count = 1;
  380. link->format = link->in_formats->formats[0];
  381. if (link->type == AVMEDIA_TYPE_AUDIO) {
  382. if (!link->in_samplerates->format_count) {
  383. av_log(link->src, AV_LOG_ERROR, "Cannot select sample rate for"
  384. " the link between filters %s and %s.\n", link->src->name,
  385. link->dst->name);
  386. return AVERROR(EINVAL);
  387. }
  388. link->in_samplerates->format_count = 1;
  389. link->sample_rate = link->in_samplerates->formats[0];
  390. if (!link->in_channel_layouts->nb_channel_layouts) {
  391. av_log(link->src, AV_LOG_ERROR, "Cannot select channel layout for"
  392. "the link between filters %s and %s.\n", link->src->name,
  393. link->dst->name);
  394. return AVERROR(EINVAL);
  395. }
  396. link->in_channel_layouts->nb_channel_layouts = 1;
  397. link->channel_layout = link->in_channel_layouts->channel_layouts[0];
  398. }
  399. ff_formats_unref(&link->in_formats);
  400. ff_formats_unref(&link->out_formats);
  401. ff_formats_unref(&link->in_samplerates);
  402. ff_formats_unref(&link->out_samplerates);
  403. ff_channel_layouts_unref(&link->in_channel_layouts);
  404. ff_channel_layouts_unref(&link->out_channel_layouts);
  405. return 0;
  406. }
  407. #define REDUCE_FORMATS(fmt_type, list_type, list, var, nb, add_format) \
  408. do { \
  409. for (i = 0; i < filter->nb_inputs; i++) { \
  410. AVFilterLink *link = filter->inputs[i]; \
  411. fmt_type fmt; \
  412. \
  413. if (!link->out_ ## list || link->out_ ## list->nb != 1) \
  414. continue; \
  415. fmt = link->out_ ## list->var[0]; \
  416. \
  417. for (j = 0; j < filter->nb_outputs; j++) { \
  418. AVFilterLink *out_link = filter->outputs[j]; \
  419. list_type *fmts; \
  420. \
  421. if (link->type != out_link->type || \
  422. out_link->in_ ## list->nb == 1) \
  423. continue; \
  424. fmts = out_link->in_ ## list; \
  425. \
  426. if (!out_link->in_ ## list->nb) { \
  427. add_format(&out_link->in_ ##list, fmt); \
  428. break; \
  429. } \
  430. \
  431. for (k = 0; k < out_link->in_ ## list->nb; k++) \
  432. if (fmts->var[k] == fmt) { \
  433. fmts->var[0] = fmt; \
  434. fmts->nb = 1; \
  435. ret = 1; \
  436. break; \
  437. } \
  438. } \
  439. } \
  440. } while (0)
  441. static int reduce_formats_on_filter(AVFilterContext *filter)
  442. {
  443. int i, j, k, ret = 0;
  444. REDUCE_FORMATS(int, AVFilterFormats, formats, formats,
  445. format_count, ff_add_format);
  446. REDUCE_FORMATS(int, AVFilterFormats, samplerates, formats,
  447. format_count, ff_add_format);
  448. REDUCE_FORMATS(uint64_t, AVFilterChannelLayouts, channel_layouts,
  449. channel_layouts, nb_channel_layouts, ff_add_channel_layout);
  450. return ret;
  451. }
  452. static void reduce_formats(AVFilterGraph *graph)
  453. {
  454. int i, reduced;
  455. do {
  456. reduced = 0;
  457. for (i = 0; i < graph->filter_count; i++)
  458. reduced |= reduce_formats_on_filter(graph->filters[i]);
  459. } while (reduced);
  460. }
  461. static void swap_samplerates_on_filter(AVFilterContext *filter)
  462. {
  463. AVFilterLink *link = NULL;
  464. int sample_rate;
  465. int i, j;
  466. for (i = 0; i < filter->nb_inputs; i++) {
  467. link = filter->inputs[i];
  468. if (link->type == AVMEDIA_TYPE_AUDIO &&
  469. link->out_samplerates->format_count == 1)
  470. break;
  471. }
  472. if (i == filter->nb_inputs)
  473. return;
  474. sample_rate = link->out_samplerates->formats[0];
  475. for (i = 0; i < filter->nb_outputs; i++) {
  476. AVFilterLink *outlink = filter->outputs[i];
  477. int best_idx, best_diff = INT_MAX;
  478. if (outlink->type != AVMEDIA_TYPE_AUDIO ||
  479. outlink->in_samplerates->format_count < 2)
  480. continue;
  481. for (j = 0; j < outlink->in_samplerates->format_count; j++) {
  482. int diff = abs(sample_rate - outlink->in_samplerates->formats[j]);
  483. if (diff < best_diff) {
  484. best_diff = diff;
  485. best_idx = j;
  486. }
  487. }
  488. FFSWAP(int, outlink->in_samplerates->formats[0],
  489. outlink->in_samplerates->formats[best_idx]);
  490. }
  491. }
  492. static void swap_samplerates(AVFilterGraph *graph)
  493. {
  494. int i;
  495. for (i = 0; i < graph->filter_count; i++)
  496. swap_samplerates_on_filter(graph->filters[i]);
  497. }
  498. static void swap_channel_layouts_on_filter(AVFilterContext *filter)
  499. {
  500. AVFilterLink *link = NULL;
  501. uint64_t chlayout;
  502. int i, j;
  503. for (i = 0; i < filter->nb_inputs; i++) {
  504. link = filter->inputs[i];
  505. if (link->type == AVMEDIA_TYPE_AUDIO &&
  506. link->out_channel_layouts->nb_channel_layouts == 1)
  507. break;
  508. }
  509. if (i == filter->nb_inputs)
  510. return;
  511. chlayout = link->out_channel_layouts->channel_layouts[0];
  512. for (i = 0; i < filter->nb_outputs; i++) {
  513. AVFilterLink *outlink = filter->outputs[i];
  514. int best_idx, best_score = INT_MIN;
  515. if (outlink->type != AVMEDIA_TYPE_AUDIO ||
  516. outlink->in_channel_layouts->nb_channel_layouts < 2)
  517. continue;
  518. for (j = 0; j < outlink->in_channel_layouts->nb_channel_layouts; j++) {
  519. uint64_t out_chlayout = outlink->in_channel_layouts->channel_layouts[j];
  520. int matched_channels = av_get_channel_layout_nb_channels(chlayout &
  521. out_chlayout);
  522. int extra_channels = av_get_channel_layout_nb_channels(out_chlayout &
  523. (~chlayout));
  524. int score = matched_channels - extra_channels;
  525. if (score > best_score) {
  526. best_score = score;
  527. best_idx = j;
  528. }
  529. }
  530. FFSWAP(uint64_t, outlink->in_channel_layouts->channel_layouts[0],
  531. outlink->in_channel_layouts->channel_layouts[best_idx]);
  532. }
  533. }
  534. static void swap_channel_layouts(AVFilterGraph *graph)
  535. {
  536. int i;
  537. for (i = 0; i < graph->filter_count; i++)
  538. swap_channel_layouts_on_filter(graph->filters[i]);
  539. }
  540. static void swap_sample_fmts_on_filter(AVFilterContext *filter)
  541. {
  542. AVFilterLink *link = NULL;
  543. int format, bps;
  544. int i, j;
  545. for (i = 0; i < filter->nb_inputs; i++) {
  546. link = filter->inputs[i];
  547. if (link->type == AVMEDIA_TYPE_AUDIO &&
  548. link->out_formats->format_count == 1)
  549. break;
  550. }
  551. if (i == filter->nb_inputs)
  552. return;
  553. format = link->out_formats->formats[0];
  554. bps = av_get_bytes_per_sample(format);
  555. for (i = 0; i < filter->nb_outputs; i++) {
  556. AVFilterLink *outlink = filter->outputs[i];
  557. int best_idx = -1, best_score = INT_MIN;
  558. if (outlink->type != AVMEDIA_TYPE_AUDIO ||
  559. outlink->in_formats->format_count < 2)
  560. continue;
  561. for (j = 0; j < outlink->in_formats->format_count; j++) {
  562. int out_format = outlink->in_formats->formats[j];
  563. int out_bps = av_get_bytes_per_sample(out_format);
  564. int score;
  565. if (av_get_packed_sample_fmt(out_format) == format ||
  566. av_get_planar_sample_fmt(out_format) == format) {
  567. best_idx = j;
  568. break;
  569. }
  570. /* for s32 and float prefer double to prevent loss of information */
  571. if (bps == 4 && out_bps == 8) {
  572. best_idx = j;
  573. break;
  574. }
  575. /* prefer closest higher or equal bps */
  576. score = -abs(out_bps - bps);
  577. if (out_bps >= bps)
  578. score += INT_MAX/2;
  579. if (score > best_score) {
  580. best_score = score;
  581. best_idx = j;
  582. }
  583. }
  584. av_assert0(best_idx >= 0);
  585. FFSWAP(int, outlink->in_formats->formats[0],
  586. outlink->in_formats->formats[best_idx]);
  587. }
  588. }
  589. static void swap_sample_fmts(AVFilterGraph *graph)
  590. {
  591. int i;
  592. for (i = 0; i < graph->filter_count; i++)
  593. swap_sample_fmts_on_filter(graph->filters[i]);
  594. }
  595. static int pick_formats(AVFilterGraph *graph)
  596. {
  597. int i, j, ret;
  598. int change;
  599. do{
  600. change = 0;
  601. for (i = 0; i < graph->filter_count; i++) {
  602. AVFilterContext *filter = graph->filters[i];
  603. if (filter->nb_inputs){
  604. for (j = 0; j < filter->nb_inputs; j++){
  605. if(filter->inputs[j]->in_formats && filter->inputs[j]->in_formats->format_count == 1) {
  606. pick_format(filter->inputs[j], NULL);
  607. change = 1;
  608. }
  609. }
  610. }
  611. if (filter->nb_outputs){
  612. for (j = 0; j < filter->nb_outputs; j++){
  613. if(filter->outputs[j]->in_formats && filter->outputs[j]->in_formats->format_count == 1) {
  614. pick_format(filter->outputs[j], NULL);
  615. change = 1;
  616. }
  617. }
  618. }
  619. if (filter->nb_inputs && filter->nb_outputs && filter->inputs[0]->format>=0) {
  620. for (j = 0; j < filter->nb_outputs; j++) {
  621. if(filter->outputs[j]->format<0) {
  622. pick_format(filter->outputs[j], filter->inputs[0]);
  623. change = 1;
  624. }
  625. }
  626. }
  627. }
  628. }while(change);
  629. for (i = 0; i < graph->filter_count; i++) {
  630. AVFilterContext *filter = graph->filters[i];
  631. for (j = 0; j < filter->nb_inputs; j++)
  632. if ((ret = pick_format(filter->inputs[j], NULL)) < 0)
  633. return ret;
  634. for (j = 0; j < filter->nb_outputs; j++)
  635. if ((ret = pick_format(filter->outputs[j], NULL)) < 0)
  636. return ret;
  637. }
  638. return 0;
  639. }
  640. /**
  641. * Configure the formats of all the links in the graph.
  642. */
  643. static int graph_config_formats(AVFilterGraph *graph, AVClass *log_ctx)
  644. {
  645. int ret;
  646. /* find supported formats from sub-filters, and merge along links */
  647. if ((ret = query_formats(graph, log_ctx)) < 0)
  648. return ret;
  649. /* Once everything is merged, it's possible that we'll still have
  650. * multiple valid media format choices. We try to minimize the amount
  651. * of format conversion inside filters */
  652. reduce_formats(graph);
  653. /* for audio filters, ensure the best format, sample rate and channel layout
  654. * is selected */
  655. swap_sample_fmts(graph);
  656. swap_samplerates(graph);
  657. swap_channel_layouts(graph);
  658. if ((ret = pick_formats(graph)) < 0)
  659. return ret;
  660. return 0;
  661. }
  662. static int ff_avfilter_graph_config_pointers(AVFilterGraph *graph,
  663. AVClass *log_ctx)
  664. {
  665. unsigned i, j;
  666. int sink_links_count = 0, n = 0;
  667. AVFilterContext *f;
  668. AVFilterLink **sinks;
  669. for (i = 0; i < graph->filter_count; i++) {
  670. f = graph->filters[i];
  671. for (j = 0; j < f->nb_inputs; j++) {
  672. f->inputs[j]->graph = graph;
  673. f->inputs[j]->age_index = -1;
  674. }
  675. for (j = 0; j < f->nb_outputs; j++) {
  676. f->outputs[j]->graph = graph;
  677. f->outputs[j]->age_index= -1;
  678. }
  679. if (!f->nb_outputs) {
  680. if (f->nb_inputs > INT_MAX - sink_links_count)
  681. return AVERROR(EINVAL);
  682. sink_links_count += f->nb_inputs;
  683. }
  684. }
  685. sinks = av_calloc(sink_links_count, sizeof(*sinks));
  686. if (!sinks)
  687. return AVERROR(ENOMEM);
  688. for (i = 0; i < graph->filter_count; i++) {
  689. f = graph->filters[i];
  690. if (!f->nb_outputs) {
  691. for (j = 0; j < f->nb_inputs; j++) {
  692. sinks[n] = f->inputs[j];
  693. f->inputs[j]->age_index = n++;
  694. }
  695. }
  696. }
  697. av_assert0(n == sink_links_count);
  698. graph->sink_links = sinks;
  699. graph->sink_links_count = sink_links_count;
  700. return 0;
  701. }
  702. static int graph_insert_fifos(AVFilterGraph *graph, AVClass *log_ctx)
  703. {
  704. AVFilterContext *f;
  705. int i, j, ret;
  706. int fifo_count = 0;
  707. for (i = 0; i < graph->filter_count; i++) {
  708. f = graph->filters[i];
  709. for (j = 0; j < f->nb_inputs; j++) {
  710. AVFilterLink *link = f->inputs[j];
  711. AVFilterContext *fifo_ctx;
  712. AVFilter *fifo;
  713. char name[32];
  714. if (!link->dstpad->needs_fifo)
  715. continue;
  716. fifo = f->inputs[j]->type == AVMEDIA_TYPE_VIDEO ?
  717. avfilter_get_by_name("fifo") :
  718. avfilter_get_by_name("afifo");
  719. snprintf(name, sizeof(name), "auto-inserted fifo %d", fifo_count++);
  720. ret = avfilter_graph_create_filter(&fifo_ctx, fifo, name, NULL,
  721. NULL, graph);
  722. if (ret < 0)
  723. return ret;
  724. ret = avfilter_insert_filter(link, fifo_ctx, 0, 0);
  725. if (ret < 0)
  726. return ret;
  727. }
  728. }
  729. return 0;
  730. }
  731. int avfilter_graph_config(AVFilterGraph *graphctx, void *log_ctx)
  732. {
  733. int ret;
  734. if ((ret = graph_check_validity(graphctx, log_ctx)))
  735. return ret;
  736. if ((ret = graph_insert_fifos(graphctx, log_ctx)) < 0)
  737. return ret;
  738. if ((ret = graph_config_formats(graphctx, log_ctx)))
  739. return ret;
  740. if ((ret = graph_config_links(graphctx, log_ctx)))
  741. return ret;
  742. if ((ret = ff_avfilter_graph_config_pointers(graphctx, log_ctx)))
  743. return ret;
  744. return 0;
  745. }
  746. int avfilter_graph_send_command(AVFilterGraph *graph, const char *target, const char *cmd, const char *arg, char *res, int res_len, int flags)
  747. {
  748. int i, r = AVERROR(ENOSYS);
  749. if(!graph)
  750. return r;
  751. if((flags & AVFILTER_CMD_FLAG_ONE) && !(flags & AVFILTER_CMD_FLAG_FAST)) {
  752. r=avfilter_graph_send_command(graph, target, cmd, arg, res, res_len, flags | AVFILTER_CMD_FLAG_FAST);
  753. if(r != AVERROR(ENOSYS))
  754. return r;
  755. }
  756. if(res_len && res)
  757. res[0]= 0;
  758. for (i = 0; i < graph->filter_count; i++) {
  759. AVFilterContext *filter = graph->filters[i];
  760. if(!strcmp(target, "all") || (filter->name && !strcmp(target, filter->name)) || !strcmp(target, filter->filter->name)){
  761. r = avfilter_process_command(filter, cmd, arg, res, res_len, flags);
  762. if(r != AVERROR(ENOSYS)) {
  763. if((flags & AVFILTER_CMD_FLAG_ONE) || r<0)
  764. return r;
  765. }
  766. }
  767. }
  768. return r;
  769. }
  770. int avfilter_graph_queue_command(AVFilterGraph *graph, const char *target, const char *command, const char *arg, int flags, double ts)
  771. {
  772. int i;
  773. if(!graph)
  774. return 0;
  775. for (i = 0; i < graph->filter_count; i++) {
  776. AVFilterContext *filter = graph->filters[i];
  777. if(filter && (!strcmp(target, "all") || !strcmp(target, filter->name) || !strcmp(target, filter->filter->name))){
  778. AVFilterCommand **que = &filter->command_queue, *next;
  779. while(*que && (*que)->time <= ts)
  780. que = &(*que)->next;
  781. next= *que;
  782. *que= av_mallocz(sizeof(AVFilterCommand));
  783. (*que)->command = av_strdup(command);
  784. (*que)->arg = av_strdup(arg);
  785. (*que)->time = ts;
  786. (*que)->flags = flags;
  787. (*que)->next = next;
  788. if(flags & AVFILTER_CMD_FLAG_ONE)
  789. return 0;
  790. }
  791. }
  792. return 0;
  793. }
  794. static void heap_bubble_up(AVFilterGraph *graph,
  795. AVFilterLink *link, int index)
  796. {
  797. AVFilterLink **links = graph->sink_links;
  798. while (index) {
  799. int parent = (index - 1) >> 1;
  800. if (links[parent]->current_pts >= link->current_pts)
  801. break;
  802. links[index] = links[parent];
  803. links[index]->age_index = index;
  804. index = parent;
  805. }
  806. links[index] = link;
  807. link->age_index = index;
  808. }
  809. static void heap_bubble_down(AVFilterGraph *graph,
  810. AVFilterLink *link, int index)
  811. {
  812. AVFilterLink **links = graph->sink_links;
  813. while (1) {
  814. int child = 2 * index + 1;
  815. if (child >= graph->sink_links_count)
  816. break;
  817. if (child + 1 < graph->sink_links_count &&
  818. links[child + 1]->current_pts < links[child]->current_pts)
  819. child++;
  820. if (link->current_pts < links[child]->current_pts)
  821. break;
  822. links[index] = links[child];
  823. links[index]->age_index = index;
  824. index = child;
  825. }
  826. links[index] = link;
  827. link->age_index = index;
  828. }
  829. void ff_avfilter_graph_update_heap(AVFilterGraph *graph, AVFilterLink *link)
  830. {
  831. heap_bubble_up (graph, link, link->age_index);
  832. heap_bubble_down(graph, link, link->age_index);
  833. }
  834. int avfilter_graph_request_oldest(AVFilterGraph *graph)
  835. {
  836. while (graph->sink_links_count) {
  837. AVFilterLink *oldest = graph->sink_links[0];
  838. int r = ff_request_frame(oldest);
  839. if (r != AVERROR_EOF)
  840. return r;
  841. /* EOF: remove the link from the heap */
  842. if (oldest->age_index < --graph->sink_links_count)
  843. heap_bubble_down(graph, graph->sink_links[graph->sink_links_count],
  844. oldest->age_index);
  845. oldest->age_index = -1;
  846. }
  847. return AVERROR_EOF;
  848. }