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.

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