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.

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