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.

909 lines
31KB

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