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.

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