You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1075 lines
38KB

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