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.

603 lines
21KB

  1. /*
  2. * filter graphs
  3. * Copyright (c) 2008 Vitor Sessak
  4. * Copyright (c) 2007 Bobby Bingham
  5. *
  6. * This file is part of Libav.
  7. *
  8. * Libav 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. * Libav 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 Libav; 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 "avfilter.h"
  25. #include "avfiltergraph.h"
  26. #include "formats.h"
  27. #include "internal.h"
  28. #include "libavutil/audioconvert.h"
  29. #include "libavutil/log.h"
  30. static const AVClass filtergraph_class = {
  31. .class_name = "AVFilterGraph",
  32. .item_name = av_default_item_name,
  33. .version = LIBAVUTIL_VERSION_INT,
  34. };
  35. AVFilterGraph *avfilter_graph_alloc(void)
  36. {
  37. AVFilterGraph *ret = av_mallocz(sizeof(AVFilterGraph));
  38. if (!ret)
  39. return NULL;
  40. #if FF_API_GRAPH_AVCLASS
  41. ret->av_class = &filtergraph_class;
  42. #endif
  43. return ret;
  44. }
  45. void avfilter_graph_free(AVFilterGraph **graph)
  46. {
  47. if (!*graph)
  48. return;
  49. for (; (*graph)->filter_count > 0; (*graph)->filter_count--)
  50. avfilter_free((*graph)->filters[(*graph)->filter_count - 1]);
  51. av_freep(&(*graph)->scale_sws_opts);
  52. av_freep(&(*graph)->filters);
  53. av_freep(graph);
  54. }
  55. int avfilter_graph_add_filter(AVFilterGraph *graph, AVFilterContext *filter)
  56. {
  57. AVFilterContext **filters = av_realloc(graph->filters,
  58. sizeof(AVFilterContext*) * (graph->filter_count+1));
  59. if (!filters)
  60. return AVERROR(ENOMEM);
  61. graph->filters = filters;
  62. graph->filters[graph->filter_count++] = filter;
  63. return 0;
  64. }
  65. int avfilter_graph_create_filter(AVFilterContext **filt_ctx, AVFilter *filt,
  66. const char *name, const char *args, void *opaque,
  67. AVFilterGraph *graph_ctx)
  68. {
  69. int ret;
  70. if ((ret = avfilter_open(filt_ctx, filt, name)) < 0)
  71. goto fail;
  72. if ((ret = avfilter_init_filter(*filt_ctx, args, opaque)) < 0)
  73. goto fail;
  74. if ((ret = avfilter_graph_add_filter(graph_ctx, *filt_ctx)) < 0)
  75. goto fail;
  76. return 0;
  77. fail:
  78. if (*filt_ctx)
  79. avfilter_free(*filt_ctx);
  80. *filt_ctx = NULL;
  81. return ret;
  82. }
  83. /**
  84. * Check for the validity of graph.
  85. *
  86. * A graph is considered valid if all its input and output pads are
  87. * connected.
  88. *
  89. * @return 0 in case of success, a negative value otherwise
  90. */
  91. static int graph_check_validity(AVFilterGraph *graph, AVClass *log_ctx)
  92. {
  93. AVFilterContext *filt;
  94. int i, j;
  95. for (i = 0; i < graph->filter_count; i++) {
  96. filt = graph->filters[i];
  97. for (j = 0; j < filt->input_count; j++) {
  98. if (!filt->inputs[j] || !filt->inputs[j]->src) {
  99. av_log(log_ctx, AV_LOG_ERROR,
  100. "Input pad \"%s\" for the filter \"%s\" of type \"%s\" not connected to any source\n",
  101. filt->input_pads[j].name, filt->name, filt->filter->name);
  102. return AVERROR(EINVAL);
  103. }
  104. }
  105. for (j = 0; j < filt->output_count; j++) {
  106. if (!filt->outputs[j] || !filt->outputs[j]->dst) {
  107. av_log(log_ctx, AV_LOG_ERROR,
  108. "Output pad \"%s\" for the filter \"%s\" of type \"%s\" not connected to any destination\n",
  109. filt->output_pads[j].name, filt->name, filt->filter->name);
  110. return AVERROR(EINVAL);
  111. }
  112. }
  113. }
  114. return 0;
  115. }
  116. /**
  117. * Configure all the links of graphctx.
  118. *
  119. * @return 0 in case of success, a negative value otherwise
  120. */
  121. static int graph_config_links(AVFilterGraph *graph, AVClass *log_ctx)
  122. {
  123. AVFilterContext *filt;
  124. int i, ret;
  125. for (i=0; i < graph->filter_count; i++) {
  126. filt = graph->filters[i];
  127. if (!filt->output_count) {
  128. if ((ret = avfilter_config_links(filt)))
  129. return ret;
  130. }
  131. }
  132. return 0;
  133. }
  134. AVFilterContext *avfilter_graph_get_filter(AVFilterGraph *graph, char *name)
  135. {
  136. int i;
  137. for (i = 0; i < graph->filter_count; i++)
  138. if (graph->filters[i]->name && !strcmp(name, graph->filters[i]->name))
  139. return graph->filters[i];
  140. return NULL;
  141. }
  142. static int query_formats(AVFilterGraph *graph, AVClass *log_ctx)
  143. {
  144. int i, j, ret;
  145. int scaler_count = 0, resampler_count = 0;
  146. /* ask all the sub-filters for their supported media formats */
  147. for (i = 0; i < graph->filter_count; i++) {
  148. if (graph->filters[i]->filter->query_formats)
  149. graph->filters[i]->filter->query_formats(graph->filters[i]);
  150. else
  151. ff_default_query_formats(graph->filters[i]);
  152. }
  153. /* go through and merge as many format lists as possible */
  154. for (i = 0; i < graph->filter_count; i++) {
  155. AVFilterContext *filter = graph->filters[i];
  156. for (j = 0; j < filter->input_count; j++) {
  157. AVFilterLink *link = filter->inputs[j];
  158. int convert_needed = 0;
  159. if (!link)
  160. continue;
  161. if (link->in_formats != link->out_formats &&
  162. !ff_merge_formats(link->in_formats,
  163. link->out_formats))
  164. convert_needed = 1;
  165. if (link->type == AVMEDIA_TYPE_AUDIO) {
  166. if (link->in_channel_layouts != link->out_channel_layouts &&
  167. !ff_merge_channel_layouts(link->in_channel_layouts,
  168. link->out_channel_layouts))
  169. convert_needed = 1;
  170. if (link->in_samplerates != link->out_samplerates &&
  171. !ff_merge_samplerates(link->in_samplerates,
  172. link->out_samplerates))
  173. convert_needed = 1;
  174. }
  175. if (convert_needed) {
  176. AVFilterContext *convert;
  177. AVFilter *filter;
  178. AVFilterLink *inlink, *outlink;
  179. char scale_args[256];
  180. char inst_name[30];
  181. /* couldn't merge format lists. auto-insert conversion filter */
  182. switch (link->type) {
  183. case AVMEDIA_TYPE_VIDEO:
  184. snprintf(inst_name, sizeof(inst_name), "auto-inserted scaler %d",
  185. scaler_count++);
  186. snprintf(scale_args, sizeof(scale_args), "0:0:%s", graph->scale_sws_opts);
  187. if ((ret = avfilter_graph_create_filter(&convert,
  188. avfilter_get_by_name("scale"),
  189. inst_name, scale_args, NULL,
  190. graph)) < 0)
  191. return ret;
  192. break;
  193. case AVMEDIA_TYPE_AUDIO:
  194. if (!(filter = avfilter_get_by_name("resample"))) {
  195. av_log(log_ctx, AV_LOG_ERROR, "'resample' filter "
  196. "not present, cannot convert audio formats.\n");
  197. return AVERROR(EINVAL);
  198. }
  199. snprintf(inst_name, sizeof(inst_name), "auto-inserted resampler %d",
  200. resampler_count++);
  201. if ((ret = avfilter_graph_create_filter(&convert,
  202. avfilter_get_by_name("resample"),
  203. inst_name, NULL, NULL, graph)) < 0)
  204. return ret;
  205. break;
  206. default:
  207. return AVERROR(EINVAL);
  208. }
  209. if ((ret = avfilter_insert_filter(link, convert, 0, 0)) < 0)
  210. return ret;
  211. convert->filter->query_formats(convert);
  212. inlink = convert->inputs[0];
  213. outlink = convert->outputs[0];
  214. if (!ff_merge_formats( inlink->in_formats, inlink->out_formats) ||
  215. !ff_merge_formats(outlink->in_formats, outlink->out_formats))
  216. ret |= AVERROR(ENOSYS);
  217. if (inlink->type == AVMEDIA_TYPE_AUDIO &&
  218. (!ff_merge_samplerates(inlink->in_samplerates,
  219. inlink->out_samplerates) ||
  220. !ff_merge_channel_layouts(inlink->in_channel_layouts,
  221. inlink->out_channel_layouts)))
  222. ret |= AVERROR(ENOSYS);
  223. if (outlink->type == AVMEDIA_TYPE_AUDIO &&
  224. (!ff_merge_samplerates(outlink->in_samplerates,
  225. outlink->out_samplerates) ||
  226. !ff_merge_channel_layouts(outlink->in_channel_layouts,
  227. outlink->out_channel_layouts)))
  228. ret |= AVERROR(ENOSYS);
  229. if (ret < 0) {
  230. av_log(log_ctx, AV_LOG_ERROR,
  231. "Impossible to convert between the formats supported by the filter "
  232. "'%s' and the filter '%s'\n", link->src->name, link->dst->name);
  233. return ret;
  234. }
  235. }
  236. }
  237. }
  238. return 0;
  239. }
  240. static int pick_format(AVFilterLink *link)
  241. {
  242. if (!link || !link->in_formats)
  243. return 0;
  244. link->in_formats->format_count = 1;
  245. link->format = link->in_formats->formats[0];
  246. if (link->type == AVMEDIA_TYPE_AUDIO) {
  247. if (!link->in_samplerates->format_count) {
  248. av_log(link->src, AV_LOG_ERROR, "Cannot select sample rate for"
  249. " the link between filters %s and %s.\n", link->src->name,
  250. link->dst->name);
  251. return AVERROR(EINVAL);
  252. }
  253. link->in_samplerates->format_count = 1;
  254. link->sample_rate = link->in_samplerates->formats[0];
  255. if (!link->in_channel_layouts->nb_channel_layouts) {
  256. av_log(link->src, AV_LOG_ERROR, "Cannot select channel layout for"
  257. "the link between filters %s and %s.\n", link->src->name,
  258. link->dst->name);
  259. return AVERROR(EINVAL);
  260. }
  261. link->in_channel_layouts->nb_channel_layouts = 1;
  262. link->channel_layout = link->in_channel_layouts->channel_layouts[0];
  263. }
  264. ff_formats_unref(&link->in_formats);
  265. ff_formats_unref(&link->out_formats);
  266. ff_formats_unref(&link->in_samplerates);
  267. ff_formats_unref(&link->out_samplerates);
  268. ff_channel_layouts_unref(&link->in_channel_layouts);
  269. ff_channel_layouts_unref(&link->out_channel_layouts);
  270. return 0;
  271. }
  272. #define REDUCE_FORMATS(fmt_type, list_type, list, var, nb, add_format) \
  273. do { \
  274. for (i = 0; i < filter->input_count; i++) { \
  275. AVFilterLink *link = filter->inputs[i]; \
  276. fmt_type fmt; \
  277. \
  278. if (!link->out_ ## list || link->out_ ## list->nb != 1) \
  279. continue; \
  280. fmt = link->out_ ## list->var[0]; \
  281. \
  282. for (j = 0; j < filter->output_count; j++) { \
  283. AVFilterLink *out_link = filter->outputs[j]; \
  284. list_type *fmts; \
  285. \
  286. if (link->type != out_link->type || \
  287. out_link->in_ ## list->nb == 1) \
  288. continue; \
  289. fmts = out_link->in_ ## list; \
  290. \
  291. if (!out_link->in_ ## list->nb) { \
  292. add_format(&out_link->in_ ##list, fmt); \
  293. break; \
  294. } \
  295. \
  296. for (k = 0; k < out_link->in_ ## list->nb; k++) \
  297. if (fmts->var[k] == fmt) { \
  298. fmts->var[0] = fmt; \
  299. fmts->nb = 1; \
  300. ret = 1; \
  301. break; \
  302. } \
  303. } \
  304. } \
  305. } while (0)
  306. static int reduce_formats_on_filter(AVFilterContext *filter)
  307. {
  308. int i, j, k, ret = 0;
  309. REDUCE_FORMATS(int, AVFilterFormats, formats, formats,
  310. format_count, ff_add_format);
  311. REDUCE_FORMATS(int, AVFilterFormats, samplerates, formats,
  312. format_count, ff_add_format);
  313. REDUCE_FORMATS(uint64_t, AVFilterChannelLayouts, channel_layouts,
  314. channel_layouts, nb_channel_layouts, ff_add_channel_layout);
  315. return ret;
  316. }
  317. static void reduce_formats(AVFilterGraph *graph)
  318. {
  319. int i, reduced;
  320. do {
  321. reduced = 0;
  322. for (i = 0; i < graph->filter_count; i++)
  323. reduced |= reduce_formats_on_filter(graph->filters[i]);
  324. } while (reduced);
  325. }
  326. static void swap_samplerates_on_filter(AVFilterContext *filter)
  327. {
  328. AVFilterLink *link = NULL;
  329. int sample_rate;
  330. int i, j;
  331. for (i = 0; i < filter->input_count; i++) {
  332. link = filter->inputs[i];
  333. if (link->type == AVMEDIA_TYPE_AUDIO &&
  334. link->out_samplerates->format_count == 1)
  335. break;
  336. }
  337. if (i == filter->input_count)
  338. return;
  339. sample_rate = link->out_samplerates->formats[0];
  340. for (i = 0; i < filter->output_count; i++) {
  341. AVFilterLink *outlink = filter->outputs[i];
  342. int best_idx, best_diff = INT_MAX;
  343. if (outlink->type != AVMEDIA_TYPE_AUDIO ||
  344. outlink->in_samplerates->format_count < 2)
  345. continue;
  346. for (j = 0; j < outlink->in_samplerates->format_count; j++) {
  347. int diff = abs(sample_rate - outlink->in_samplerates->formats[j]);
  348. if (diff < best_diff) {
  349. best_diff = diff;
  350. best_idx = j;
  351. }
  352. }
  353. FFSWAP(int, outlink->in_samplerates->formats[0],
  354. outlink->in_samplerates->formats[best_idx]);
  355. }
  356. }
  357. static void swap_samplerates(AVFilterGraph *graph)
  358. {
  359. int i;
  360. for (i = 0; i < graph->filter_count; i++)
  361. swap_samplerates_on_filter(graph->filters[i]);
  362. }
  363. static void swap_channel_layouts_on_filter(AVFilterContext *filter)
  364. {
  365. AVFilterLink *link = NULL;
  366. uint64_t chlayout;
  367. int i, j;
  368. for (i = 0; i < filter->input_count; i++) {
  369. link = filter->inputs[i];
  370. if (link->type == AVMEDIA_TYPE_AUDIO &&
  371. link->out_channel_layouts->nb_channel_layouts == 1)
  372. break;
  373. }
  374. if (i == filter->input_count)
  375. return;
  376. chlayout = link->out_channel_layouts->channel_layouts[0];
  377. for (i = 0; i < filter->output_count; i++) {
  378. AVFilterLink *outlink = filter->outputs[i];
  379. int best_idx, best_score = INT_MIN;
  380. if (outlink->type != AVMEDIA_TYPE_AUDIO ||
  381. outlink->in_channel_layouts->nb_channel_layouts < 2)
  382. continue;
  383. for (j = 0; j < outlink->in_channel_layouts->nb_channel_layouts; j++) {
  384. uint64_t out_chlayout = outlink->in_channel_layouts->channel_layouts[j];
  385. int matched_channels = av_get_channel_layout_nb_channels(chlayout &
  386. out_chlayout);
  387. int extra_channels = av_get_channel_layout_nb_channels(out_chlayout &
  388. (~chlayout));
  389. int score = matched_channels - extra_channels;
  390. if (score > best_score) {
  391. best_score = score;
  392. best_idx = j;
  393. }
  394. }
  395. FFSWAP(uint64_t, outlink->in_channel_layouts->channel_layouts[0],
  396. outlink->in_channel_layouts->channel_layouts[best_idx]);
  397. }
  398. }
  399. static void swap_channel_layouts(AVFilterGraph *graph)
  400. {
  401. int i;
  402. for (i = 0; i < graph->filter_count; i++)
  403. swap_channel_layouts_on_filter(graph->filters[i]);
  404. }
  405. static void swap_sample_fmts_on_filter(AVFilterContext *filter)
  406. {
  407. AVFilterLink *link = NULL;
  408. int format, bps;
  409. int i, j;
  410. for (i = 0; i < filter->input_count; i++) {
  411. link = filter->inputs[i];
  412. if (link->type == AVMEDIA_TYPE_AUDIO &&
  413. link->out_formats->format_count == 1)
  414. break;
  415. }
  416. if (i == filter->input_count)
  417. return;
  418. format = link->out_formats->formats[0];
  419. bps = av_get_bytes_per_sample(format);
  420. for (i = 0; i < filter->output_count; i++) {
  421. AVFilterLink *outlink = filter->outputs[i];
  422. int best_idx, best_score = INT_MIN;
  423. if (outlink->type != AVMEDIA_TYPE_AUDIO ||
  424. outlink->in_formats->format_count < 2)
  425. continue;
  426. for (j = 0; j < outlink->in_formats->format_count; j++) {
  427. int out_format = outlink->in_formats->formats[j];
  428. int out_bps = av_get_bytes_per_sample(out_format);
  429. int score;
  430. if (av_get_packed_sample_fmt(out_format) == format ||
  431. av_get_planar_sample_fmt(out_format) == format) {
  432. best_idx = j;
  433. break;
  434. }
  435. /* for s32 and float prefer double to prevent loss of information */
  436. if (bps == 4 && out_bps == 8) {
  437. best_idx = j;
  438. break;
  439. }
  440. /* prefer closest higher or equal bps */
  441. score = -abs(out_bps - bps);
  442. if (out_bps >= bps)
  443. score += INT_MAX/2;
  444. if (score > best_score) {
  445. best_score = score;
  446. best_idx = j;
  447. }
  448. }
  449. FFSWAP(int, outlink->in_formats->formats[0],
  450. outlink->in_formats->formats[best_idx]);
  451. }
  452. }
  453. static void swap_sample_fmts(AVFilterGraph *graph)
  454. {
  455. int i;
  456. for (i = 0; i < graph->filter_count; i++)
  457. swap_sample_fmts_on_filter(graph->filters[i]);
  458. }
  459. static int pick_formats(AVFilterGraph *graph)
  460. {
  461. int i, j, ret;
  462. for (i = 0; i < graph->filter_count; i++) {
  463. AVFilterContext *filter = graph->filters[i];
  464. for (j = 0; j < filter->input_count; j++)
  465. if ((ret = pick_format(filter->inputs[j])) < 0)
  466. return ret;
  467. for (j = 0; j < filter->output_count; j++)
  468. if ((ret = pick_format(filter->outputs[j])) < 0)
  469. return ret;
  470. }
  471. return 0;
  472. }
  473. /**
  474. * Configure the formats of all the links in the graph.
  475. */
  476. static int graph_config_formats(AVFilterGraph *graph, AVClass *log_ctx)
  477. {
  478. int ret;
  479. /* find supported formats from sub-filters, and merge along links */
  480. if ((ret = query_formats(graph, log_ctx)) < 0)
  481. return ret;
  482. /* Once everything is merged, it's possible that we'll still have
  483. * multiple valid media format choices. We try to minimize the amount
  484. * of format conversion inside filters */
  485. reduce_formats(graph);
  486. /* for audio filters, ensure the best format, sample rate and channel layout
  487. * is selected */
  488. swap_sample_fmts(graph);
  489. swap_samplerates(graph);
  490. swap_channel_layouts(graph);
  491. if ((ret = pick_formats(graph)) < 0)
  492. return ret;
  493. return 0;
  494. }
  495. int avfilter_graph_config(AVFilterGraph *graphctx, void *log_ctx)
  496. {
  497. int ret;
  498. if ((ret = graph_check_validity(graphctx, log_ctx)))
  499. return ret;
  500. if ((ret = graph_config_formats(graphctx, log_ctx)))
  501. return ret;
  502. if ((ret = graph_config_links(graphctx, log_ctx)))
  503. return ret;
  504. return 0;
  505. }