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.

827 lines
29KB

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