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.

647 lines
22KB

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