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.

1132 lines
40KB

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