You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1000 lines
34KB

  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, "'aresample' 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. av_log(link->src,AV_LOG_DEBUG, "picking %s out of %d ref:%s alpha:%d\n",
  377. av_get_pix_fmt_name(best), link->in_formats->format_count,
  378. av_get_pix_fmt_name(ref->format), has_alpha);
  379. link->in_formats->formats[0] = best;
  380. }
  381. }
  382. link->in_formats->format_count = 1;
  383. link->format = link->in_formats->formats[0];
  384. if (link->type == AVMEDIA_TYPE_AUDIO) {
  385. if (!link->in_samplerates->format_count) {
  386. av_log(link->src, AV_LOG_ERROR, "Cannot select sample rate for"
  387. " the link between filters %s and %s.\n", link->src->name,
  388. link->dst->name);
  389. return AVERROR(EINVAL);
  390. }
  391. link->in_samplerates->format_count = 1;
  392. link->sample_rate = link->in_samplerates->formats[0];
  393. if (!link->in_channel_layouts->nb_channel_layouts) {
  394. av_log(link->src, AV_LOG_ERROR, "Cannot select channel layout for"
  395. "the link between filters %s and %s.\n", link->src->name,
  396. link->dst->name);
  397. return AVERROR(EINVAL);
  398. }
  399. link->in_channel_layouts->nb_channel_layouts = 1;
  400. link->channel_layout = link->in_channel_layouts->channel_layouts[0];
  401. }
  402. ff_formats_unref(&link->in_formats);
  403. ff_formats_unref(&link->out_formats);
  404. ff_formats_unref(&link->in_samplerates);
  405. ff_formats_unref(&link->out_samplerates);
  406. ff_channel_layouts_unref(&link->in_channel_layouts);
  407. ff_channel_layouts_unref(&link->out_channel_layouts);
  408. return 0;
  409. }
  410. #define REDUCE_FORMATS(fmt_type, list_type, list, var, nb, add_format) \
  411. do { \
  412. for (i = 0; i < filter->nb_inputs; i++) { \
  413. AVFilterLink *link = filter->inputs[i]; \
  414. fmt_type fmt; \
  415. \
  416. if (!link->out_ ## list || link->out_ ## list->nb != 1) \
  417. continue; \
  418. fmt = link->out_ ## list->var[0]; \
  419. \
  420. for (j = 0; j < filter->nb_outputs; j++) { \
  421. AVFilterLink *out_link = filter->outputs[j]; \
  422. list_type *fmts; \
  423. \
  424. if (link->type != out_link->type || \
  425. out_link->in_ ## list->nb == 1) \
  426. continue; \
  427. fmts = out_link->in_ ## list; \
  428. \
  429. if (!out_link->in_ ## list->nb) { \
  430. add_format(&out_link->in_ ##list, fmt); \
  431. break; \
  432. } \
  433. \
  434. for (k = 0; k < out_link->in_ ## list->nb; k++) \
  435. if (fmts->var[k] == fmt) { \
  436. fmts->var[0] = fmt; \
  437. fmts->nb = 1; \
  438. ret = 1; \
  439. break; \
  440. } \
  441. } \
  442. } \
  443. } while (0)
  444. static int reduce_formats_on_filter(AVFilterContext *filter)
  445. {
  446. int i, j, k, ret = 0;
  447. REDUCE_FORMATS(int, AVFilterFormats, formats, formats,
  448. format_count, ff_add_format);
  449. REDUCE_FORMATS(int, AVFilterFormats, samplerates, formats,
  450. format_count, ff_add_format);
  451. REDUCE_FORMATS(uint64_t, AVFilterChannelLayouts, channel_layouts,
  452. channel_layouts, nb_channel_layouts, ff_add_channel_layout);
  453. return ret;
  454. }
  455. static void reduce_formats(AVFilterGraph *graph)
  456. {
  457. int i, reduced;
  458. do {
  459. reduced = 0;
  460. for (i = 0; i < graph->filter_count; i++)
  461. reduced |= reduce_formats_on_filter(graph->filters[i]);
  462. } while (reduced);
  463. }
  464. static void swap_samplerates_on_filter(AVFilterContext *filter)
  465. {
  466. AVFilterLink *link = NULL;
  467. int sample_rate;
  468. int i, j;
  469. for (i = 0; i < filter->nb_inputs; i++) {
  470. link = filter->inputs[i];
  471. if (link->type == AVMEDIA_TYPE_AUDIO &&
  472. link->out_samplerates->format_count == 1)
  473. break;
  474. }
  475. if (i == filter->nb_inputs)
  476. return;
  477. sample_rate = link->out_samplerates->formats[0];
  478. for (i = 0; i < filter->nb_outputs; i++) {
  479. AVFilterLink *outlink = filter->outputs[i];
  480. int best_idx, best_diff = INT_MAX;
  481. if (outlink->type != AVMEDIA_TYPE_AUDIO ||
  482. outlink->in_samplerates->format_count < 2)
  483. continue;
  484. for (j = 0; j < outlink->in_samplerates->format_count; j++) {
  485. int diff = abs(sample_rate - outlink->in_samplerates->formats[j]);
  486. if (diff < best_diff) {
  487. best_diff = diff;
  488. best_idx = j;
  489. }
  490. }
  491. FFSWAP(int, outlink->in_samplerates->formats[0],
  492. outlink->in_samplerates->formats[best_idx]);
  493. }
  494. }
  495. static void swap_samplerates(AVFilterGraph *graph)
  496. {
  497. int i;
  498. for (i = 0; i < graph->filter_count; i++)
  499. swap_samplerates_on_filter(graph->filters[i]);
  500. }
  501. static void swap_channel_layouts_on_filter(AVFilterContext *filter)
  502. {
  503. AVFilterLink *link = NULL;
  504. uint64_t chlayout;
  505. int i, j;
  506. for (i = 0; i < filter->nb_inputs; i++) {
  507. link = filter->inputs[i];
  508. if (link->type == AVMEDIA_TYPE_AUDIO &&
  509. link->out_channel_layouts->nb_channel_layouts == 1)
  510. break;
  511. }
  512. if (i == filter->nb_inputs)
  513. return;
  514. chlayout = link->out_channel_layouts->channel_layouts[0];
  515. for (i = 0; i < filter->nb_outputs; i++) {
  516. AVFilterLink *outlink = filter->outputs[i];
  517. int best_idx, best_score = INT_MIN;
  518. if (outlink->type != AVMEDIA_TYPE_AUDIO ||
  519. outlink->in_channel_layouts->nb_channel_layouts < 2)
  520. continue;
  521. for (j = 0; j < outlink->in_channel_layouts->nb_channel_layouts; j++) {
  522. uint64_t out_chlayout = outlink->in_channel_layouts->channel_layouts[j];
  523. int matched_channels = av_get_channel_layout_nb_channels(chlayout &
  524. out_chlayout);
  525. int extra_channels = av_get_channel_layout_nb_channels(out_chlayout &
  526. (~chlayout));
  527. int score = matched_channels - extra_channels;
  528. if (score > best_score) {
  529. best_score = score;
  530. best_idx = j;
  531. }
  532. }
  533. FFSWAP(uint64_t, outlink->in_channel_layouts->channel_layouts[0],
  534. outlink->in_channel_layouts->channel_layouts[best_idx]);
  535. }
  536. }
  537. static void swap_channel_layouts(AVFilterGraph *graph)
  538. {
  539. int i;
  540. for (i = 0; i < graph->filter_count; i++)
  541. swap_channel_layouts_on_filter(graph->filters[i]);
  542. }
  543. static void swap_sample_fmts_on_filter(AVFilterContext *filter)
  544. {
  545. AVFilterLink *link = NULL;
  546. int format, bps;
  547. int i, j;
  548. for (i = 0; i < filter->nb_inputs; i++) {
  549. link = filter->inputs[i];
  550. if (link->type == AVMEDIA_TYPE_AUDIO &&
  551. link->out_formats->format_count == 1)
  552. break;
  553. }
  554. if (i == filter->nb_inputs)
  555. return;
  556. format = link->out_formats->formats[0];
  557. bps = av_get_bytes_per_sample(format);
  558. for (i = 0; i < filter->nb_outputs; i++) {
  559. AVFilterLink *outlink = filter->outputs[i];
  560. int best_idx = -1, best_score = INT_MIN;
  561. if (outlink->type != AVMEDIA_TYPE_AUDIO ||
  562. outlink->in_formats->format_count < 2)
  563. continue;
  564. for (j = 0; j < outlink->in_formats->format_count; j++) {
  565. int out_format = outlink->in_formats->formats[j];
  566. int out_bps = av_get_bytes_per_sample(out_format);
  567. int score;
  568. if (av_get_packed_sample_fmt(out_format) == format ||
  569. av_get_planar_sample_fmt(out_format) == format) {
  570. best_idx = j;
  571. break;
  572. }
  573. /* for s32 and float prefer double to prevent loss of information */
  574. if (bps == 4 && out_bps == 8) {
  575. best_idx = j;
  576. break;
  577. }
  578. /* prefer closest higher or equal bps */
  579. score = -abs(out_bps - bps);
  580. if (out_bps >= bps)
  581. score += INT_MAX/2;
  582. if (score > best_score) {
  583. best_score = score;
  584. best_idx = j;
  585. }
  586. }
  587. av_assert0(best_idx >= 0);
  588. FFSWAP(int, outlink->in_formats->formats[0],
  589. outlink->in_formats->formats[best_idx]);
  590. }
  591. }
  592. static void swap_sample_fmts(AVFilterGraph *graph)
  593. {
  594. int i;
  595. for (i = 0; i < graph->filter_count; i++)
  596. swap_sample_fmts_on_filter(graph->filters[i]);
  597. }
  598. static int pick_formats(AVFilterGraph *graph)
  599. {
  600. int i, j, ret;
  601. int change;
  602. do{
  603. change = 0;
  604. for (i = 0; i < graph->filter_count; i++) {
  605. AVFilterContext *filter = graph->filters[i];
  606. if (filter->nb_inputs){
  607. for (j = 0; j < filter->nb_inputs; j++){
  608. if(filter->inputs[j]->in_formats && filter->inputs[j]->in_formats->format_count == 1) {
  609. pick_format(filter->inputs[j], NULL);
  610. change = 1;
  611. }
  612. }
  613. }
  614. if (filter->nb_outputs){
  615. for (j = 0; j < filter->nb_outputs; j++){
  616. if(filter->outputs[j]->in_formats && filter->outputs[j]->in_formats->format_count == 1) {
  617. pick_format(filter->outputs[j], NULL);
  618. change = 1;
  619. }
  620. }
  621. }
  622. if (filter->nb_inputs && filter->nb_outputs && filter->inputs[0]->format>=0) {
  623. for (j = 0; j < filter->nb_outputs; j++) {
  624. if(filter->outputs[j]->format<0) {
  625. pick_format(filter->outputs[j], filter->inputs[0]);
  626. change = 1;
  627. }
  628. }
  629. }
  630. }
  631. }while(change);
  632. for (i = 0; i < graph->filter_count; i++) {
  633. AVFilterContext *filter = graph->filters[i];
  634. for (j = 0; j < filter->nb_inputs; j++)
  635. if ((ret = pick_format(filter->inputs[j], NULL)) < 0)
  636. return ret;
  637. for (j = 0; j < filter->nb_outputs; j++)
  638. if ((ret = pick_format(filter->outputs[j], NULL)) < 0)
  639. return ret;
  640. }
  641. return 0;
  642. }
  643. /**
  644. * Configure the formats of all the links in the graph.
  645. */
  646. static int graph_config_formats(AVFilterGraph *graph, AVClass *log_ctx)
  647. {
  648. int ret;
  649. /* find supported formats from sub-filters, and merge along links */
  650. if ((ret = query_formats(graph, log_ctx)) < 0)
  651. return ret;
  652. /* Once everything is merged, it's possible that we'll still have
  653. * multiple valid media format choices. We try to minimize the amount
  654. * of format conversion inside filters */
  655. reduce_formats(graph);
  656. /* for audio filters, ensure the best format, sample rate and channel layout
  657. * is selected */
  658. swap_sample_fmts(graph);
  659. swap_samplerates(graph);
  660. swap_channel_layouts(graph);
  661. if ((ret = pick_formats(graph)) < 0)
  662. return ret;
  663. return 0;
  664. }
  665. static int ff_avfilter_graph_config_pointers(AVFilterGraph *graph,
  666. AVClass *log_ctx)
  667. {
  668. unsigned i, j;
  669. int sink_links_count = 0, n = 0;
  670. AVFilterContext *f;
  671. AVFilterLink **sinks;
  672. for (i = 0; i < graph->filter_count; i++) {
  673. f = graph->filters[i];
  674. for (j = 0; j < f->nb_inputs; j++) {
  675. f->inputs[j]->graph = graph;
  676. f->inputs[j]->age_index = -1;
  677. }
  678. for (j = 0; j < f->nb_outputs; j++) {
  679. f->outputs[j]->graph = graph;
  680. f->outputs[j]->age_index= -1;
  681. }
  682. if (!f->nb_outputs) {
  683. if (f->nb_inputs > INT_MAX - sink_links_count)
  684. return AVERROR(EINVAL);
  685. sink_links_count += f->nb_inputs;
  686. }
  687. }
  688. sinks = av_calloc(sink_links_count, sizeof(*sinks));
  689. if (!sinks)
  690. return AVERROR(ENOMEM);
  691. for (i = 0; i < graph->filter_count; i++) {
  692. f = graph->filters[i];
  693. if (!f->nb_outputs) {
  694. for (j = 0; j < f->nb_inputs; j++) {
  695. sinks[n] = f->inputs[j];
  696. f->inputs[j]->age_index = n++;
  697. }
  698. }
  699. }
  700. av_assert0(n == sink_links_count);
  701. graph->sink_links = sinks;
  702. graph->sink_links_count = sink_links_count;
  703. return 0;
  704. }
  705. static int graph_insert_fifos(AVFilterGraph *graph, AVClass *log_ctx)
  706. {
  707. AVFilterContext *f;
  708. int i, j, ret;
  709. int fifo_count = 0;
  710. for (i = 0; i < graph->filter_count; i++) {
  711. f = graph->filters[i];
  712. for (j = 0; j < f->nb_inputs; j++) {
  713. AVFilterLink *link = f->inputs[j];
  714. AVFilterContext *fifo_ctx;
  715. AVFilter *fifo;
  716. char name[32];
  717. if (!link->dstpad->needs_fifo)
  718. continue;
  719. fifo = f->inputs[j]->type == AVMEDIA_TYPE_VIDEO ?
  720. avfilter_get_by_name("fifo") :
  721. avfilter_get_by_name("afifo");
  722. snprintf(name, sizeof(name), "auto-inserted fifo %d", fifo_count++);
  723. ret = avfilter_graph_create_filter(&fifo_ctx, fifo, name, NULL,
  724. NULL, graph);
  725. if (ret < 0)
  726. return ret;
  727. ret = avfilter_insert_filter(link, fifo_ctx, 0, 0);
  728. if (ret < 0)
  729. return ret;
  730. }
  731. }
  732. return 0;
  733. }
  734. int avfilter_graph_config(AVFilterGraph *graphctx, void *log_ctx)
  735. {
  736. int ret;
  737. if ((ret = graph_check_validity(graphctx, log_ctx)))
  738. return ret;
  739. if ((ret = graph_insert_fifos(graphctx, log_ctx)) < 0)
  740. return ret;
  741. if ((ret = graph_config_formats(graphctx, log_ctx)))
  742. return ret;
  743. if ((ret = graph_config_links(graphctx, log_ctx)))
  744. return ret;
  745. if ((ret = ff_avfilter_graph_config_pointers(graphctx, log_ctx)))
  746. return ret;
  747. return 0;
  748. }
  749. int avfilter_graph_send_command(AVFilterGraph *graph, const char *target, const char *cmd, const char *arg, char *res, int res_len, int flags)
  750. {
  751. int i, r = AVERROR(ENOSYS);
  752. if(!graph)
  753. return r;
  754. if((flags & AVFILTER_CMD_FLAG_ONE) && !(flags & AVFILTER_CMD_FLAG_FAST)) {
  755. r=avfilter_graph_send_command(graph, target, cmd, arg, res, res_len, flags | AVFILTER_CMD_FLAG_FAST);
  756. if(r != AVERROR(ENOSYS))
  757. return r;
  758. }
  759. if(res_len && res)
  760. res[0]= 0;
  761. for (i = 0; i < graph->filter_count; i++) {
  762. AVFilterContext *filter = graph->filters[i];
  763. if(!strcmp(target, "all") || (filter->name && !strcmp(target, filter->name)) || !strcmp(target, filter->filter->name)){
  764. r = avfilter_process_command(filter, cmd, arg, res, res_len, flags);
  765. if(r != AVERROR(ENOSYS)) {
  766. if((flags & AVFILTER_CMD_FLAG_ONE) || r<0)
  767. return r;
  768. }
  769. }
  770. }
  771. return r;
  772. }
  773. int avfilter_graph_queue_command(AVFilterGraph *graph, const char *target, const char *command, const char *arg, int flags, double ts)
  774. {
  775. int i;
  776. if(!graph)
  777. return 0;
  778. for (i = 0; i < graph->filter_count; i++) {
  779. AVFilterContext *filter = graph->filters[i];
  780. if(filter && (!strcmp(target, "all") || !strcmp(target, filter->name) || !strcmp(target, filter->filter->name))){
  781. AVFilterCommand **que = &filter->command_queue, *next;
  782. while(*que && (*que)->time <= ts)
  783. que = &(*que)->next;
  784. next= *que;
  785. *que= av_mallocz(sizeof(AVFilterCommand));
  786. (*que)->command = av_strdup(command);
  787. (*que)->arg = av_strdup(arg);
  788. (*que)->time = ts;
  789. (*que)->flags = flags;
  790. (*que)->next = next;
  791. if(flags & AVFILTER_CMD_FLAG_ONE)
  792. return 0;
  793. }
  794. }
  795. return 0;
  796. }
  797. static void heap_bubble_up(AVFilterGraph *graph,
  798. AVFilterLink *link, int index)
  799. {
  800. AVFilterLink **links = graph->sink_links;
  801. while (index) {
  802. int parent = (index - 1) >> 1;
  803. if (links[parent]->current_pts >= link->current_pts)
  804. break;
  805. links[index] = links[parent];
  806. links[index]->age_index = index;
  807. index = parent;
  808. }
  809. links[index] = link;
  810. link->age_index = index;
  811. }
  812. static void heap_bubble_down(AVFilterGraph *graph,
  813. AVFilterLink *link, int index)
  814. {
  815. AVFilterLink **links = graph->sink_links;
  816. while (1) {
  817. int child = 2 * index + 1;
  818. if (child >= graph->sink_links_count)
  819. break;
  820. if (child + 1 < graph->sink_links_count &&
  821. links[child + 1]->current_pts < links[child]->current_pts)
  822. child++;
  823. if (link->current_pts < links[child]->current_pts)
  824. break;
  825. links[index] = links[child];
  826. links[index]->age_index = index;
  827. index = child;
  828. }
  829. links[index] = link;
  830. link->age_index = index;
  831. }
  832. void ff_avfilter_graph_update_heap(AVFilterGraph *graph, AVFilterLink *link)
  833. {
  834. heap_bubble_up (graph, link, link->age_index);
  835. heap_bubble_down(graph, link, link->age_index);
  836. }
  837. int avfilter_graph_request_oldest(AVFilterGraph *graph)
  838. {
  839. while (graph->sink_links_count) {
  840. AVFilterLink *oldest = graph->sink_links[0];
  841. int r = ff_request_frame(oldest);
  842. if (r != AVERROR_EOF)
  843. return r;
  844. av_log(oldest->dst, AV_LOG_DEBUG, "EOF on sink link %s:%s.\n",
  845. oldest->dst ? oldest->dst->name : "unknown",
  846. oldest->dstpad ? oldest->dstpad->name : "unknown");
  847. /* EOF: remove the link from the heap */
  848. if (oldest->age_index < --graph->sink_links_count)
  849. heap_bubble_down(graph, graph->sink_links[graph->sink_links_count],
  850. oldest->age_index);
  851. oldest->age_index = -1;
  852. }
  853. return AVERROR_EOF;
  854. }