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.

810 lines
28KB

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