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.

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