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.

763 lines
27KB

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