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.

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