avfiltergraph.c 31 KB

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