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.

918 lines
32KB

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