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.

1184 lines
41KB

  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 <string.h>
  23. #include "libavutil/avassert.h"
  24. #include "libavutil/avstring.h"
  25. #include "libavutil/bprint.h"
  26. #include "libavutil/channel_layout.h"
  27. #include "libavutil/opt.h"
  28. #include "libavutil/pixdesc.h"
  29. #include "libavcodec/avcodec.h" // avcodec_find_best_pix_fmt_of_2()
  30. #include "avfilter.h"
  31. #include "formats.h"
  32. #include "internal.h"
  33. #define OFFSET(x) offsetof(AVFilterGraph,x)
  34. static const AVOption options[]={
  35. {"scale_sws_opts" , "default scale filter options" , OFFSET(scale_sws_opts) , AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, 0 },
  36. {"aresample_swr_opts" , "default aresample filter options" , OFFSET(aresample_swr_opts) , AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, 0 },
  37. {0}
  38. };
  39. static const AVClass filtergraph_class = {
  40. .class_name = "AVFilterGraph",
  41. .item_name = av_default_item_name,
  42. .option = options,
  43. .version = LIBAVUTIL_VERSION_INT,
  44. .category = AV_CLASS_CATEGORY_FILTER,
  45. };
  46. AVFilterGraph *avfilter_graph_alloc(void)
  47. {
  48. AVFilterGraph *ret = av_mallocz(sizeof(*ret));
  49. if (!ret)
  50. return NULL;
  51. ret->av_class = &filtergraph_class;
  52. return ret;
  53. }
  54. void ff_filter_graph_remove_filter(AVFilterGraph *graph, AVFilterContext *filter)
  55. {
  56. int i;
  57. for (i = 0; i < graph->nb_filters; i++) {
  58. if (graph->filters[i] == filter) {
  59. FFSWAP(AVFilterContext*, graph->filters[i],
  60. graph->filters[graph->nb_filters - 1]);
  61. graph->nb_filters--;
  62. return;
  63. }
  64. }
  65. }
  66. void avfilter_graph_free(AVFilterGraph **graph)
  67. {
  68. if (!*graph)
  69. return;
  70. while ((*graph)->nb_filters)
  71. avfilter_free((*graph)->filters[0]);
  72. av_freep(&(*graph)->sink_links);
  73. av_freep(&(*graph)->scale_sws_opts);
  74. av_freep(&(*graph)->aresample_swr_opts);
  75. av_freep(&(*graph)->resample_lavr_opts);
  76. av_freep(&(*graph)->filters);
  77. av_freep(graph);
  78. }
  79. #if FF_API_AVFILTER_OPEN
  80. int avfilter_graph_add_filter(AVFilterGraph *graph, AVFilterContext *filter)
  81. {
  82. AVFilterContext **filters = av_realloc(graph->filters,
  83. sizeof(*filters) * (graph->nb_filters + 1));
  84. if (!filters)
  85. return AVERROR(ENOMEM);
  86. graph->filters = filters;
  87. graph->filters[graph->nb_filters++] = filter;
  88. #if FF_API_FOO_COUNT
  89. graph->filter_count_unused = graph->nb_filters;
  90. #endif
  91. filter->graph = graph;
  92. return 0;
  93. }
  94. #endif
  95. int avfilter_graph_create_filter(AVFilterContext **filt_ctx, AVFilter *filt,
  96. const char *name, const char *args, void *opaque,
  97. AVFilterGraph *graph_ctx)
  98. {
  99. int ret;
  100. *filt_ctx = avfilter_graph_alloc_filter(graph_ctx, filt, name);
  101. if (!*filt_ctx)
  102. return AVERROR(ENOMEM);
  103. ret = avfilter_init_str(*filt_ctx, args);
  104. if (ret < 0)
  105. goto fail;
  106. return 0;
  107. fail:
  108. if (*filt_ctx)
  109. avfilter_free(*filt_ctx);
  110. *filt_ctx = NULL;
  111. return ret;
  112. }
  113. void avfilter_graph_set_auto_convert(AVFilterGraph *graph, unsigned flags)
  114. {
  115. graph->disable_auto_convert = flags;
  116. }
  117. AVFilterContext *avfilter_graph_alloc_filter(AVFilterGraph *graph,
  118. const AVFilter *filter,
  119. const char *name)
  120. {
  121. AVFilterContext **filters, *s;
  122. s = ff_filter_alloc(filter, name);
  123. if (!s)
  124. return NULL;
  125. filters = av_realloc(graph->filters, sizeof(*filters) * (graph->nb_filters + 1));
  126. if (!filters) {
  127. avfilter_free(s);
  128. return NULL;
  129. }
  130. graph->filters = filters;
  131. graph->filters[graph->nb_filters++] = s;
  132. #if FF_API_FOO_COUNT
  133. graph->filter_count_unused = graph->nb_filters;
  134. #endif
  135. s->graph = graph;
  136. return s;
  137. }
  138. /**
  139. * Check for the validity of graph.
  140. *
  141. * A graph is considered valid if all its input and output pads are
  142. * connected.
  143. *
  144. * @return 0 in case of success, a negative value otherwise
  145. */
  146. static int graph_check_validity(AVFilterGraph *graph, AVClass *log_ctx)
  147. {
  148. AVFilterContext *filt;
  149. int i, j;
  150. for (i = 0; i < graph->nb_filters; i++) {
  151. const AVFilterPad *pad;
  152. filt = graph->filters[i];
  153. for (j = 0; j < filt->nb_inputs; j++) {
  154. if (!filt->inputs[j] || !filt->inputs[j]->src) {
  155. pad = &filt->input_pads[j];
  156. av_log(log_ctx, AV_LOG_ERROR,
  157. "Input pad \"%s\" with type %s of the filter instance \"%s\" of %s not connected to any source\n",
  158. pad->name, av_get_media_type_string(pad->type), filt->name, filt->filter->name);
  159. return AVERROR(EINVAL);
  160. }
  161. }
  162. for (j = 0; j < filt->nb_outputs; j++) {
  163. if (!filt->outputs[j] || !filt->outputs[j]->dst) {
  164. pad = &filt->output_pads[j];
  165. av_log(log_ctx, AV_LOG_ERROR,
  166. "Output pad \"%s\" with type %s of the filter instance \"%s\" of %s not connected to any destination\n",
  167. pad->name, av_get_media_type_string(pad->type), filt->name, filt->filter->name);
  168. return AVERROR(EINVAL);
  169. }
  170. }
  171. }
  172. return 0;
  173. }
  174. /**
  175. * Configure all the links of graphctx.
  176. *
  177. * @return 0 in case of success, a negative value otherwise
  178. */
  179. static int graph_config_links(AVFilterGraph *graph, AVClass *log_ctx)
  180. {
  181. AVFilterContext *filt;
  182. int i, ret;
  183. for (i = 0; i < graph->nb_filters; i++) {
  184. filt = graph->filters[i];
  185. if (!filt->nb_outputs) {
  186. if ((ret = avfilter_config_links(filt)))
  187. return ret;
  188. }
  189. }
  190. return 0;
  191. }
  192. AVFilterContext *avfilter_graph_get_filter(AVFilterGraph *graph, char *name)
  193. {
  194. int i;
  195. for (i = 0; i < graph->nb_filters; i++)
  196. if (graph->filters[i]->name && !strcmp(name, graph->filters[i]->name))
  197. return graph->filters[i];
  198. return NULL;
  199. }
  200. static void sanitize_channel_layouts(void *log, AVFilterChannelLayouts *l)
  201. {
  202. if (!l)
  203. return;
  204. if (l->nb_channel_layouts) {
  205. if (l->all_layouts || l->all_counts)
  206. av_log(log, AV_LOG_WARNING, "All layouts set on non-empty list\n");
  207. l->all_layouts = l->all_counts = 0;
  208. } else {
  209. if (l->all_counts && !l->all_layouts)
  210. av_log(log, AV_LOG_WARNING, "All counts without all layouts\n");
  211. l->all_layouts = 1;
  212. }
  213. }
  214. static int filter_query_formats(AVFilterContext *ctx)
  215. {
  216. int ret, i;
  217. AVFilterFormats *formats;
  218. AVFilterChannelLayouts *chlayouts;
  219. AVFilterFormats *samplerates;
  220. enum AVMediaType type = ctx->inputs && ctx->inputs [0] ? ctx->inputs [0]->type :
  221. ctx->outputs && ctx->outputs[0] ? ctx->outputs[0]->type :
  222. AVMEDIA_TYPE_VIDEO;
  223. if ((ret = ctx->filter->query_formats(ctx)) < 0) {
  224. if (ret != AVERROR(EAGAIN))
  225. av_log(ctx, AV_LOG_ERROR, "Query format failed for '%s': %s\n",
  226. ctx->name, av_err2str(ret));
  227. return ret;
  228. }
  229. for (i = 0; i < ctx->nb_inputs; i++)
  230. sanitize_channel_layouts(ctx, ctx->inputs[i]->out_channel_layouts);
  231. for (i = 0; i < ctx->nb_outputs; i++)
  232. sanitize_channel_layouts(ctx, ctx->outputs[i]->in_channel_layouts);
  233. formats = ff_all_formats(type);
  234. if (!formats)
  235. return AVERROR(ENOMEM);
  236. ff_set_common_formats(ctx, formats);
  237. if (type == AVMEDIA_TYPE_AUDIO) {
  238. samplerates = ff_all_samplerates();
  239. if (!samplerates)
  240. return AVERROR(ENOMEM);
  241. ff_set_common_samplerates(ctx, samplerates);
  242. chlayouts = ff_all_channel_layouts();
  243. if (!chlayouts)
  244. return AVERROR(ENOMEM);
  245. ff_set_common_channel_layouts(ctx, chlayouts);
  246. }
  247. return 0;
  248. }
  249. static int formats_declared(AVFilterContext *f)
  250. {
  251. int i;
  252. for (i = 0; i < f->nb_inputs; i++) {
  253. if (!f->inputs[i]->out_formats)
  254. return 0;
  255. if (f->inputs[i]->type == AVMEDIA_TYPE_AUDIO &&
  256. !(f->inputs[i]->out_samplerates &&
  257. f->inputs[i]->out_channel_layouts))
  258. return 0;
  259. }
  260. for (i = 0; i < f->nb_outputs; i++) {
  261. if (!f->outputs[i]->in_formats)
  262. return 0;
  263. if (f->outputs[i]->type == AVMEDIA_TYPE_AUDIO &&
  264. !(f->outputs[i]->in_samplerates &&
  265. f->outputs[i]->in_channel_layouts))
  266. return 0;
  267. }
  268. return 1;
  269. }
  270. static int query_formats(AVFilterGraph *graph, AVClass *log_ctx)
  271. {
  272. int i, j, ret;
  273. int scaler_count = 0, resampler_count = 0;
  274. int count_queried = 0, count_merged = 0, count_already_merged = 0,
  275. count_delayed = 0;
  276. for (i = 0; i < graph->nb_filters; i++) {
  277. AVFilterContext *f = graph->filters[i];
  278. if (formats_declared(f))
  279. continue;
  280. if (f->filter->query_formats)
  281. ret = filter_query_formats(f);
  282. else
  283. ret = ff_default_query_formats(f);
  284. if (ret < 0 && ret != AVERROR(EAGAIN))
  285. return ret;
  286. count_queried++;
  287. }
  288. /* go through and merge as many format lists as possible */
  289. for (i = 0; i < graph->nb_filters; i++) {
  290. AVFilterContext *filter = graph->filters[i];
  291. for (j = 0; j < filter->nb_inputs; j++) {
  292. AVFilterLink *link = filter->inputs[j];
  293. int convert_needed = 0;
  294. if (!link)
  295. continue;
  296. #define MERGE_DISPATCH(field, statement) \
  297. if (!(link->in_ ## field && link->out_ ## field)) { \
  298. count_delayed++; \
  299. } else if (link->in_ ## field == link->out_ ## field) { \
  300. count_already_merged++; \
  301. } else { \
  302. count_merged++; \
  303. statement \
  304. }
  305. MERGE_DISPATCH(formats,
  306. if (!ff_merge_formats(link->in_formats, link->out_formats,
  307. link->type))
  308. convert_needed = 1;
  309. )
  310. if (link->type == AVMEDIA_TYPE_AUDIO) {
  311. MERGE_DISPATCH(channel_layouts,
  312. if (!ff_merge_channel_layouts(link->in_channel_layouts,
  313. link->out_channel_layouts))
  314. convert_needed = 1;
  315. )
  316. MERGE_DISPATCH(samplerates,
  317. if (!ff_merge_samplerates(link->in_samplerates,
  318. link->out_samplerates))
  319. convert_needed = 1;
  320. )
  321. }
  322. #undef MERGE_DISPATCH
  323. if (convert_needed) {
  324. AVFilterContext *convert;
  325. AVFilter *filter;
  326. AVFilterLink *inlink, *outlink;
  327. char scale_args[256];
  328. char inst_name[30];
  329. /* couldn't merge format lists. auto-insert conversion filter */
  330. switch (link->type) {
  331. case AVMEDIA_TYPE_VIDEO:
  332. if (!(filter = avfilter_get_by_name("scale"))) {
  333. av_log(log_ctx, AV_LOG_ERROR, "'scale' filter "
  334. "not present, cannot convert pixel formats.\n");
  335. return AVERROR(EINVAL);
  336. }
  337. snprintf(inst_name, sizeof(inst_name), "auto-inserted scaler %d",
  338. scaler_count++);
  339. av_strlcpy(scale_args, "0:0", sizeof(scale_args));
  340. if (graph->scale_sws_opts) {
  341. av_strlcat(scale_args, ":", sizeof(scale_args));
  342. av_strlcat(scale_args, graph->scale_sws_opts, sizeof(scale_args));
  343. }
  344. if ((ret = avfilter_graph_create_filter(&convert, filter,
  345. inst_name, scale_args, NULL,
  346. graph)) < 0)
  347. return ret;
  348. break;
  349. case AVMEDIA_TYPE_AUDIO:
  350. if (!(filter = avfilter_get_by_name("aresample"))) {
  351. av_log(log_ctx, AV_LOG_ERROR, "'aresample' filter "
  352. "not present, cannot convert audio formats.\n");
  353. return AVERROR(EINVAL);
  354. }
  355. snprintf(inst_name, sizeof(inst_name), "auto-inserted resampler %d",
  356. resampler_count++);
  357. scale_args[0] = '\0';
  358. if (graph->aresample_swr_opts)
  359. snprintf(scale_args, sizeof(scale_args), "%s",
  360. graph->aresample_swr_opts);
  361. if ((ret = avfilter_graph_create_filter(&convert, filter,
  362. inst_name, graph->aresample_swr_opts,
  363. NULL, graph)) < 0)
  364. return ret;
  365. break;
  366. default:
  367. return AVERROR(EINVAL);
  368. }
  369. if ((ret = avfilter_insert_filter(link, convert, 0, 0)) < 0)
  370. return ret;
  371. filter_query_formats(convert);
  372. inlink = convert->inputs[0];
  373. outlink = convert->outputs[0];
  374. if (!ff_merge_formats( inlink->in_formats, inlink->out_formats, inlink->type) ||
  375. !ff_merge_formats(outlink->in_formats, outlink->out_formats, outlink->type))
  376. ret |= AVERROR(ENOSYS);
  377. if (inlink->type == AVMEDIA_TYPE_AUDIO &&
  378. (!ff_merge_samplerates(inlink->in_samplerates,
  379. inlink->out_samplerates) ||
  380. !ff_merge_channel_layouts(inlink->in_channel_layouts,
  381. inlink->out_channel_layouts)))
  382. ret |= AVERROR(ENOSYS);
  383. if (outlink->type == AVMEDIA_TYPE_AUDIO &&
  384. (!ff_merge_samplerates(outlink->in_samplerates,
  385. outlink->out_samplerates) ||
  386. !ff_merge_channel_layouts(outlink->in_channel_layouts,
  387. outlink->out_channel_layouts)))
  388. ret |= AVERROR(ENOSYS);
  389. if (ret < 0) {
  390. av_log(log_ctx, AV_LOG_ERROR,
  391. "Impossible to convert between the formats supported by the filter "
  392. "'%s' and the filter '%s'\n", link->src->name, link->dst->name);
  393. return ret;
  394. }
  395. }
  396. }
  397. }
  398. av_log(graph, AV_LOG_DEBUG, "query_formats: "
  399. "%d queried, %d merged, %d already done, %d delayed\n",
  400. count_queried, count_merged, count_already_merged, count_delayed);
  401. if (count_delayed) {
  402. AVBPrint bp;
  403. if (count_queried || count_merged)
  404. return AVERROR(EAGAIN);
  405. av_bprint_init(&bp, 0, AV_BPRINT_SIZE_AUTOMATIC);
  406. for (i = 0; i < graph->nb_filters; i++)
  407. if (!formats_declared(graph->filters[i]))
  408. av_bprintf(&bp, "%s%s", bp.len ? ", " : "",
  409. graph->filters[i]->name);
  410. av_log(graph, AV_LOG_ERROR,
  411. "The following filters could not choose their formats: %s\n"
  412. "Consider inserting the (a)format filter near their input or "
  413. "output.\n", bp.str);
  414. return AVERROR(EIO);
  415. }
  416. return 0;
  417. }
  418. static int pick_format(AVFilterLink *link, AVFilterLink *ref)
  419. {
  420. if (!link || !link->in_formats)
  421. return 0;
  422. if (link->type == AVMEDIA_TYPE_VIDEO) {
  423. if(ref && ref->type == AVMEDIA_TYPE_VIDEO){
  424. int has_alpha= av_pix_fmt_desc_get(ref->format)->nb_components % 2 == 0;
  425. enum AVPixelFormat best= AV_PIX_FMT_NONE;
  426. int i;
  427. for (i=0; i<link->in_formats->format_count; i++) {
  428. enum AVPixelFormat p = link->in_formats->formats[i];
  429. best= avcodec_find_best_pix_fmt_of_2(best, p, ref->format, has_alpha, NULL);
  430. }
  431. av_log(link->src,AV_LOG_DEBUG, "picking %s out of %d ref:%s alpha:%d\n",
  432. av_get_pix_fmt_name(best), link->in_formats->format_count,
  433. av_get_pix_fmt_name(ref->format), has_alpha);
  434. link->in_formats->formats[0] = best;
  435. }
  436. }
  437. link->in_formats->format_count = 1;
  438. link->format = link->in_formats->formats[0];
  439. if (link->type == AVMEDIA_TYPE_AUDIO) {
  440. if (!link->in_samplerates->format_count) {
  441. av_log(link->src, AV_LOG_ERROR, "Cannot select sample rate for"
  442. " the link between filters %s and %s.\n", link->src->name,
  443. link->dst->name);
  444. return AVERROR(EINVAL);
  445. }
  446. link->in_samplerates->format_count = 1;
  447. link->sample_rate = link->in_samplerates->formats[0];
  448. if (link->in_channel_layouts->all_layouts) {
  449. av_log(link->src, AV_LOG_ERROR, "Cannot select channel layout for"
  450. " the link between filters %s and %s.\n", link->src->name,
  451. link->dst->name);
  452. return AVERROR(EINVAL);
  453. }
  454. link->in_channel_layouts->nb_channel_layouts = 1;
  455. link->channel_layout = link->in_channel_layouts->channel_layouts[0];
  456. if ((link->channels = FF_LAYOUT2COUNT(link->channel_layout)))
  457. link->channel_layout = 0;
  458. else
  459. link->channels = av_get_channel_layout_nb_channels(link->channel_layout);
  460. }
  461. ff_formats_unref(&link->in_formats);
  462. ff_formats_unref(&link->out_formats);
  463. ff_formats_unref(&link->in_samplerates);
  464. ff_formats_unref(&link->out_samplerates);
  465. ff_channel_layouts_unref(&link->in_channel_layouts);
  466. ff_channel_layouts_unref(&link->out_channel_layouts);
  467. return 0;
  468. }
  469. #define REDUCE_FORMATS(fmt_type, list_type, list, var, nb, add_format) \
  470. do { \
  471. for (i = 0; i < filter->nb_inputs; i++) { \
  472. AVFilterLink *link = filter->inputs[i]; \
  473. fmt_type fmt; \
  474. \
  475. if (!link->out_ ## list || link->out_ ## list->nb != 1) \
  476. continue; \
  477. fmt = link->out_ ## list->var[0]; \
  478. \
  479. for (j = 0; j < filter->nb_outputs; j++) { \
  480. AVFilterLink *out_link = filter->outputs[j]; \
  481. list_type *fmts; \
  482. \
  483. if (link->type != out_link->type || \
  484. out_link->in_ ## list->nb == 1) \
  485. continue; \
  486. fmts = out_link->in_ ## list; \
  487. \
  488. if (!out_link->in_ ## list->nb) { \
  489. add_format(&out_link->in_ ##list, fmt); \
  490. break; \
  491. } \
  492. \
  493. for (k = 0; k < out_link->in_ ## list->nb; k++) \
  494. if (fmts->var[k] == fmt) { \
  495. fmts->var[0] = fmt; \
  496. fmts->nb = 1; \
  497. ret = 1; \
  498. break; \
  499. } \
  500. } \
  501. } \
  502. } while (0)
  503. static int reduce_formats_on_filter(AVFilterContext *filter)
  504. {
  505. int i, j, k, ret = 0;
  506. REDUCE_FORMATS(int, AVFilterFormats, formats, formats,
  507. format_count, ff_add_format);
  508. REDUCE_FORMATS(int, AVFilterFormats, samplerates, formats,
  509. format_count, ff_add_format);
  510. /* reduce channel layouts */
  511. for (i = 0; i < filter->nb_inputs; i++) {
  512. AVFilterLink *inlink = filter->inputs[i];
  513. uint64_t fmt;
  514. if (!inlink->out_channel_layouts ||
  515. inlink->out_channel_layouts->nb_channel_layouts != 1)
  516. continue;
  517. fmt = inlink->out_channel_layouts->channel_layouts[0];
  518. for (j = 0; j < filter->nb_outputs; j++) {
  519. AVFilterLink *outlink = filter->outputs[j];
  520. AVFilterChannelLayouts *fmts;
  521. fmts = outlink->in_channel_layouts;
  522. if (inlink->type != outlink->type || fmts->nb_channel_layouts == 1)
  523. continue;
  524. if (fmts->all_layouts) {
  525. /* Turn the infinite list into a singleton */
  526. fmts->all_layouts = fmts->all_counts = 0;
  527. ff_add_channel_layout(&outlink->in_channel_layouts, fmt);
  528. break;
  529. }
  530. for (k = 0; k < outlink->in_channel_layouts->nb_channel_layouts; k++) {
  531. if (fmts->channel_layouts[k] == fmt) {
  532. fmts->channel_layouts[0] = fmt;
  533. fmts->nb_channel_layouts = 1;
  534. ret = 1;
  535. break;
  536. }
  537. }
  538. }
  539. }
  540. return ret;
  541. }
  542. static void reduce_formats(AVFilterGraph *graph)
  543. {
  544. int i, reduced;
  545. do {
  546. reduced = 0;
  547. for (i = 0; i < graph->nb_filters; i++)
  548. reduced |= reduce_formats_on_filter(graph->filters[i]);
  549. } while (reduced);
  550. }
  551. static void swap_samplerates_on_filter(AVFilterContext *filter)
  552. {
  553. AVFilterLink *link = NULL;
  554. int sample_rate;
  555. int i, j;
  556. for (i = 0; i < filter->nb_inputs; i++) {
  557. link = filter->inputs[i];
  558. if (link->type == AVMEDIA_TYPE_AUDIO &&
  559. link->out_samplerates->format_count == 1)
  560. break;
  561. }
  562. if (i == filter->nb_inputs)
  563. return;
  564. sample_rate = link->out_samplerates->formats[0];
  565. for (i = 0; i < filter->nb_outputs; i++) {
  566. AVFilterLink *outlink = filter->outputs[i];
  567. int best_idx, best_diff = INT_MAX;
  568. if (outlink->type != AVMEDIA_TYPE_AUDIO ||
  569. outlink->in_samplerates->format_count < 2)
  570. continue;
  571. for (j = 0; j < outlink->in_samplerates->format_count; j++) {
  572. int diff = abs(sample_rate - outlink->in_samplerates->formats[j]);
  573. if (diff < best_diff) {
  574. best_diff = diff;
  575. best_idx = j;
  576. }
  577. }
  578. FFSWAP(int, outlink->in_samplerates->formats[0],
  579. outlink->in_samplerates->formats[best_idx]);
  580. }
  581. }
  582. static void swap_samplerates(AVFilterGraph *graph)
  583. {
  584. int i;
  585. for (i = 0; i < graph->nb_filters; i++)
  586. swap_samplerates_on_filter(graph->filters[i]);
  587. }
  588. #define CH_CENTER_PAIR (AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_RIGHT_OF_CENTER)
  589. #define CH_FRONT_PAIR (AV_CH_FRONT_LEFT | AV_CH_FRONT_RIGHT)
  590. #define CH_STEREO_PAIR (AV_CH_STEREO_LEFT | AV_CH_STEREO_RIGHT)
  591. #define CH_WIDE_PAIR (AV_CH_WIDE_LEFT | AV_CH_WIDE_RIGHT)
  592. #define CH_SIDE_PAIR (AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT)
  593. #define CH_DIRECT_PAIR (AV_CH_SURROUND_DIRECT_LEFT | AV_CH_SURROUND_DIRECT_RIGHT)
  594. #define CH_BACK_PAIR (AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT)
  595. /* allowable substitutions for channel pairs when comparing layouts,
  596. * ordered by priority for both values */
  597. static const uint64_t ch_subst[][2] = {
  598. { CH_FRONT_PAIR, CH_CENTER_PAIR },
  599. { CH_FRONT_PAIR, CH_WIDE_PAIR },
  600. { CH_FRONT_PAIR, AV_CH_FRONT_CENTER },
  601. { CH_CENTER_PAIR, CH_FRONT_PAIR },
  602. { CH_CENTER_PAIR, CH_WIDE_PAIR },
  603. { CH_CENTER_PAIR, AV_CH_FRONT_CENTER },
  604. { CH_WIDE_PAIR, CH_FRONT_PAIR },
  605. { CH_WIDE_PAIR, CH_CENTER_PAIR },
  606. { CH_WIDE_PAIR, AV_CH_FRONT_CENTER },
  607. { AV_CH_FRONT_CENTER, CH_FRONT_PAIR },
  608. { AV_CH_FRONT_CENTER, CH_CENTER_PAIR },
  609. { AV_CH_FRONT_CENTER, CH_WIDE_PAIR },
  610. { CH_SIDE_PAIR, CH_DIRECT_PAIR },
  611. { CH_SIDE_PAIR, CH_BACK_PAIR },
  612. { CH_SIDE_PAIR, AV_CH_BACK_CENTER },
  613. { CH_BACK_PAIR, CH_DIRECT_PAIR },
  614. { CH_BACK_PAIR, CH_SIDE_PAIR },
  615. { CH_BACK_PAIR, AV_CH_BACK_CENTER },
  616. { AV_CH_BACK_CENTER, CH_BACK_PAIR },
  617. { AV_CH_BACK_CENTER, CH_DIRECT_PAIR },
  618. { AV_CH_BACK_CENTER, CH_SIDE_PAIR },
  619. };
  620. static void swap_channel_layouts_on_filter(AVFilterContext *filter)
  621. {
  622. AVFilterLink *link = NULL;
  623. int i, j, k;
  624. for (i = 0; i < filter->nb_inputs; i++) {
  625. link = filter->inputs[i];
  626. if (link->type == AVMEDIA_TYPE_AUDIO &&
  627. link->out_channel_layouts->nb_channel_layouts == 1)
  628. break;
  629. }
  630. if (i == filter->nb_inputs)
  631. return;
  632. for (i = 0; i < filter->nb_outputs; i++) {
  633. AVFilterLink *outlink = filter->outputs[i];
  634. int best_idx = -1, best_score = INT_MIN, best_count_diff = INT_MAX;
  635. if (outlink->type != AVMEDIA_TYPE_AUDIO ||
  636. outlink->in_channel_layouts->nb_channel_layouts < 2)
  637. continue;
  638. for (j = 0; j < outlink->in_channel_layouts->nb_channel_layouts; j++) {
  639. uint64_t in_chlayout = link->out_channel_layouts->channel_layouts[0];
  640. uint64_t out_chlayout = outlink->in_channel_layouts->channel_layouts[j];
  641. int in_channels = av_get_channel_layout_nb_channels(in_chlayout);
  642. int out_channels = av_get_channel_layout_nb_channels(out_chlayout);
  643. int count_diff = out_channels - in_channels;
  644. int matched_channels, extra_channels;
  645. int score = 100000;
  646. if (FF_LAYOUT2COUNT(in_chlayout) || FF_LAYOUT2COUNT(out_chlayout)) {
  647. /* Compute score in case the input or output layout encodes
  648. a channel count; in this case the score is not altered by
  649. the computation afterwards, as in_chlayout and
  650. out_chlayout have both been set to 0 */
  651. if (FF_LAYOUT2COUNT(in_chlayout))
  652. in_channels = FF_LAYOUT2COUNT(in_chlayout);
  653. if (FF_LAYOUT2COUNT(out_chlayout))
  654. out_channels = FF_LAYOUT2COUNT(out_chlayout);
  655. score -= 10000 + FFABS(out_channels - in_channels) +
  656. (in_channels > out_channels ? 10000 : 0);
  657. in_chlayout = out_chlayout = 0;
  658. /* Let the remaining computation run, even if the score
  659. value is not altered */
  660. }
  661. /* channel substitution */
  662. for (k = 0; k < FF_ARRAY_ELEMS(ch_subst); k++) {
  663. uint64_t cmp0 = ch_subst[k][0];
  664. uint64_t cmp1 = ch_subst[k][1];
  665. if (( in_chlayout & cmp0) && (!(out_chlayout & cmp0)) &&
  666. (out_chlayout & cmp1) && (!( in_chlayout & cmp1))) {
  667. in_chlayout &= ~cmp0;
  668. out_chlayout &= ~cmp1;
  669. /* add score for channel match, minus a deduction for
  670. having to do the substitution */
  671. score += 10 * av_get_channel_layout_nb_channels(cmp1) - 2;
  672. }
  673. }
  674. /* no penalty for LFE channel mismatch */
  675. if ( (in_chlayout & AV_CH_LOW_FREQUENCY) &&
  676. (out_chlayout & AV_CH_LOW_FREQUENCY))
  677. score += 10;
  678. in_chlayout &= ~AV_CH_LOW_FREQUENCY;
  679. out_chlayout &= ~AV_CH_LOW_FREQUENCY;
  680. matched_channels = av_get_channel_layout_nb_channels(in_chlayout &
  681. out_chlayout);
  682. extra_channels = av_get_channel_layout_nb_channels(out_chlayout &
  683. (~in_chlayout));
  684. score += 10 * matched_channels - 5 * extra_channels;
  685. if (score > best_score ||
  686. (count_diff < best_count_diff && score == best_score)) {
  687. best_score = score;
  688. best_idx = j;
  689. best_count_diff = count_diff;
  690. }
  691. }
  692. av_assert0(best_idx >= 0);
  693. FFSWAP(uint64_t, outlink->in_channel_layouts->channel_layouts[0],
  694. outlink->in_channel_layouts->channel_layouts[best_idx]);
  695. }
  696. }
  697. static void swap_channel_layouts(AVFilterGraph *graph)
  698. {
  699. int i;
  700. for (i = 0; i < graph->nb_filters; i++)
  701. swap_channel_layouts_on_filter(graph->filters[i]);
  702. }
  703. static void swap_sample_fmts_on_filter(AVFilterContext *filter)
  704. {
  705. AVFilterLink *link = NULL;
  706. int format, bps;
  707. int i, j;
  708. for (i = 0; i < filter->nb_inputs; i++) {
  709. link = filter->inputs[i];
  710. if (link->type == AVMEDIA_TYPE_AUDIO &&
  711. link->out_formats->format_count == 1)
  712. break;
  713. }
  714. if (i == filter->nb_inputs)
  715. return;
  716. format = link->out_formats->formats[0];
  717. bps = av_get_bytes_per_sample(format);
  718. for (i = 0; i < filter->nb_outputs; i++) {
  719. AVFilterLink *outlink = filter->outputs[i];
  720. int best_idx = -1, best_score = INT_MIN;
  721. if (outlink->type != AVMEDIA_TYPE_AUDIO ||
  722. outlink->in_formats->format_count < 2)
  723. continue;
  724. for (j = 0; j < outlink->in_formats->format_count; j++) {
  725. int out_format = outlink->in_formats->formats[j];
  726. int out_bps = av_get_bytes_per_sample(out_format);
  727. int score;
  728. if (av_get_packed_sample_fmt(out_format) == format ||
  729. av_get_planar_sample_fmt(out_format) == format) {
  730. best_idx = j;
  731. break;
  732. }
  733. /* for s32 and float prefer double to prevent loss of information */
  734. if (bps == 4 && out_bps == 8) {
  735. best_idx = j;
  736. break;
  737. }
  738. /* prefer closest higher or equal bps */
  739. score = -abs(out_bps - bps);
  740. if (out_bps >= bps)
  741. score += INT_MAX/2;
  742. if (score > best_score) {
  743. best_score = score;
  744. best_idx = j;
  745. }
  746. }
  747. av_assert0(best_idx >= 0);
  748. FFSWAP(int, outlink->in_formats->formats[0],
  749. outlink->in_formats->formats[best_idx]);
  750. }
  751. }
  752. static void swap_sample_fmts(AVFilterGraph *graph)
  753. {
  754. int i;
  755. for (i = 0; i < graph->nb_filters; i++)
  756. swap_sample_fmts_on_filter(graph->filters[i]);
  757. }
  758. static int pick_formats(AVFilterGraph *graph)
  759. {
  760. int i, j, ret;
  761. int change;
  762. do{
  763. change = 0;
  764. for (i = 0; i < graph->nb_filters; i++) {
  765. AVFilterContext *filter = graph->filters[i];
  766. if (filter->nb_inputs){
  767. for (j = 0; j < filter->nb_inputs; j++){
  768. if(filter->inputs[j]->in_formats && filter->inputs[j]->in_formats->format_count == 1) {
  769. if ((ret = pick_format(filter->inputs[j], NULL)) < 0)
  770. return ret;
  771. change = 1;
  772. }
  773. }
  774. }
  775. if (filter->nb_outputs){
  776. for (j = 0; j < filter->nb_outputs; j++){
  777. if(filter->outputs[j]->in_formats && filter->outputs[j]->in_formats->format_count == 1) {
  778. if ((ret = pick_format(filter->outputs[j], NULL)) < 0)
  779. return ret;
  780. change = 1;
  781. }
  782. }
  783. }
  784. if (filter->nb_inputs && filter->nb_outputs && filter->inputs[0]->format>=0) {
  785. for (j = 0; j < filter->nb_outputs; j++) {
  786. if(filter->outputs[j]->format<0) {
  787. if ((ret = pick_format(filter->outputs[j], filter->inputs[0])) < 0)
  788. return ret;
  789. change = 1;
  790. }
  791. }
  792. }
  793. }
  794. }while(change);
  795. for (i = 0; i < graph->nb_filters; i++) {
  796. AVFilterContext *filter = graph->filters[i];
  797. for (j = 0; j < filter->nb_inputs; j++)
  798. if ((ret = pick_format(filter->inputs[j], NULL)) < 0)
  799. return ret;
  800. for (j = 0; j < filter->nb_outputs; j++)
  801. if ((ret = pick_format(filter->outputs[j], NULL)) < 0)
  802. return ret;
  803. }
  804. return 0;
  805. }
  806. /**
  807. * Configure the formats of all the links in the graph.
  808. */
  809. static int graph_config_formats(AVFilterGraph *graph, AVClass *log_ctx)
  810. {
  811. int ret;
  812. /* find supported formats from sub-filters, and merge along links */
  813. while ((ret = query_formats(graph, log_ctx)) == AVERROR(EAGAIN))
  814. av_log(graph, AV_LOG_DEBUG, "query_formats not finished\n");
  815. if (ret < 0)
  816. return ret;
  817. /* Once everything is merged, it's possible that we'll still have
  818. * multiple valid media format choices. We try to minimize the amount
  819. * of format conversion inside filters */
  820. reduce_formats(graph);
  821. /* for audio filters, ensure the best format, sample rate and channel layout
  822. * is selected */
  823. swap_sample_fmts(graph);
  824. swap_samplerates(graph);
  825. swap_channel_layouts(graph);
  826. if ((ret = pick_formats(graph)) < 0)
  827. return ret;
  828. return 0;
  829. }
  830. static int ff_avfilter_graph_config_pointers(AVFilterGraph *graph,
  831. AVClass *log_ctx)
  832. {
  833. unsigned i, j;
  834. int sink_links_count = 0, n = 0;
  835. AVFilterContext *f;
  836. AVFilterLink **sinks;
  837. for (i = 0; i < graph->nb_filters; i++) {
  838. f = graph->filters[i];
  839. for (j = 0; j < f->nb_inputs; j++) {
  840. f->inputs[j]->graph = graph;
  841. f->inputs[j]->age_index = -1;
  842. }
  843. for (j = 0; j < f->nb_outputs; j++) {
  844. f->outputs[j]->graph = graph;
  845. f->outputs[j]->age_index= -1;
  846. }
  847. if (!f->nb_outputs) {
  848. if (f->nb_inputs > INT_MAX - sink_links_count)
  849. return AVERROR(EINVAL);
  850. sink_links_count += f->nb_inputs;
  851. }
  852. }
  853. sinks = av_calloc(sink_links_count, sizeof(*sinks));
  854. if (!sinks)
  855. return AVERROR(ENOMEM);
  856. for (i = 0; i < graph->nb_filters; i++) {
  857. f = graph->filters[i];
  858. if (!f->nb_outputs) {
  859. for (j = 0; j < f->nb_inputs; j++) {
  860. sinks[n] = f->inputs[j];
  861. f->inputs[j]->age_index = n++;
  862. }
  863. }
  864. }
  865. av_assert0(n == sink_links_count);
  866. graph->sink_links = sinks;
  867. graph->sink_links_count = sink_links_count;
  868. return 0;
  869. }
  870. static int graph_insert_fifos(AVFilterGraph *graph, AVClass *log_ctx)
  871. {
  872. AVFilterContext *f;
  873. int i, j, ret;
  874. int fifo_count = 0;
  875. for (i = 0; i < graph->nb_filters; i++) {
  876. f = graph->filters[i];
  877. for (j = 0; j < f->nb_inputs; j++) {
  878. AVFilterLink *link = f->inputs[j];
  879. AVFilterContext *fifo_ctx;
  880. AVFilter *fifo;
  881. char name[32];
  882. if (!link->dstpad->needs_fifo)
  883. continue;
  884. fifo = f->inputs[j]->type == AVMEDIA_TYPE_VIDEO ?
  885. avfilter_get_by_name("fifo") :
  886. avfilter_get_by_name("afifo");
  887. snprintf(name, sizeof(name), "auto-inserted fifo %d", fifo_count++);
  888. ret = avfilter_graph_create_filter(&fifo_ctx, fifo, name, NULL,
  889. NULL, graph);
  890. if (ret < 0)
  891. return ret;
  892. ret = avfilter_insert_filter(link, fifo_ctx, 0, 0);
  893. if (ret < 0)
  894. return ret;
  895. }
  896. }
  897. return 0;
  898. }
  899. int avfilter_graph_config(AVFilterGraph *graphctx, void *log_ctx)
  900. {
  901. int ret;
  902. if ((ret = graph_check_validity(graphctx, log_ctx)))
  903. return ret;
  904. if ((ret = graph_insert_fifos(graphctx, log_ctx)) < 0)
  905. return ret;
  906. if ((ret = graph_config_formats(graphctx, log_ctx)))
  907. return ret;
  908. if ((ret = graph_config_links(graphctx, log_ctx)))
  909. return ret;
  910. if ((ret = ff_avfilter_graph_config_pointers(graphctx, log_ctx)))
  911. return ret;
  912. return 0;
  913. }
  914. int avfilter_graph_send_command(AVFilterGraph *graph, const char *target, const char *cmd, const char *arg, char *res, int res_len, int flags)
  915. {
  916. int i, r = AVERROR(ENOSYS);
  917. if(!graph)
  918. return r;
  919. if((flags & AVFILTER_CMD_FLAG_ONE) && !(flags & AVFILTER_CMD_FLAG_FAST)) {
  920. r=avfilter_graph_send_command(graph, target, cmd, arg, res, res_len, flags | AVFILTER_CMD_FLAG_FAST);
  921. if(r != AVERROR(ENOSYS))
  922. return r;
  923. }
  924. if(res_len && res)
  925. res[0]= 0;
  926. for (i = 0; i < graph->nb_filters; i++) {
  927. AVFilterContext *filter = graph->filters[i];
  928. if(!strcmp(target, "all") || (filter->name && !strcmp(target, filter->name)) || !strcmp(target, filter->filter->name)){
  929. r = avfilter_process_command(filter, cmd, arg, res, res_len, flags);
  930. if(r != AVERROR(ENOSYS)) {
  931. if((flags & AVFILTER_CMD_FLAG_ONE) || r<0)
  932. return r;
  933. }
  934. }
  935. }
  936. return r;
  937. }
  938. int avfilter_graph_queue_command(AVFilterGraph *graph, const char *target, const char *command, const char *arg, int flags, double ts)
  939. {
  940. int i;
  941. if(!graph)
  942. return 0;
  943. for (i = 0; i < graph->nb_filters; i++) {
  944. AVFilterContext *filter = graph->filters[i];
  945. if(filter && (!strcmp(target, "all") || !strcmp(target, filter->name) || !strcmp(target, filter->filter->name))){
  946. AVFilterCommand **queue = &filter->command_queue, *next;
  947. while (*queue && (*queue)->time <= ts)
  948. queue = &(*queue)->next;
  949. next = *queue;
  950. *queue = av_mallocz(sizeof(AVFilterCommand));
  951. (*queue)->command = av_strdup(command);
  952. (*queue)->arg = av_strdup(arg);
  953. (*queue)->time = ts;
  954. (*queue)->flags = flags;
  955. (*queue)->next = next;
  956. if(flags & AVFILTER_CMD_FLAG_ONE)
  957. return 0;
  958. }
  959. }
  960. return 0;
  961. }
  962. static void heap_bubble_up(AVFilterGraph *graph,
  963. AVFilterLink *link, int index)
  964. {
  965. AVFilterLink **links = graph->sink_links;
  966. while (index) {
  967. int parent = (index - 1) >> 1;
  968. if (links[parent]->current_pts >= link->current_pts)
  969. break;
  970. links[index] = links[parent];
  971. links[index]->age_index = index;
  972. index = parent;
  973. }
  974. links[index] = link;
  975. link->age_index = index;
  976. }
  977. static void heap_bubble_down(AVFilterGraph *graph,
  978. AVFilterLink *link, int index)
  979. {
  980. AVFilterLink **links = graph->sink_links;
  981. while (1) {
  982. int child = 2 * index + 1;
  983. if (child >= graph->sink_links_count)
  984. break;
  985. if (child + 1 < graph->sink_links_count &&
  986. links[child + 1]->current_pts < links[child]->current_pts)
  987. child++;
  988. if (link->current_pts < links[child]->current_pts)
  989. break;
  990. links[index] = links[child];
  991. links[index]->age_index = index;
  992. index = child;
  993. }
  994. links[index] = link;
  995. link->age_index = index;
  996. }
  997. void ff_avfilter_graph_update_heap(AVFilterGraph *graph, AVFilterLink *link)
  998. {
  999. heap_bubble_up (graph, link, link->age_index);
  1000. heap_bubble_down(graph, link, link->age_index);
  1001. }
  1002. int avfilter_graph_request_oldest(AVFilterGraph *graph)
  1003. {
  1004. while (graph->sink_links_count) {
  1005. AVFilterLink *oldest = graph->sink_links[0];
  1006. int r = ff_request_frame(oldest);
  1007. if (r != AVERROR_EOF)
  1008. return r;
  1009. av_log(oldest->dst, AV_LOG_DEBUG, "EOF on sink link %s:%s.\n",
  1010. oldest->dst ? oldest->dst->name : "unknown",
  1011. oldest->dstpad ? oldest->dstpad->name : "unknown");
  1012. /* EOF: remove the link from the heap */
  1013. if (oldest->age_index < --graph->sink_links_count)
  1014. heap_bubble_down(graph, graph->sink_links[graph->sink_links_count],
  1015. oldest->age_index);
  1016. oldest->age_index = -1;
  1017. }
  1018. return AVERROR_EOF;
  1019. }