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.

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