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.

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