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.

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