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.

702 lines
26KB

  1. /*
  2. * avconv filter configuration
  3. *
  4. * This file is part of Libav.
  5. *
  6. * Libav is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * Libav is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with Libav; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include <stdint.h>
  21. #include "avconv.h"
  22. #include "libavfilter/avfilter.h"
  23. #include "libavresample/avresample.h"
  24. #include "libavutil/avassert.h"
  25. #include "libavutil/avstring.h"
  26. #include "libavutil/channel_layout.h"
  27. #include "libavutil/display.h"
  28. #include "libavutil/opt.h"
  29. #include "libavutil/pixdesc.h"
  30. #include "libavutil/pixfmt.h"
  31. #include "libavutil/samplefmt.h"
  32. /* Define a function for building a string containing a list of
  33. * allowed formats. */
  34. #define DEF_CHOOSE_FORMAT(type, var, supported_list, none, get_name) \
  35. static char *choose_ ## var ## s(OutputStream *ost) \
  36. { \
  37. if (ost->enc_ctx->var != none) { \
  38. get_name(ost->enc_ctx->var); \
  39. return av_strdup(name); \
  40. } else if (ost->enc && ost->enc->supported_list) { \
  41. const type *p; \
  42. AVIOContext *s = NULL; \
  43. uint8_t *ret; \
  44. int len; \
  45. \
  46. if (avio_open_dyn_buf(&s) < 0) \
  47. exit(1); \
  48. \
  49. for (p = ost->enc->supported_list; *p != none; p++) { \
  50. get_name(*p); \
  51. avio_printf(s, "%s|", name); \
  52. } \
  53. len = avio_close_dyn_buf(s, &ret); \
  54. ret[len - 1] = 0; \
  55. return ret; \
  56. } else \
  57. return NULL; \
  58. }
  59. DEF_CHOOSE_FORMAT(enum AVPixelFormat, pix_fmt, pix_fmts, AV_PIX_FMT_NONE,
  60. GET_PIX_FMT_NAME)
  61. DEF_CHOOSE_FORMAT(enum AVSampleFormat, sample_fmt, sample_fmts,
  62. AV_SAMPLE_FMT_NONE, GET_SAMPLE_FMT_NAME)
  63. DEF_CHOOSE_FORMAT(int, sample_rate, supported_samplerates, 0,
  64. GET_SAMPLE_RATE_NAME)
  65. DEF_CHOOSE_FORMAT(uint64_t, channel_layout, channel_layouts, 0,
  66. GET_CH_LAYOUT_NAME)
  67. FilterGraph *init_simple_filtergraph(InputStream *ist, OutputStream *ost)
  68. {
  69. FilterGraph *fg = av_mallocz(sizeof(*fg));
  70. if (!fg)
  71. exit(1);
  72. fg->index = nb_filtergraphs;
  73. GROW_ARRAY(fg->outputs, fg->nb_outputs);
  74. if (!(fg->outputs[0] = av_mallocz(sizeof(*fg->outputs[0]))))
  75. exit(1);
  76. fg->outputs[0]->ost = ost;
  77. fg->outputs[0]->graph = fg;
  78. ost->filter = fg->outputs[0];
  79. GROW_ARRAY(fg->inputs, fg->nb_inputs);
  80. if (!(fg->inputs[0] = av_mallocz(sizeof(*fg->inputs[0]))))
  81. exit(1);
  82. fg->inputs[0]->ist = ist;
  83. fg->inputs[0]->graph = fg;
  84. GROW_ARRAY(ist->filters, ist->nb_filters);
  85. ist->filters[ist->nb_filters - 1] = fg->inputs[0];
  86. GROW_ARRAY(filtergraphs, nb_filtergraphs);
  87. filtergraphs[nb_filtergraphs - 1] = fg;
  88. return fg;
  89. }
  90. static void init_input_filter(FilterGraph *fg, AVFilterInOut *in)
  91. {
  92. InputStream *ist = NULL;
  93. enum AVMediaType type = avfilter_pad_get_type(in->filter_ctx->input_pads, in->pad_idx);
  94. int i;
  95. // TODO: support other filter types
  96. if (type != AVMEDIA_TYPE_VIDEO && type != AVMEDIA_TYPE_AUDIO) {
  97. av_log(NULL, AV_LOG_FATAL, "Only video and audio filters supported "
  98. "currently.\n");
  99. exit(1);
  100. }
  101. if (in->name) {
  102. AVFormatContext *s;
  103. AVStream *st = NULL;
  104. char *p;
  105. int file_idx = strtol(in->name, &p, 0);
  106. if (file_idx < 0 || file_idx >= nb_input_files) {
  107. av_log(NULL, AV_LOG_FATAL, "Invalid file index %d in filtegraph description %s.\n",
  108. file_idx, fg->graph_desc);
  109. exit(1);
  110. }
  111. s = input_files[file_idx]->ctx;
  112. for (i = 0; i < s->nb_streams; i++) {
  113. if (s->streams[i]->codec->codec_type != type)
  114. continue;
  115. if (check_stream_specifier(s, s->streams[i], *p == ':' ? p + 1 : p) == 1) {
  116. st = s->streams[i];
  117. break;
  118. }
  119. }
  120. if (!st) {
  121. av_log(NULL, AV_LOG_FATAL, "Stream specifier '%s' in filtergraph description %s "
  122. "matches no streams.\n", p, fg->graph_desc);
  123. exit(1);
  124. }
  125. ist = input_streams[input_files[file_idx]->ist_index + st->index];
  126. } else {
  127. /* find the first unused stream of corresponding type */
  128. for (i = 0; i < nb_input_streams; i++) {
  129. ist = input_streams[i];
  130. if (ist->dec_ctx->codec_type == type && ist->discard)
  131. break;
  132. }
  133. if (i == nb_input_streams) {
  134. av_log(NULL, AV_LOG_FATAL, "Cannot find a matching stream for "
  135. "unlabeled input pad %d on filter %s\n", in->pad_idx,
  136. in->filter_ctx->name);
  137. exit(1);
  138. }
  139. }
  140. av_assert0(ist);
  141. ist->discard = 0;
  142. ist->decoding_needed = 1;
  143. ist->st->discard = AVDISCARD_NONE;
  144. GROW_ARRAY(fg->inputs, fg->nb_inputs);
  145. if (!(fg->inputs[fg->nb_inputs - 1] = av_mallocz(sizeof(*fg->inputs[0]))))
  146. exit(1);
  147. fg->inputs[fg->nb_inputs - 1]->ist = ist;
  148. fg->inputs[fg->nb_inputs - 1]->graph = fg;
  149. GROW_ARRAY(ist->filters, ist->nb_filters);
  150. ist->filters[ist->nb_filters - 1] = fg->inputs[fg->nb_inputs - 1];
  151. }
  152. static int insert_trim(int64_t start_time, int64_t duration,
  153. AVFilterContext **last_filter, int *pad_idx,
  154. const char *filter_name)
  155. {
  156. AVFilterGraph *graph = (*last_filter)->graph;
  157. AVFilterContext *ctx;
  158. const AVFilter *trim;
  159. enum AVMediaType type = avfilter_pad_get_type((*last_filter)->output_pads, *pad_idx);
  160. const char *name = (type == AVMEDIA_TYPE_VIDEO) ? "trim" : "atrim";
  161. int ret = 0;
  162. if (duration == INT64_MAX && start_time == AV_NOPTS_VALUE)
  163. return 0;
  164. trim = avfilter_get_by_name(name);
  165. if (!trim) {
  166. av_log(NULL, AV_LOG_ERROR, "%s filter not present, cannot limit "
  167. "recording time.\n", name);
  168. return AVERROR_FILTER_NOT_FOUND;
  169. }
  170. ctx = avfilter_graph_alloc_filter(graph, trim, filter_name);
  171. if (!ctx)
  172. return AVERROR(ENOMEM);
  173. if (duration != INT64_MAX) {
  174. ret = av_opt_set_double(ctx, "duration", (double)duration / 1e6,
  175. AV_OPT_SEARCH_CHILDREN);
  176. }
  177. if (ret >= 0 && start_time != AV_NOPTS_VALUE) {
  178. ret = av_opt_set_double(ctx, "start", (double)start_time / 1e6,
  179. AV_OPT_SEARCH_CHILDREN);
  180. }
  181. if (ret < 0) {
  182. av_log(ctx, AV_LOG_ERROR, "Error configuring the %s filter", name);
  183. return ret;
  184. }
  185. ret = avfilter_init_str(ctx, NULL);
  186. if (ret < 0)
  187. return ret;
  188. ret = avfilter_link(*last_filter, *pad_idx, ctx, 0);
  189. if (ret < 0)
  190. return ret;
  191. *last_filter = ctx;
  192. *pad_idx = 0;
  193. return 0;
  194. }
  195. static int insert_filter(AVFilterContext **last_filter, int *pad_idx,
  196. const char *filter_name, const char *args)
  197. {
  198. AVFilterGraph *graph = (*last_filter)->graph;
  199. AVFilterContext *ctx;
  200. int ret;
  201. ret = avfilter_graph_create_filter(&ctx,
  202. avfilter_get_by_name(filter_name),
  203. filter_name, args, NULL, graph);
  204. if (ret < 0)
  205. return ret;
  206. ret = avfilter_link(*last_filter, *pad_idx, ctx, 0);
  207. if (ret < 0)
  208. return ret;
  209. *last_filter = ctx;
  210. *pad_idx = 0;
  211. return 0;
  212. }
  213. static int configure_output_video_filter(FilterGraph *fg, OutputFilter *ofilter, AVFilterInOut *out)
  214. {
  215. char *pix_fmts;
  216. OutputStream *ost = ofilter->ost;
  217. OutputFile *of = output_files[ost->file_index];
  218. AVCodecContext *codec = ost->enc_ctx;
  219. AVFilterContext *last_filter = out->filter_ctx;
  220. int pad_idx = out->pad_idx;
  221. int ret;
  222. char name[255];
  223. snprintf(name, sizeof(name), "output stream %d:%d", ost->file_index, ost->index);
  224. ret = avfilter_graph_create_filter(&ofilter->filter,
  225. avfilter_get_by_name("buffersink"),
  226. name, NULL, NULL, fg->graph);
  227. if (ret < 0)
  228. return ret;
  229. if (codec->width || codec->height) {
  230. char args[255];
  231. AVFilterContext *filter;
  232. snprintf(args, sizeof(args), "%d:%d:0x%X",
  233. codec->width,
  234. codec->height,
  235. (unsigned)ost->sws_flags);
  236. snprintf(name, sizeof(name), "scaler for output stream %d:%d",
  237. ost->file_index, ost->index);
  238. if ((ret = avfilter_graph_create_filter(&filter, avfilter_get_by_name("scale"),
  239. name, args, NULL, fg->graph)) < 0)
  240. return ret;
  241. if ((ret = avfilter_link(last_filter, pad_idx, filter, 0)) < 0)
  242. return ret;
  243. last_filter = filter;
  244. pad_idx = 0;
  245. }
  246. if ((pix_fmts = choose_pix_fmts(ost))) {
  247. AVFilterContext *filter;
  248. snprintf(name, sizeof(name), "pixel format for output stream %d:%d",
  249. ost->file_index, ost->index);
  250. ret = avfilter_graph_create_filter(&filter,
  251. avfilter_get_by_name("format"),
  252. "format", pix_fmts, NULL, fg->graph);
  253. av_freep(&pix_fmts);
  254. if (ret < 0)
  255. return ret;
  256. if ((ret = avfilter_link(last_filter, pad_idx, filter, 0)) < 0)
  257. return ret;
  258. last_filter = filter;
  259. pad_idx = 0;
  260. }
  261. if (ost->frame_rate.num) {
  262. AVFilterContext *fps;
  263. char args[255];
  264. snprintf(args, sizeof(args), "fps=%d/%d", ost->frame_rate.num,
  265. ost->frame_rate.den);
  266. snprintf(name, sizeof(name), "fps for output stream %d:%d",
  267. ost->file_index, ost->index);
  268. ret = avfilter_graph_create_filter(&fps, avfilter_get_by_name("fps"),
  269. name, args, NULL, fg->graph);
  270. if (ret < 0)
  271. return ret;
  272. ret = avfilter_link(last_filter, pad_idx, fps, 0);
  273. if (ret < 0)
  274. return ret;
  275. last_filter = fps;
  276. pad_idx = 0;
  277. }
  278. snprintf(name, sizeof(name), "trim for output stream %d:%d",
  279. ost->file_index, ost->index);
  280. ret = insert_trim(of->start_time, of->recording_time,
  281. &last_filter, &pad_idx, name);
  282. if (ret < 0)
  283. return ret;
  284. if ((ret = avfilter_link(last_filter, pad_idx, ofilter->filter, 0)) < 0)
  285. return ret;
  286. return 0;
  287. }
  288. static int configure_output_audio_filter(FilterGraph *fg, OutputFilter *ofilter, AVFilterInOut *out)
  289. {
  290. OutputStream *ost = ofilter->ost;
  291. OutputFile *of = output_files[ost->file_index];
  292. AVCodecContext *codec = ost->enc_ctx;
  293. AVFilterContext *last_filter = out->filter_ctx;
  294. int pad_idx = out->pad_idx;
  295. char *sample_fmts, *sample_rates, *channel_layouts;
  296. char name[255];
  297. int ret;
  298. snprintf(name, sizeof(name), "output stream %d:%d", ost->file_index, ost->index);
  299. ret = avfilter_graph_create_filter(&ofilter->filter,
  300. avfilter_get_by_name("abuffersink"),
  301. name, NULL, NULL, fg->graph);
  302. if (ret < 0)
  303. return ret;
  304. if (codec->channels && !codec->channel_layout)
  305. codec->channel_layout = av_get_default_channel_layout(codec->channels);
  306. sample_fmts = choose_sample_fmts(ost);
  307. sample_rates = choose_sample_rates(ost);
  308. channel_layouts = choose_channel_layouts(ost);
  309. if (sample_fmts || sample_rates || channel_layouts) {
  310. AVFilterContext *format;
  311. char args[256];
  312. int len = 0;
  313. if (sample_fmts)
  314. len += snprintf(args + len, sizeof(args) - len, "sample_fmts=%s:",
  315. sample_fmts);
  316. if (sample_rates)
  317. len += snprintf(args + len, sizeof(args) - len, "sample_rates=%s:",
  318. sample_rates);
  319. if (channel_layouts)
  320. len += snprintf(args + len, sizeof(args) - len, "channel_layouts=%s:",
  321. channel_layouts);
  322. args[len - 1] = 0;
  323. av_freep(&sample_fmts);
  324. av_freep(&sample_rates);
  325. av_freep(&channel_layouts);
  326. snprintf(name, sizeof(name), "audio format for output stream %d:%d",
  327. ost->file_index, ost->index);
  328. ret = avfilter_graph_create_filter(&format,
  329. avfilter_get_by_name("aformat"),
  330. name, args, NULL, fg->graph);
  331. if (ret < 0)
  332. return ret;
  333. ret = avfilter_link(last_filter, pad_idx, format, 0);
  334. if (ret < 0)
  335. return ret;
  336. last_filter = format;
  337. pad_idx = 0;
  338. }
  339. snprintf(name, sizeof(name), "trim for output stream %d:%d",
  340. ost->file_index, ost->index);
  341. ret = insert_trim(of->start_time, of->recording_time,
  342. &last_filter, &pad_idx, name);
  343. if (ret < 0)
  344. return ret;
  345. if ((ret = avfilter_link(last_filter, pad_idx, ofilter->filter, 0)) < 0)
  346. return ret;
  347. return 0;
  348. }
  349. #define DESCRIBE_FILTER_LINK(f, inout, in) \
  350. { \
  351. AVFilterContext *ctx = inout->filter_ctx; \
  352. AVFilterPad *pads = in ? ctx->input_pads : ctx->output_pads; \
  353. int nb_pads = in ? ctx->nb_inputs : ctx->nb_outputs; \
  354. AVIOContext *pb; \
  355. \
  356. if (avio_open_dyn_buf(&pb) < 0) \
  357. exit(1); \
  358. \
  359. avio_printf(pb, "%s", ctx->filter->name); \
  360. if (nb_pads > 1) \
  361. avio_printf(pb, ":%s", avfilter_pad_get_name(pads, inout->pad_idx));\
  362. avio_w8(pb, 0); \
  363. avio_close_dyn_buf(pb, &f->name); \
  364. }
  365. int configure_output_filter(FilterGraph *fg, OutputFilter *ofilter, AVFilterInOut *out)
  366. {
  367. av_freep(&ofilter->name);
  368. DESCRIBE_FILTER_LINK(ofilter, out, 0);
  369. switch (avfilter_pad_get_type(out->filter_ctx->output_pads, out->pad_idx)) {
  370. case AVMEDIA_TYPE_VIDEO: return configure_output_video_filter(fg, ofilter, out);
  371. case AVMEDIA_TYPE_AUDIO: return configure_output_audio_filter(fg, ofilter, out);
  372. default: av_assert0(0);
  373. }
  374. }
  375. static int configure_input_video_filter(FilterGraph *fg, InputFilter *ifilter,
  376. AVFilterInOut *in)
  377. {
  378. AVFilterContext *last_filter;
  379. const AVFilter *buffer_filt = avfilter_get_by_name("buffer");
  380. InputStream *ist = ifilter->ist;
  381. InputFile *f = input_files[ist->file_index];
  382. AVRational tb = ist->framerate.num ? av_inv_q(ist->framerate) :
  383. ist->st->time_base;
  384. AVRational sar;
  385. char args[255], name[255];
  386. int ret, pad_idx = 0;
  387. sar = ist->st->sample_aspect_ratio.num ?
  388. ist->st->sample_aspect_ratio :
  389. ist->dec_ctx->sample_aspect_ratio;
  390. snprintf(args, sizeof(args), "%d:%d:%d:%d:%d:%d:%d", ist->dec_ctx->width,
  391. ist->dec_ctx->height,
  392. ist->hwaccel_retrieve_data ? ist->hwaccel_retrieved_pix_fmt : ist->dec_ctx->pix_fmt,
  393. tb.num, tb.den, sar.num, sar.den);
  394. snprintf(name, sizeof(name), "graph %d input from stream %d:%d", fg->index,
  395. ist->file_index, ist->st->index);
  396. if ((ret = avfilter_graph_create_filter(&ifilter->filter, buffer_filt, name,
  397. args, NULL, fg->graph)) < 0)
  398. return ret;
  399. last_filter = ifilter->filter;
  400. if (ist->autorotate) {
  401. uint8_t* displaymatrix = av_stream_get_side_data(ist->st,
  402. AV_PKT_DATA_DISPLAYMATRIX, NULL);
  403. if (displaymatrix) {
  404. double rot = av_display_rotation_get((int32_t*) displaymatrix);
  405. if (rot < -135 || rot > 135) {
  406. ret = insert_filter(&last_filter, &pad_idx, "vflip", NULL);
  407. if (ret < 0)
  408. return ret;
  409. ret = insert_filter(&last_filter, &pad_idx, "hflip", NULL);
  410. } else if (rot < -45) {
  411. ret = insert_filter(&last_filter, &pad_idx, "transpose", "dir=clock");
  412. } else if (rot > 45) {
  413. ret = insert_filter(&last_filter, &pad_idx, "transpose", "dir=cclock");
  414. }
  415. if (ret < 0)
  416. return ret;
  417. }
  418. }
  419. if (ist->framerate.num) {
  420. AVFilterContext *setpts;
  421. snprintf(name, sizeof(name), "force CFR for input from stream %d:%d",
  422. ist->file_index, ist->st->index);
  423. if ((ret = avfilter_graph_create_filter(&setpts,
  424. avfilter_get_by_name("setpts"),
  425. name, "N", NULL,
  426. fg->graph)) < 0)
  427. return ret;
  428. if ((ret = avfilter_link(last_filter, 0, setpts, 0)) < 0)
  429. return ret;
  430. last_filter = setpts;
  431. }
  432. snprintf(name, sizeof(name), "trim for input stream %d:%d",
  433. ist->file_index, ist->st->index);
  434. ret = insert_trim(((f->start_time == AV_NOPTS_VALUE) || !f->accurate_seek) ?
  435. AV_NOPTS_VALUE : 0, f->recording_time, &last_filter, &pad_idx, name);
  436. if (ret < 0)
  437. return ret;
  438. if ((ret = avfilter_link(last_filter, 0, in->filter_ctx, in->pad_idx)) < 0)
  439. return ret;
  440. return 0;
  441. }
  442. static int configure_input_audio_filter(FilterGraph *fg, InputFilter *ifilter,
  443. AVFilterInOut *in)
  444. {
  445. AVFilterContext *last_filter;
  446. const AVFilter *abuffer_filt = avfilter_get_by_name("abuffer");
  447. InputStream *ist = ifilter->ist;
  448. InputFile *f = input_files[ist->file_index];
  449. char args[255], name[255];
  450. int ret, pad_idx = 0;
  451. snprintf(args, sizeof(args), "time_base=%d/%d:sample_rate=%d:sample_fmt=%s"
  452. ":channel_layout=0x%"PRIx64,
  453. 1, ist->dec_ctx->sample_rate,
  454. ist->dec_ctx->sample_rate,
  455. av_get_sample_fmt_name(ist->dec_ctx->sample_fmt),
  456. ist->dec_ctx->channel_layout);
  457. snprintf(name, sizeof(name), "graph %d input from stream %d:%d", fg->index,
  458. ist->file_index, ist->st->index);
  459. if ((ret = avfilter_graph_create_filter(&ifilter->filter, abuffer_filt,
  460. name, args, NULL,
  461. fg->graph)) < 0)
  462. return ret;
  463. last_filter = ifilter->filter;
  464. if (audio_sync_method > 0) {
  465. AVFilterContext *async;
  466. int len = 0;
  467. av_log(NULL, AV_LOG_WARNING, "-async has been deprecated. Used the "
  468. "asyncts audio filter instead.\n");
  469. if (audio_sync_method > 1)
  470. len += snprintf(args + len, sizeof(args) - len, "compensate=1:"
  471. "max_comp=%d:", audio_sync_method);
  472. snprintf(args + len, sizeof(args) - len, "min_delta=%f",
  473. audio_drift_threshold);
  474. snprintf(name, sizeof(name), "graph %d audio sync for input stream %d:%d",
  475. fg->index, ist->file_index, ist->st->index);
  476. ret = avfilter_graph_create_filter(&async,
  477. avfilter_get_by_name("asyncts"),
  478. name, args, NULL, fg->graph);
  479. if (ret < 0)
  480. return ret;
  481. ret = avfilter_link(last_filter, 0, async, 0);
  482. if (ret < 0)
  483. return ret;
  484. last_filter = async;
  485. }
  486. if (audio_volume != 256) {
  487. AVFilterContext *volume;
  488. av_log(NULL, AV_LOG_WARNING, "-vol has been deprecated. Use the volume "
  489. "audio filter instead.\n");
  490. snprintf(args, sizeof(args), "volume=%f", audio_volume / 256.0);
  491. snprintf(name, sizeof(name), "graph %d volume for input stream %d:%d",
  492. fg->index, ist->file_index, ist->st->index);
  493. ret = avfilter_graph_create_filter(&volume,
  494. avfilter_get_by_name("volume"),
  495. name, args, NULL, fg->graph);
  496. if (ret < 0)
  497. return ret;
  498. ret = avfilter_link(last_filter, 0, volume, 0);
  499. if (ret < 0)
  500. return ret;
  501. last_filter = volume;
  502. }
  503. snprintf(name, sizeof(name), "trim for input stream %d:%d",
  504. ist->file_index, ist->st->index);
  505. ret = insert_trim(((f->start_time == AV_NOPTS_VALUE) || !f->accurate_seek) ?
  506. AV_NOPTS_VALUE : 0, f->recording_time, &last_filter, &pad_idx, name);
  507. if (ret < 0)
  508. return ret;
  509. if ((ret = avfilter_link(last_filter, 0, in->filter_ctx, in->pad_idx)) < 0)
  510. return ret;
  511. return 0;
  512. }
  513. static int configure_input_filter(FilterGraph *fg, InputFilter *ifilter,
  514. AVFilterInOut *in)
  515. {
  516. av_freep(&ifilter->name);
  517. DESCRIBE_FILTER_LINK(ifilter, in, 1);
  518. switch (avfilter_pad_get_type(in->filter_ctx->input_pads, in->pad_idx)) {
  519. case AVMEDIA_TYPE_VIDEO: return configure_input_video_filter(fg, ifilter, in);
  520. case AVMEDIA_TYPE_AUDIO: return configure_input_audio_filter(fg, ifilter, in);
  521. default: av_assert0(0);
  522. }
  523. }
  524. int configure_filtergraph(FilterGraph *fg)
  525. {
  526. AVFilterInOut *inputs, *outputs, *cur;
  527. int ret, i, init = !fg->graph, simple = !fg->graph_desc;
  528. const char *graph_desc = simple ? fg->outputs[0]->ost->avfilter :
  529. fg->graph_desc;
  530. avfilter_graph_free(&fg->graph);
  531. if (!(fg->graph = avfilter_graph_alloc()))
  532. return AVERROR(ENOMEM);
  533. if (simple) {
  534. OutputStream *ost = fg->outputs[0]->ost;
  535. char args[512];
  536. AVDictionaryEntry *e = NULL;
  537. snprintf(args, sizeof(args), "flags=0x%X", (unsigned)ost->sws_flags);
  538. fg->graph->scale_sws_opts = av_strdup(args);
  539. args[0] = '\0';
  540. while ((e = av_dict_get(fg->outputs[0]->ost->resample_opts, "", e,
  541. AV_DICT_IGNORE_SUFFIX))) {
  542. av_strlcatf(args, sizeof(args), "%s=%s:", e->key, e->value);
  543. }
  544. if (strlen(args))
  545. args[strlen(args) - 1] = '\0';
  546. fg->graph->resample_lavr_opts = av_strdup(args);
  547. }
  548. if ((ret = avfilter_graph_parse2(fg->graph, graph_desc, &inputs, &outputs)) < 0)
  549. return ret;
  550. if (simple && (!inputs || inputs->next || !outputs || outputs->next)) {
  551. av_log(NULL, AV_LOG_ERROR, "Simple filtergraph '%s' does not have "
  552. "exactly one input and output.\n", graph_desc);
  553. return AVERROR(EINVAL);
  554. }
  555. for (cur = inputs; !simple && init && cur; cur = cur->next)
  556. init_input_filter(fg, cur);
  557. for (cur = inputs, i = 0; cur; cur = cur->next, i++)
  558. if ((ret = configure_input_filter(fg, fg->inputs[i], cur)) < 0)
  559. return ret;
  560. avfilter_inout_free(&inputs);
  561. if (!init || simple) {
  562. /* we already know the mappings between lavfi outputs and output streams,
  563. * so we can finish the setup */
  564. for (cur = outputs, i = 0; cur; cur = cur->next, i++)
  565. configure_output_filter(fg, fg->outputs[i], cur);
  566. avfilter_inout_free(&outputs);
  567. if ((ret = avfilter_graph_config(fg->graph, NULL)) < 0)
  568. return ret;
  569. } else {
  570. /* wait until output mappings are processed */
  571. for (cur = outputs; cur;) {
  572. GROW_ARRAY(fg->outputs, fg->nb_outputs);
  573. if (!(fg->outputs[fg->nb_outputs - 1] = av_mallocz(sizeof(*fg->outputs[0]))))
  574. exit(1);
  575. fg->outputs[fg->nb_outputs - 1]->graph = fg;
  576. fg->outputs[fg->nb_outputs - 1]->out_tmp = cur;
  577. cur = cur->next;
  578. fg->outputs[fg->nb_outputs - 1]->out_tmp->next = NULL;
  579. }
  580. }
  581. return 0;
  582. }
  583. int ist_in_filtergraph(FilterGraph *fg, InputStream *ist)
  584. {
  585. int i;
  586. for (i = 0; i < fg->nb_inputs; i++)
  587. if (fg->inputs[i]->ist == ist)
  588. return 1;
  589. return 0;
  590. }