avfiltergraph.c 37 KB

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