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.

608 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->nb_inputs; 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->nb_outputs; 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->nb_outputs) {
  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->nb_inputs; 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. if (!(filter = avfilter_get_by_name("scale"))) {
  185. av_log(log_ctx, AV_LOG_ERROR, "'scale' filter "
  186. "not present, cannot convert pixel formats.\n");
  187. return AVERROR(EINVAL);
  188. }
  189. snprintf(inst_name, sizeof(inst_name), "auto-inserted scaler %d",
  190. scaler_count++);
  191. snprintf(scale_args, sizeof(scale_args), "0:0:%s", graph->scale_sws_opts);
  192. if ((ret = avfilter_graph_create_filter(&convert, filter,
  193. inst_name, scale_args, NULL,
  194. graph)) < 0)
  195. return ret;
  196. break;
  197. case AVMEDIA_TYPE_AUDIO:
  198. if (!(filter = avfilter_get_by_name("resample"))) {
  199. av_log(log_ctx, AV_LOG_ERROR, "'resample' filter "
  200. "not present, cannot convert audio formats.\n");
  201. return AVERROR(EINVAL);
  202. }
  203. snprintf(inst_name, sizeof(inst_name), "auto-inserted resampler %d",
  204. resampler_count++);
  205. if ((ret = avfilter_graph_create_filter(&convert,
  206. avfilter_get_by_name("resample"),
  207. inst_name, NULL, NULL, graph)) < 0)
  208. return ret;
  209. break;
  210. default:
  211. return AVERROR(EINVAL);
  212. }
  213. if ((ret = avfilter_insert_filter(link, convert, 0, 0)) < 0)
  214. return ret;
  215. convert->filter->query_formats(convert);
  216. inlink = convert->inputs[0];
  217. outlink = convert->outputs[0];
  218. if (!ff_merge_formats( inlink->in_formats, inlink->out_formats) ||
  219. !ff_merge_formats(outlink->in_formats, outlink->out_formats))
  220. ret |= AVERROR(ENOSYS);
  221. if (inlink->type == AVMEDIA_TYPE_AUDIO &&
  222. (!ff_merge_samplerates(inlink->in_samplerates,
  223. inlink->out_samplerates) ||
  224. !ff_merge_channel_layouts(inlink->in_channel_layouts,
  225. inlink->out_channel_layouts)))
  226. ret |= AVERROR(ENOSYS);
  227. if (outlink->type == AVMEDIA_TYPE_AUDIO &&
  228. (!ff_merge_samplerates(outlink->in_samplerates,
  229. outlink->out_samplerates) ||
  230. !ff_merge_channel_layouts(outlink->in_channel_layouts,
  231. outlink->out_channel_layouts)))
  232. ret |= AVERROR(ENOSYS);
  233. if (ret < 0) {
  234. av_log(log_ctx, AV_LOG_ERROR,
  235. "Impossible to convert between the formats supported by the filter "
  236. "'%s' and the filter '%s'\n", link->src->name, link->dst->name);
  237. return ret;
  238. }
  239. }
  240. }
  241. }
  242. return 0;
  243. }
  244. static int pick_format(AVFilterLink *link)
  245. {
  246. if (!link || !link->in_formats)
  247. return 0;
  248. link->in_formats->format_count = 1;
  249. link->format = link->in_formats->formats[0];
  250. if (link->type == AVMEDIA_TYPE_AUDIO) {
  251. if (!link->in_samplerates->format_count) {
  252. av_log(link->src, AV_LOG_ERROR, "Cannot select sample rate for"
  253. " the link between filters %s and %s.\n", link->src->name,
  254. link->dst->name);
  255. return AVERROR(EINVAL);
  256. }
  257. link->in_samplerates->format_count = 1;
  258. link->sample_rate = link->in_samplerates->formats[0];
  259. if (!link->in_channel_layouts->nb_channel_layouts) {
  260. av_log(link->src, AV_LOG_ERROR, "Cannot select channel layout for"
  261. "the link between filters %s and %s.\n", link->src->name,
  262. link->dst->name);
  263. return AVERROR(EINVAL);
  264. }
  265. link->in_channel_layouts->nb_channel_layouts = 1;
  266. link->channel_layout = link->in_channel_layouts->channel_layouts[0];
  267. }
  268. ff_formats_unref(&link->in_formats);
  269. ff_formats_unref(&link->out_formats);
  270. ff_formats_unref(&link->in_samplerates);
  271. ff_formats_unref(&link->out_samplerates);
  272. ff_channel_layouts_unref(&link->in_channel_layouts);
  273. ff_channel_layouts_unref(&link->out_channel_layouts);
  274. return 0;
  275. }
  276. #define REDUCE_FORMATS(fmt_type, list_type, list, var, nb, add_format) \
  277. do { \
  278. for (i = 0; i < filter->nb_inputs; i++) { \
  279. AVFilterLink *link = filter->inputs[i]; \
  280. fmt_type fmt; \
  281. \
  282. if (!link->out_ ## list || link->out_ ## list->nb != 1) \
  283. continue; \
  284. fmt = link->out_ ## list->var[0]; \
  285. \
  286. for (j = 0; j < filter->nb_outputs; j++) { \
  287. AVFilterLink *out_link = filter->outputs[j]; \
  288. list_type *fmts; \
  289. \
  290. if (link->type != out_link->type || \
  291. out_link->in_ ## list->nb == 1) \
  292. continue; \
  293. fmts = out_link->in_ ## list; \
  294. \
  295. if (!out_link->in_ ## list->nb) { \
  296. add_format(&out_link->in_ ##list, fmt); \
  297. break; \
  298. } \
  299. \
  300. for (k = 0; k < out_link->in_ ## list->nb; k++) \
  301. if (fmts->var[k] == fmt) { \
  302. fmts->var[0] = fmt; \
  303. fmts->nb = 1; \
  304. ret = 1; \
  305. break; \
  306. } \
  307. } \
  308. } \
  309. } while (0)
  310. static int reduce_formats_on_filter(AVFilterContext *filter)
  311. {
  312. int i, j, k, ret = 0;
  313. REDUCE_FORMATS(int, AVFilterFormats, formats, formats,
  314. format_count, ff_add_format);
  315. REDUCE_FORMATS(int, AVFilterFormats, samplerates, formats,
  316. format_count, ff_add_format);
  317. REDUCE_FORMATS(uint64_t, AVFilterChannelLayouts, channel_layouts,
  318. channel_layouts, nb_channel_layouts, ff_add_channel_layout);
  319. return ret;
  320. }
  321. static void reduce_formats(AVFilterGraph *graph)
  322. {
  323. int i, reduced;
  324. do {
  325. reduced = 0;
  326. for (i = 0; i < graph->filter_count; i++)
  327. reduced |= reduce_formats_on_filter(graph->filters[i]);
  328. } while (reduced);
  329. }
  330. static void swap_samplerates_on_filter(AVFilterContext *filter)
  331. {
  332. AVFilterLink *link = NULL;
  333. int sample_rate;
  334. int i, j;
  335. for (i = 0; i < filter->nb_inputs; i++) {
  336. link = filter->inputs[i];
  337. if (link->type == AVMEDIA_TYPE_AUDIO &&
  338. link->out_samplerates->format_count == 1)
  339. break;
  340. }
  341. if (i == filter->nb_inputs)
  342. return;
  343. sample_rate = link->out_samplerates->formats[0];
  344. for (i = 0; i < filter->nb_outputs; i++) {
  345. AVFilterLink *outlink = filter->outputs[i];
  346. int best_idx, best_diff = INT_MAX;
  347. if (outlink->type != AVMEDIA_TYPE_AUDIO ||
  348. outlink->in_samplerates->format_count < 2)
  349. continue;
  350. for (j = 0; j < outlink->in_samplerates->format_count; j++) {
  351. int diff = abs(sample_rate - outlink->in_samplerates->formats[j]);
  352. if (diff < best_diff) {
  353. best_diff = diff;
  354. best_idx = j;
  355. }
  356. }
  357. FFSWAP(int, outlink->in_samplerates->formats[0],
  358. outlink->in_samplerates->formats[best_idx]);
  359. }
  360. }
  361. static void swap_samplerates(AVFilterGraph *graph)
  362. {
  363. int i;
  364. for (i = 0; i < graph->filter_count; i++)
  365. swap_samplerates_on_filter(graph->filters[i]);
  366. }
  367. static void swap_channel_layouts_on_filter(AVFilterContext *filter)
  368. {
  369. AVFilterLink *link = NULL;
  370. uint64_t chlayout;
  371. int i, j;
  372. for (i = 0; i < filter->nb_inputs; i++) {
  373. link = filter->inputs[i];
  374. if (link->type == AVMEDIA_TYPE_AUDIO &&
  375. link->out_channel_layouts->nb_channel_layouts == 1)
  376. break;
  377. }
  378. if (i == filter->nb_inputs)
  379. return;
  380. chlayout = link->out_channel_layouts->channel_layouts[0];
  381. for (i = 0; i < filter->nb_outputs; i++) {
  382. AVFilterLink *outlink = filter->outputs[i];
  383. int best_idx, best_score = INT_MIN;
  384. if (outlink->type != AVMEDIA_TYPE_AUDIO ||
  385. outlink->in_channel_layouts->nb_channel_layouts < 2)
  386. continue;
  387. for (j = 0; j < outlink->in_channel_layouts->nb_channel_layouts; j++) {
  388. uint64_t out_chlayout = outlink->in_channel_layouts->channel_layouts[j];
  389. int matched_channels = av_get_channel_layout_nb_channels(chlayout &
  390. out_chlayout);
  391. int extra_channels = av_get_channel_layout_nb_channels(out_chlayout &
  392. (~chlayout));
  393. int score = matched_channels - extra_channels;
  394. if (score > best_score) {
  395. best_score = score;
  396. best_idx = j;
  397. }
  398. }
  399. FFSWAP(uint64_t, outlink->in_channel_layouts->channel_layouts[0],
  400. outlink->in_channel_layouts->channel_layouts[best_idx]);
  401. }
  402. }
  403. static void swap_channel_layouts(AVFilterGraph *graph)
  404. {
  405. int i;
  406. for (i = 0; i < graph->filter_count; i++)
  407. swap_channel_layouts_on_filter(graph->filters[i]);
  408. }
  409. static void swap_sample_fmts_on_filter(AVFilterContext *filter)
  410. {
  411. AVFilterLink *link = NULL;
  412. int format, bps;
  413. int i, j;
  414. for (i = 0; i < filter->nb_inputs; i++) {
  415. link = filter->inputs[i];
  416. if (link->type == AVMEDIA_TYPE_AUDIO &&
  417. link->out_formats->format_count == 1)
  418. break;
  419. }
  420. if (i == filter->nb_inputs)
  421. return;
  422. format = link->out_formats->formats[0];
  423. bps = av_get_bytes_per_sample(format);
  424. for (i = 0; i < filter->nb_outputs; i++) {
  425. AVFilterLink *outlink = filter->outputs[i];
  426. int best_idx, best_score = INT_MIN;
  427. if (outlink->type != AVMEDIA_TYPE_AUDIO ||
  428. outlink->in_formats->format_count < 2)
  429. continue;
  430. for (j = 0; j < outlink->in_formats->format_count; j++) {
  431. int out_format = outlink->in_formats->formats[j];
  432. int out_bps = av_get_bytes_per_sample(out_format);
  433. int score;
  434. if (av_get_packed_sample_fmt(out_format) == format ||
  435. av_get_planar_sample_fmt(out_format) == format) {
  436. best_idx = j;
  437. break;
  438. }
  439. /* for s32 and float prefer double to prevent loss of information */
  440. if (bps == 4 && out_bps == 8) {
  441. best_idx = j;
  442. break;
  443. }
  444. /* prefer closest higher or equal bps */
  445. score = -abs(out_bps - bps);
  446. if (out_bps >= bps)
  447. score += INT_MAX/2;
  448. if (score > best_score) {
  449. best_score = score;
  450. best_idx = j;
  451. }
  452. }
  453. FFSWAP(int, outlink->in_formats->formats[0],
  454. outlink->in_formats->formats[best_idx]);
  455. }
  456. }
  457. static void swap_sample_fmts(AVFilterGraph *graph)
  458. {
  459. int i;
  460. for (i = 0; i < graph->filter_count; i++)
  461. swap_sample_fmts_on_filter(graph->filters[i]);
  462. }
  463. static int pick_formats(AVFilterGraph *graph)
  464. {
  465. int i, j, ret;
  466. for (i = 0; i < graph->filter_count; i++) {
  467. AVFilterContext *filter = graph->filters[i];
  468. for (j = 0; j < filter->nb_inputs; j++)
  469. if ((ret = pick_format(filter->inputs[j])) < 0)
  470. return ret;
  471. for (j = 0; j < filter->nb_outputs; j++)
  472. if ((ret = pick_format(filter->outputs[j])) < 0)
  473. return ret;
  474. }
  475. return 0;
  476. }
  477. /**
  478. * Configure the formats of all the links in the graph.
  479. */
  480. static int graph_config_formats(AVFilterGraph *graph, AVClass *log_ctx)
  481. {
  482. int ret;
  483. /* find supported formats from sub-filters, and merge along links */
  484. if ((ret = query_formats(graph, log_ctx)) < 0)
  485. return ret;
  486. /* Once everything is merged, it's possible that we'll still have
  487. * multiple valid media format choices. We try to minimize the amount
  488. * of format conversion inside filters */
  489. reduce_formats(graph);
  490. /* for audio filters, ensure the best format, sample rate and channel layout
  491. * is selected */
  492. swap_sample_fmts(graph);
  493. swap_samplerates(graph);
  494. swap_channel_layouts(graph);
  495. if ((ret = pick_formats(graph)) < 0)
  496. return ret;
  497. return 0;
  498. }
  499. int avfilter_graph_config(AVFilterGraph *graphctx, void *log_ctx)
  500. {
  501. int ret;
  502. if ((ret = graph_check_validity(graphctx, log_ctx)))
  503. return ret;
  504. if ((ret = graph_config_formats(graphctx, log_ctx)))
  505. return ret;
  506. if ((ret = graph_config_links(graphctx, log_ctx)))
  507. return ret;
  508. return 0;
  509. }