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.

656 lines
24KB

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