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.

1062 lines
37KB

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