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.

1069 lines
38KB

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