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.

723 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. int init_complex_filtergraph(FilterGraph *fg)
  153. {
  154. AVFilterInOut *inputs, *outputs, *cur;
  155. AVFilterGraph *graph;
  156. int ret = 0;
  157. /* this graph is only used for determining the kinds of inputs
  158. * and outputs we have, and is discarded on exit from this function */
  159. graph = avfilter_graph_alloc();
  160. if (!graph)
  161. return AVERROR(ENOMEM);
  162. ret = avfilter_graph_parse2(graph, fg->graph_desc, &inputs, &outputs);
  163. if (ret < 0)
  164. goto fail;
  165. for (cur = inputs; cur; cur = cur->next)
  166. init_input_filter(fg, cur);
  167. for (cur = outputs; cur;) {
  168. GROW_ARRAY(fg->outputs, fg->nb_outputs);
  169. fg->outputs[fg->nb_outputs - 1] = av_mallocz(sizeof(*fg->outputs[0]));
  170. if (!fg->outputs[fg->nb_outputs - 1])
  171. exit(1);
  172. fg->outputs[fg->nb_outputs - 1]->graph = fg;
  173. fg->outputs[fg->nb_outputs - 1]->out_tmp = cur;
  174. fg->outputs[fg->nb_outputs - 1]->type = avfilter_pad_get_type(cur->filter_ctx->output_pads,
  175. cur->pad_idx);
  176. cur = cur->next;
  177. fg->outputs[fg->nb_outputs - 1]->out_tmp->next = NULL;
  178. }
  179. fail:
  180. avfilter_inout_free(&inputs);
  181. avfilter_graph_free(&graph);
  182. return ret;
  183. }
  184. static int insert_trim(int64_t start_time, int64_t duration,
  185. AVFilterContext **last_filter, int *pad_idx,
  186. const char *filter_name)
  187. {
  188. AVFilterGraph *graph = (*last_filter)->graph;
  189. AVFilterContext *ctx;
  190. const AVFilter *trim;
  191. enum AVMediaType type = avfilter_pad_get_type((*last_filter)->output_pads, *pad_idx);
  192. const char *name = (type == AVMEDIA_TYPE_VIDEO) ? "trim" : "atrim";
  193. int ret = 0;
  194. if (duration == INT64_MAX && start_time == AV_NOPTS_VALUE)
  195. return 0;
  196. trim = avfilter_get_by_name(name);
  197. if (!trim) {
  198. av_log(NULL, AV_LOG_ERROR, "%s filter not present, cannot limit "
  199. "recording time.\n", name);
  200. return AVERROR_FILTER_NOT_FOUND;
  201. }
  202. ctx = avfilter_graph_alloc_filter(graph, trim, filter_name);
  203. if (!ctx)
  204. return AVERROR(ENOMEM);
  205. if (duration != INT64_MAX) {
  206. ret = av_opt_set_double(ctx, "duration", (double)duration / 1e6,
  207. AV_OPT_SEARCH_CHILDREN);
  208. }
  209. if (ret >= 0 && start_time != AV_NOPTS_VALUE) {
  210. ret = av_opt_set_double(ctx, "start", (double)start_time / 1e6,
  211. AV_OPT_SEARCH_CHILDREN);
  212. }
  213. if (ret < 0) {
  214. av_log(ctx, AV_LOG_ERROR, "Error configuring the %s filter", name);
  215. return ret;
  216. }
  217. ret = avfilter_init_str(ctx, NULL);
  218. if (ret < 0)
  219. return ret;
  220. ret = avfilter_link(*last_filter, *pad_idx, ctx, 0);
  221. if (ret < 0)
  222. return ret;
  223. *last_filter = ctx;
  224. *pad_idx = 0;
  225. return 0;
  226. }
  227. static int insert_filter(AVFilterContext **last_filter, int *pad_idx,
  228. const char *filter_name, const char *args)
  229. {
  230. AVFilterGraph *graph = (*last_filter)->graph;
  231. AVFilterContext *ctx;
  232. int ret;
  233. ret = avfilter_graph_create_filter(&ctx,
  234. avfilter_get_by_name(filter_name),
  235. filter_name, args, NULL, graph);
  236. if (ret < 0)
  237. return ret;
  238. ret = avfilter_link(*last_filter, *pad_idx, ctx, 0);
  239. if (ret < 0)
  240. return ret;
  241. *last_filter = ctx;
  242. *pad_idx = 0;
  243. return 0;
  244. }
  245. static int configure_output_video_filter(FilterGraph *fg, OutputFilter *ofilter, AVFilterInOut *out)
  246. {
  247. char *pix_fmts;
  248. OutputStream *ost = ofilter->ost;
  249. OutputFile *of = output_files[ost->file_index];
  250. AVCodecContext *codec = ost->enc_ctx;
  251. AVFilterContext *last_filter = out->filter_ctx;
  252. int pad_idx = out->pad_idx;
  253. int ret;
  254. char name[255];
  255. snprintf(name, sizeof(name), "output stream %d:%d", ost->file_index, ost->index);
  256. ret = avfilter_graph_create_filter(&ofilter->filter,
  257. avfilter_get_by_name("buffersink"),
  258. name, NULL, NULL, fg->graph);
  259. if (ret < 0)
  260. return ret;
  261. if (codec->width || codec->height) {
  262. char args[255];
  263. AVFilterContext *filter;
  264. snprintf(args, sizeof(args), "%d:%d:0x%X",
  265. codec->width,
  266. codec->height,
  267. (unsigned)ost->sws_flags);
  268. snprintf(name, sizeof(name), "scaler for output stream %d:%d",
  269. ost->file_index, ost->index);
  270. if ((ret = avfilter_graph_create_filter(&filter, avfilter_get_by_name("scale"),
  271. name, args, NULL, fg->graph)) < 0)
  272. return ret;
  273. if ((ret = avfilter_link(last_filter, pad_idx, filter, 0)) < 0)
  274. return ret;
  275. last_filter = filter;
  276. pad_idx = 0;
  277. }
  278. if ((pix_fmts = choose_pix_fmts(ost))) {
  279. AVFilterContext *filter;
  280. snprintf(name, sizeof(name), "pixel format for output stream %d:%d",
  281. ost->file_index, ost->index);
  282. ret = avfilter_graph_create_filter(&filter,
  283. avfilter_get_by_name("format"),
  284. "format", pix_fmts, NULL, fg->graph);
  285. av_freep(&pix_fmts);
  286. if (ret < 0)
  287. return ret;
  288. if ((ret = avfilter_link(last_filter, pad_idx, filter, 0)) < 0)
  289. return ret;
  290. last_filter = filter;
  291. pad_idx = 0;
  292. }
  293. if (ost->frame_rate.num) {
  294. AVFilterContext *fps;
  295. char args[255];
  296. snprintf(args, sizeof(args), "fps=%d/%d", ost->frame_rate.num,
  297. ost->frame_rate.den);
  298. snprintf(name, sizeof(name), "fps for output stream %d:%d",
  299. ost->file_index, ost->index);
  300. ret = avfilter_graph_create_filter(&fps, avfilter_get_by_name("fps"),
  301. name, args, NULL, fg->graph);
  302. if (ret < 0)
  303. return ret;
  304. ret = avfilter_link(last_filter, pad_idx, fps, 0);
  305. if (ret < 0)
  306. return ret;
  307. last_filter = fps;
  308. pad_idx = 0;
  309. }
  310. snprintf(name, sizeof(name), "trim for output stream %d:%d",
  311. ost->file_index, ost->index);
  312. ret = insert_trim(of->start_time, of->recording_time,
  313. &last_filter, &pad_idx, name);
  314. if (ret < 0)
  315. return ret;
  316. if ((ret = avfilter_link(last_filter, pad_idx, ofilter->filter, 0)) < 0)
  317. return ret;
  318. return 0;
  319. }
  320. static int configure_output_audio_filter(FilterGraph *fg, OutputFilter *ofilter, AVFilterInOut *out)
  321. {
  322. OutputStream *ost = ofilter->ost;
  323. OutputFile *of = output_files[ost->file_index];
  324. AVCodecContext *codec = ost->enc_ctx;
  325. AVFilterContext *last_filter = out->filter_ctx;
  326. int pad_idx = out->pad_idx;
  327. char *sample_fmts, *sample_rates, *channel_layouts;
  328. char name[255];
  329. int ret;
  330. snprintf(name, sizeof(name), "output stream %d:%d", ost->file_index, ost->index);
  331. ret = avfilter_graph_create_filter(&ofilter->filter,
  332. avfilter_get_by_name("abuffersink"),
  333. name, NULL, NULL, fg->graph);
  334. if (ret < 0)
  335. return ret;
  336. if (codec->channels && !codec->channel_layout)
  337. codec->channel_layout = av_get_default_channel_layout(codec->channels);
  338. sample_fmts = choose_sample_fmts(ost);
  339. sample_rates = choose_sample_rates(ost);
  340. channel_layouts = choose_channel_layouts(ost);
  341. if (sample_fmts || sample_rates || channel_layouts) {
  342. AVFilterContext *format;
  343. char args[256];
  344. int len = 0;
  345. if (sample_fmts)
  346. len += snprintf(args + len, sizeof(args) - len, "sample_fmts=%s:",
  347. sample_fmts);
  348. if (sample_rates)
  349. len += snprintf(args + len, sizeof(args) - len, "sample_rates=%s:",
  350. sample_rates);
  351. if (channel_layouts)
  352. len += snprintf(args + len, sizeof(args) - len, "channel_layouts=%s:",
  353. channel_layouts);
  354. args[len - 1] = 0;
  355. av_freep(&sample_fmts);
  356. av_freep(&sample_rates);
  357. av_freep(&channel_layouts);
  358. snprintf(name, sizeof(name), "audio format for output stream %d:%d",
  359. ost->file_index, ost->index);
  360. ret = avfilter_graph_create_filter(&format,
  361. avfilter_get_by_name("aformat"),
  362. name, args, NULL, fg->graph);
  363. if (ret < 0)
  364. return ret;
  365. ret = avfilter_link(last_filter, pad_idx, format, 0);
  366. if (ret < 0)
  367. return ret;
  368. last_filter = format;
  369. pad_idx = 0;
  370. }
  371. snprintf(name, sizeof(name), "trim for output stream %d:%d",
  372. ost->file_index, ost->index);
  373. ret = insert_trim(of->start_time, of->recording_time,
  374. &last_filter, &pad_idx, name);
  375. if (ret < 0)
  376. return ret;
  377. if ((ret = avfilter_link(last_filter, pad_idx, ofilter->filter, 0)) < 0)
  378. return ret;
  379. return 0;
  380. }
  381. #define DESCRIBE_FILTER_LINK(f, inout, in) \
  382. { \
  383. AVFilterContext *ctx = inout->filter_ctx; \
  384. AVFilterPad *pads = in ? ctx->input_pads : ctx->output_pads; \
  385. int nb_pads = in ? ctx->nb_inputs : ctx->nb_outputs; \
  386. AVIOContext *pb; \
  387. \
  388. if (avio_open_dyn_buf(&pb) < 0) \
  389. exit(1); \
  390. \
  391. avio_printf(pb, "%s", ctx->filter->name); \
  392. if (nb_pads > 1) \
  393. avio_printf(pb, ":%s", avfilter_pad_get_name(pads, inout->pad_idx));\
  394. avio_w8(pb, 0); \
  395. avio_close_dyn_buf(pb, &f->name); \
  396. }
  397. int configure_output_filter(FilterGraph *fg, OutputFilter *ofilter, AVFilterInOut *out)
  398. {
  399. av_freep(&ofilter->name);
  400. DESCRIBE_FILTER_LINK(ofilter, out, 0);
  401. switch (avfilter_pad_get_type(out->filter_ctx->output_pads, out->pad_idx)) {
  402. case AVMEDIA_TYPE_VIDEO: return configure_output_video_filter(fg, ofilter, out);
  403. case AVMEDIA_TYPE_AUDIO: return configure_output_audio_filter(fg, ofilter, out);
  404. default: av_assert0(0);
  405. }
  406. }
  407. static int configure_input_video_filter(FilterGraph *fg, InputFilter *ifilter,
  408. AVFilterInOut *in)
  409. {
  410. AVFilterContext *last_filter;
  411. const AVFilter *buffer_filt = avfilter_get_by_name("buffer");
  412. InputStream *ist = ifilter->ist;
  413. InputFile *f = input_files[ist->file_index];
  414. AVRational tb = ist->framerate.num ? av_inv_q(ist->framerate) :
  415. ist->st->time_base;
  416. AVRational sar;
  417. char args[255], name[255];
  418. int ret, pad_idx = 0;
  419. sar = ist->st->sample_aspect_ratio.num ?
  420. ist->st->sample_aspect_ratio :
  421. ist->dec_ctx->sample_aspect_ratio;
  422. snprintf(args, sizeof(args), "%d:%d:%d:%d:%d:%d:%d", ist->dec_ctx->width,
  423. ist->dec_ctx->height,
  424. ist->hwaccel_retrieve_data ? ist->hwaccel_retrieved_pix_fmt : ist->dec_ctx->pix_fmt,
  425. tb.num, tb.den, sar.num, sar.den);
  426. snprintf(name, sizeof(name), "graph %d input from stream %d:%d", fg->index,
  427. ist->file_index, ist->st->index);
  428. if ((ret = avfilter_graph_create_filter(&ifilter->filter, buffer_filt, name,
  429. args, NULL, fg->graph)) < 0)
  430. return ret;
  431. last_filter = ifilter->filter;
  432. if (ist->autorotate) {
  433. uint8_t* displaymatrix = av_stream_get_side_data(ist->st,
  434. AV_PKT_DATA_DISPLAYMATRIX, NULL);
  435. if (displaymatrix) {
  436. double rot = av_display_rotation_get((int32_t*) displaymatrix);
  437. if (rot < -135 || rot > 135) {
  438. ret = insert_filter(&last_filter, &pad_idx, "vflip", NULL);
  439. if (ret < 0)
  440. return ret;
  441. ret = insert_filter(&last_filter, &pad_idx, "hflip", NULL);
  442. } else if (rot < -45) {
  443. ret = insert_filter(&last_filter, &pad_idx, "transpose", "dir=clock");
  444. } else if (rot > 45) {
  445. ret = insert_filter(&last_filter, &pad_idx, "transpose", "dir=cclock");
  446. }
  447. if (ret < 0)
  448. return ret;
  449. }
  450. }
  451. if (ist->framerate.num) {
  452. AVFilterContext *setpts;
  453. snprintf(name, sizeof(name), "force CFR for input from stream %d:%d",
  454. ist->file_index, ist->st->index);
  455. if ((ret = avfilter_graph_create_filter(&setpts,
  456. avfilter_get_by_name("setpts"),
  457. name, "N", NULL,
  458. fg->graph)) < 0)
  459. return ret;
  460. if ((ret = avfilter_link(last_filter, 0, setpts, 0)) < 0)
  461. return ret;
  462. last_filter = setpts;
  463. }
  464. snprintf(name, sizeof(name), "trim for input stream %d:%d",
  465. ist->file_index, ist->st->index);
  466. ret = insert_trim(((f->start_time == AV_NOPTS_VALUE) || !f->accurate_seek) ?
  467. AV_NOPTS_VALUE : 0, f->recording_time, &last_filter, &pad_idx, name);
  468. if (ret < 0)
  469. return ret;
  470. if ((ret = avfilter_link(last_filter, 0, in->filter_ctx, in->pad_idx)) < 0)
  471. return ret;
  472. return 0;
  473. }
  474. static int configure_input_audio_filter(FilterGraph *fg, InputFilter *ifilter,
  475. AVFilterInOut *in)
  476. {
  477. AVFilterContext *last_filter;
  478. const AVFilter *abuffer_filt = avfilter_get_by_name("abuffer");
  479. InputStream *ist = ifilter->ist;
  480. InputFile *f = input_files[ist->file_index];
  481. char args[255], name[255];
  482. int ret, pad_idx = 0;
  483. snprintf(args, sizeof(args), "time_base=%d/%d:sample_rate=%d:sample_fmt=%s"
  484. ":channel_layout=0x%"PRIx64,
  485. 1, ist->dec_ctx->sample_rate,
  486. ist->dec_ctx->sample_rate,
  487. av_get_sample_fmt_name(ist->dec_ctx->sample_fmt),
  488. ist->dec_ctx->channel_layout);
  489. snprintf(name, sizeof(name), "graph %d input from stream %d:%d", fg->index,
  490. ist->file_index, ist->st->index);
  491. if ((ret = avfilter_graph_create_filter(&ifilter->filter, abuffer_filt,
  492. name, args, NULL,
  493. fg->graph)) < 0)
  494. return ret;
  495. last_filter = ifilter->filter;
  496. if (audio_sync_method > 0) {
  497. AVFilterContext *async;
  498. int len = 0;
  499. av_log(NULL, AV_LOG_WARNING, "-async has been deprecated. Used the "
  500. "asyncts audio filter instead.\n");
  501. if (audio_sync_method > 1)
  502. len += snprintf(args + len, sizeof(args) - len, "compensate=1:"
  503. "max_comp=%d:", audio_sync_method);
  504. snprintf(args + len, sizeof(args) - len, "min_delta=%f",
  505. audio_drift_threshold);
  506. snprintf(name, sizeof(name), "graph %d audio sync for input stream %d:%d",
  507. fg->index, ist->file_index, ist->st->index);
  508. ret = avfilter_graph_create_filter(&async,
  509. avfilter_get_by_name("asyncts"),
  510. name, args, NULL, fg->graph);
  511. if (ret < 0)
  512. return ret;
  513. ret = avfilter_link(last_filter, 0, async, 0);
  514. if (ret < 0)
  515. return ret;
  516. last_filter = async;
  517. }
  518. if (audio_volume != 256) {
  519. AVFilterContext *volume;
  520. av_log(NULL, AV_LOG_WARNING, "-vol has been deprecated. Use the volume "
  521. "audio filter instead.\n");
  522. snprintf(args, sizeof(args), "volume=%f", audio_volume / 256.0);
  523. snprintf(name, sizeof(name), "graph %d volume for input stream %d:%d",
  524. fg->index, ist->file_index, ist->st->index);
  525. ret = avfilter_graph_create_filter(&volume,
  526. avfilter_get_by_name("volume"),
  527. name, args, NULL, fg->graph);
  528. if (ret < 0)
  529. return ret;
  530. ret = avfilter_link(last_filter, 0, volume, 0);
  531. if (ret < 0)
  532. return ret;
  533. last_filter = volume;
  534. }
  535. snprintf(name, sizeof(name), "trim for input stream %d:%d",
  536. ist->file_index, ist->st->index);
  537. ret = insert_trim(((f->start_time == AV_NOPTS_VALUE) || !f->accurate_seek) ?
  538. AV_NOPTS_VALUE : 0, f->recording_time, &last_filter, &pad_idx, name);
  539. if (ret < 0)
  540. return ret;
  541. if ((ret = avfilter_link(last_filter, 0, in->filter_ctx, in->pad_idx)) < 0)
  542. return ret;
  543. return 0;
  544. }
  545. static int configure_input_filter(FilterGraph *fg, InputFilter *ifilter,
  546. AVFilterInOut *in)
  547. {
  548. av_freep(&ifilter->name);
  549. DESCRIBE_FILTER_LINK(ifilter, in, 1);
  550. switch (avfilter_pad_get_type(in->filter_ctx->input_pads, in->pad_idx)) {
  551. case AVMEDIA_TYPE_VIDEO: return configure_input_video_filter(fg, ifilter, in);
  552. case AVMEDIA_TYPE_AUDIO: return configure_input_audio_filter(fg, ifilter, in);
  553. default: av_assert0(0);
  554. }
  555. }
  556. int configure_filtergraph(FilterGraph *fg)
  557. {
  558. AVFilterInOut *inputs, *outputs, *cur;
  559. int ret, i, simple = !fg->graph_desc;
  560. const char *graph_desc = simple ? fg->outputs[0]->ost->avfilter :
  561. fg->graph_desc;
  562. avfilter_graph_free(&fg->graph);
  563. if (!(fg->graph = avfilter_graph_alloc()))
  564. return AVERROR(ENOMEM);
  565. if (simple) {
  566. OutputStream *ost = fg->outputs[0]->ost;
  567. char args[512];
  568. AVDictionaryEntry *e = NULL;
  569. snprintf(args, sizeof(args), "flags=0x%X", (unsigned)ost->sws_flags);
  570. fg->graph->scale_sws_opts = av_strdup(args);
  571. args[0] = '\0';
  572. while ((e = av_dict_get(fg->outputs[0]->ost->resample_opts, "", e,
  573. AV_DICT_IGNORE_SUFFIX))) {
  574. av_strlcatf(args, sizeof(args), "%s=%s:", e->key, e->value);
  575. }
  576. if (strlen(args))
  577. args[strlen(args) - 1] = '\0';
  578. fg->graph->resample_lavr_opts = av_strdup(args);
  579. }
  580. if ((ret = avfilter_graph_parse2(fg->graph, graph_desc, &inputs, &outputs)) < 0)
  581. return ret;
  582. if (simple && (!inputs || inputs->next || !outputs || outputs->next)) {
  583. av_log(NULL, AV_LOG_ERROR, "Simple filtergraph '%s' does not have "
  584. "exactly one input and output.\n", graph_desc);
  585. return AVERROR(EINVAL);
  586. }
  587. for (cur = inputs, i = 0; cur; cur = cur->next, i++)
  588. if ((ret = configure_input_filter(fg, fg->inputs[i], cur)) < 0)
  589. return ret;
  590. avfilter_inout_free(&inputs);
  591. for (cur = outputs, i = 0; cur; cur = cur->next, i++)
  592. configure_output_filter(fg, fg->outputs[i], cur);
  593. avfilter_inout_free(&outputs);
  594. if ((ret = avfilter_graph_config(fg->graph, NULL)) < 0)
  595. return ret;
  596. return 0;
  597. }
  598. int ist_in_filtergraph(FilterGraph *fg, InputStream *ist)
  599. {
  600. int i;
  601. for (i = 0; i < fg->nb_inputs; i++)
  602. if (fg->inputs[i]->ist == ist)
  603. return 1;
  604. return 0;
  605. }