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.

952 lines
33KB

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