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.

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