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.

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