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.

800 lines
32KB

  1. /*
  2. * ffmpeg filter configuration
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg 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. * FFmpeg 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 FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include "ffmpeg.h"
  21. #include "libavfilter/avfilter.h"
  22. #include "libavfilter/avfiltergraph.h"
  23. #include "libavfilter/buffersink.h"
  24. #include "libavutil/avassert.h"
  25. #include "libavutil/avstring.h"
  26. #include "libavutil/bprint.h"
  27. #include "libavutil/channel_layout.h"
  28. #include "libavutil/opt.h"
  29. #include "libavutil/pixdesc.h"
  30. #include "libavutil/pixfmt.h"
  31. #include "libavutil/imgutils.h"
  32. #include "libavutil/samplefmt.h"
  33. enum AVPixelFormat choose_pixel_fmt(AVStream *st, AVCodec *codec, enum AVPixelFormat target)
  34. {
  35. if (codec && codec->pix_fmts) {
  36. const enum AVPixelFormat *p = codec->pix_fmts;
  37. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(target);
  38. int has_alpha = desc ? desc->nb_components % 2 == 0 : 0;
  39. enum AVPixelFormat best= AV_PIX_FMT_NONE;
  40. if (st->codec->strict_std_compliance <= FF_COMPLIANCE_UNOFFICIAL) {
  41. if (st->codec->codec_id == AV_CODEC_ID_MJPEG) {
  42. p = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_NONE };
  43. } else if (st->codec->codec_id == AV_CODEC_ID_LJPEG) {
  44. p = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUV420P,
  45. AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_BGRA, AV_PIX_FMT_NONE };
  46. }
  47. }
  48. for (; *p != AV_PIX_FMT_NONE; p++) {
  49. best= avcodec_find_best_pix_fmt_of_2(best, *p, target, has_alpha, NULL);
  50. if (*p == target)
  51. break;
  52. }
  53. if (*p == AV_PIX_FMT_NONE) {
  54. if (target != AV_PIX_FMT_NONE)
  55. av_log(NULL, AV_LOG_WARNING,
  56. "Incompatible pixel format '%s' for codec '%s', auto-selecting format '%s'\n",
  57. av_get_pix_fmt_name(target),
  58. codec->name,
  59. av_get_pix_fmt_name(best));
  60. return best;
  61. }
  62. }
  63. return target;
  64. }
  65. void choose_sample_fmt(AVStream *st, AVCodec *codec)
  66. {
  67. if (codec && codec->sample_fmts) {
  68. const enum AVSampleFormat *p = codec->sample_fmts;
  69. for (; *p != -1; p++) {
  70. if (*p == st->codec->sample_fmt)
  71. break;
  72. }
  73. if (*p == -1) {
  74. if((codec->capabilities & CODEC_CAP_LOSSLESS) && av_get_sample_fmt_name(st->codec->sample_fmt) > av_get_sample_fmt_name(codec->sample_fmts[0]))
  75. av_log(NULL, AV_LOG_ERROR, "Conversion will not be lossless.\n");
  76. if(av_get_sample_fmt_name(st->codec->sample_fmt))
  77. av_log(NULL, AV_LOG_WARNING,
  78. "Incompatible sample format '%s' for codec '%s', auto-selecting format '%s'\n",
  79. av_get_sample_fmt_name(st->codec->sample_fmt),
  80. codec->name,
  81. av_get_sample_fmt_name(codec->sample_fmts[0]));
  82. st->codec->sample_fmt = codec->sample_fmts[0];
  83. }
  84. }
  85. }
  86. static char *choose_pix_fmts(OutputStream *ost)
  87. {
  88. if (ost->keep_pix_fmt) {
  89. if (ost->filter)
  90. avfilter_graph_set_auto_convert(ost->filter->graph->graph,
  91. AVFILTER_AUTO_CONVERT_NONE);
  92. if (ost->st->codec->pix_fmt == AV_PIX_FMT_NONE)
  93. return NULL;
  94. return av_strdup(av_get_pix_fmt_name(ost->st->codec->pix_fmt));
  95. }
  96. if (ost->st->codec->pix_fmt != AV_PIX_FMT_NONE) {
  97. return av_strdup(av_get_pix_fmt_name(choose_pixel_fmt(ost->st, ost->enc, ost->st->codec->pix_fmt)));
  98. } else if (ost->enc && ost->enc->pix_fmts) {
  99. const enum AVPixelFormat *p;
  100. AVIOContext *s = NULL;
  101. uint8_t *ret;
  102. int len;
  103. if (avio_open_dyn_buf(&s) < 0)
  104. exit(1);
  105. p = ost->enc->pix_fmts;
  106. if (ost->st->codec->strict_std_compliance <= FF_COMPLIANCE_UNOFFICIAL) {
  107. if (ost->st->codec->codec_id == AV_CODEC_ID_MJPEG) {
  108. p = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_NONE };
  109. } else if (ost->st->codec->codec_id == AV_CODEC_ID_LJPEG) {
  110. p = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUV420P,
  111. AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_BGRA, AV_PIX_FMT_NONE };
  112. }
  113. }
  114. for (; *p != AV_PIX_FMT_NONE; p++) {
  115. const char *name = av_get_pix_fmt_name(*p);
  116. avio_printf(s, "%s:", name);
  117. }
  118. len = avio_close_dyn_buf(s, &ret);
  119. ret[len - 1] = 0;
  120. return ret;
  121. } else
  122. return NULL;
  123. }
  124. /* Define a function for building a string containing a list of
  125. * allowed formats. */
  126. #define DEF_CHOOSE_FORMAT(type, var, supported_list, none, get_name, separator)\
  127. static char *choose_ ## var ## s(OutputStream *ost) \
  128. { \
  129. if (ost->st->codec->var != none) { \
  130. get_name(ost->st->codec->var); \
  131. return av_strdup(name); \
  132. } else if (ost->enc->supported_list) { \
  133. const type *p; \
  134. AVIOContext *s = NULL; \
  135. uint8_t *ret; \
  136. int len; \
  137. \
  138. if (avio_open_dyn_buf(&s) < 0) \
  139. exit(1); \
  140. \
  141. for (p = ost->enc->supported_list; *p != none; p++) { \
  142. get_name(*p); \
  143. avio_printf(s, "%s" separator, name); \
  144. } \
  145. len = avio_close_dyn_buf(s, &ret); \
  146. ret[len - 1] = 0; \
  147. return ret; \
  148. } else \
  149. return NULL; \
  150. }
  151. // DEF_CHOOSE_FORMAT(enum AVPixelFormat, pix_fmt, pix_fmts, AV_PIX_FMT_NONE,
  152. // GET_PIX_FMT_NAME, ":")
  153. DEF_CHOOSE_FORMAT(enum AVSampleFormat, sample_fmt, sample_fmts,
  154. AV_SAMPLE_FMT_NONE, GET_SAMPLE_FMT_NAME, ",")
  155. DEF_CHOOSE_FORMAT(int, sample_rate, supported_samplerates, 0,
  156. GET_SAMPLE_RATE_NAME, ",")
  157. DEF_CHOOSE_FORMAT(uint64_t, channel_layout, channel_layouts, 0,
  158. GET_CH_LAYOUT_NAME, ",")
  159. FilterGraph *init_simple_filtergraph(InputStream *ist, OutputStream *ost)
  160. {
  161. FilterGraph *fg = av_mallocz(sizeof(*fg));
  162. if (!fg)
  163. exit(1);
  164. fg->index = nb_filtergraphs;
  165. GROW_ARRAY(fg->outputs, fg->nb_outputs);
  166. if (!(fg->outputs[0] = av_mallocz(sizeof(*fg->outputs[0]))))
  167. exit(1);
  168. fg->outputs[0]->ost = ost;
  169. fg->outputs[0]->graph = fg;
  170. ost->filter = fg->outputs[0];
  171. GROW_ARRAY(fg->inputs, fg->nb_inputs);
  172. if (!(fg->inputs[0] = av_mallocz(sizeof(*fg->inputs[0]))))
  173. exit(1);
  174. fg->inputs[0]->ist = ist;
  175. fg->inputs[0]->graph = fg;
  176. GROW_ARRAY(ist->filters, ist->nb_filters);
  177. ist->filters[ist->nb_filters - 1] = fg->inputs[0];
  178. GROW_ARRAY(filtergraphs, nb_filtergraphs);
  179. filtergraphs[nb_filtergraphs - 1] = fg;
  180. return fg;
  181. }
  182. static void init_input_filter(FilterGraph *fg, AVFilterInOut *in)
  183. {
  184. InputStream *ist = NULL;
  185. enum AVMediaType type = avfilter_pad_get_type(in->filter_ctx->input_pads, in->pad_idx);
  186. int i;
  187. // TODO: support other filter types
  188. if (type != AVMEDIA_TYPE_VIDEO && type != AVMEDIA_TYPE_AUDIO) {
  189. av_log(NULL, AV_LOG_FATAL, "Only video and audio filters supported "
  190. "currently.\n");
  191. exit(1);
  192. }
  193. if (in->name) {
  194. AVFormatContext *s;
  195. AVStream *st = NULL;
  196. char *p;
  197. int file_idx = strtol(in->name, &p, 0);
  198. if (file_idx < 0 || file_idx >= nb_input_files) {
  199. av_log(NULL, AV_LOG_FATAL, "Invalid file index %d in filtergraph description %s.\n",
  200. file_idx, fg->graph_desc);
  201. exit(1);
  202. }
  203. s = input_files[file_idx]->ctx;
  204. for (i = 0; i < s->nb_streams; i++) {
  205. enum AVMediaType stream_type = s->streams[i]->codec->codec_type;
  206. if (stream_type != type &&
  207. !(stream_type == AVMEDIA_TYPE_SUBTITLE &&
  208. type == AVMEDIA_TYPE_VIDEO /* sub2video hack */))
  209. continue;
  210. if (check_stream_specifier(s, s->streams[i], *p == ':' ? p + 1 : p) == 1) {
  211. st = s->streams[i];
  212. break;
  213. }
  214. }
  215. if (!st) {
  216. av_log(NULL, AV_LOG_FATAL, "Stream specifier '%s' in filtergraph description %s "
  217. "matches no streams.\n", p, fg->graph_desc);
  218. exit(1);
  219. }
  220. ist = input_streams[input_files[file_idx]->ist_index + st->index];
  221. } else {
  222. /* find the first unused stream of corresponding type */
  223. for (i = 0; i < nb_input_streams; i++) {
  224. ist = input_streams[i];
  225. if (ist->st->codec->codec_type == type && ist->discard)
  226. break;
  227. }
  228. if (i == nb_input_streams) {
  229. av_log(NULL, AV_LOG_FATAL, "Cannot find a matching stream for "
  230. "unlabeled input pad %d on filter %s\n", in->pad_idx,
  231. in->filter_ctx->name);
  232. exit(1);
  233. }
  234. }
  235. av_assert0(ist);
  236. ist->discard = 0;
  237. ist->decoding_needed++;
  238. ist->st->discard = AVDISCARD_NONE;
  239. GROW_ARRAY(fg->inputs, fg->nb_inputs);
  240. if (!(fg->inputs[fg->nb_inputs - 1] = av_mallocz(sizeof(*fg->inputs[0]))))
  241. exit(1);
  242. fg->inputs[fg->nb_inputs - 1]->ist = ist;
  243. fg->inputs[fg->nb_inputs - 1]->graph = fg;
  244. GROW_ARRAY(ist->filters, ist->nb_filters);
  245. ist->filters[ist->nb_filters - 1] = fg->inputs[fg->nb_inputs - 1];
  246. }
  247. static int configure_output_video_filter(FilterGraph *fg, OutputFilter *ofilter, AVFilterInOut *out)
  248. {
  249. char *pix_fmts;
  250. OutputStream *ost = ofilter->ost;
  251. AVCodecContext *codec = ost->st->codec;
  252. AVFilterContext *last_filter = out->filter_ctx;
  253. int pad_idx = out->pad_idx;
  254. int ret;
  255. char name[255];
  256. AVBufferSinkParams *buffersink_params = av_buffersink_params_alloc();
  257. snprintf(name, sizeof(name), "output stream %d:%d", ost->file_index, ost->index);
  258. ret = avfilter_graph_create_filter(&ofilter->filter,
  259. avfilter_get_by_name("ffbuffersink"),
  260. name, NULL, NULL, fg->graph);
  261. av_freep(&buffersink_params);
  262. if (ret < 0)
  263. return ret;
  264. if (codec->width || codec->height) {
  265. char args[255];
  266. AVFilterContext *filter;
  267. snprintf(args, sizeof(args), "%d:%d:flags=0x%X",
  268. codec->width,
  269. codec->height,
  270. (unsigned)ost->sws_flags);
  271. snprintf(name, sizeof(name), "scaler for output stream %d:%d",
  272. ost->file_index, ost->index);
  273. if ((ret = avfilter_graph_create_filter(&filter, avfilter_get_by_name("scale"),
  274. name, args, NULL, fg->graph)) < 0)
  275. return ret;
  276. if ((ret = avfilter_link(last_filter, pad_idx, filter, 0)) < 0)
  277. return ret;
  278. last_filter = filter;
  279. pad_idx = 0;
  280. }
  281. if ((pix_fmts = choose_pix_fmts(ost))) {
  282. AVFilterContext *filter;
  283. snprintf(name, sizeof(name), "pixel format for output stream %d:%d",
  284. ost->file_index, ost->index);
  285. if ((ret = avfilter_graph_create_filter(&filter,
  286. avfilter_get_by_name("format"),
  287. "format", pix_fmts, NULL,
  288. fg->graph)) < 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. av_freep(&pix_fmts);
  295. }
  296. if (ost->frame_rate.num && 0) {
  297. AVFilterContext *fps;
  298. char args[255];
  299. snprintf(args, sizeof(args), "fps=%d/%d", ost->frame_rate.num,
  300. ost->frame_rate.den);
  301. snprintf(name, sizeof(name), "fps for output stream %d:%d",
  302. ost->file_index, ost->index);
  303. ret = avfilter_graph_create_filter(&fps, avfilter_get_by_name("fps"),
  304. name, args, NULL, fg->graph);
  305. if (ret < 0)
  306. return ret;
  307. ret = avfilter_link(last_filter, pad_idx, fps, 0);
  308. if (ret < 0)
  309. return ret;
  310. last_filter = fps;
  311. pad_idx = 0;
  312. }
  313. if ((ret = avfilter_link(last_filter, pad_idx, ofilter->filter, 0)) < 0)
  314. return ret;
  315. return 0;
  316. }
  317. static int configure_output_audio_filter(FilterGraph *fg, OutputFilter *ofilter, AVFilterInOut *out)
  318. {
  319. OutputStream *ost = ofilter->ost;
  320. AVCodecContext *codec = ost->st->codec;
  321. AVFilterContext *last_filter = out->filter_ctx;
  322. int pad_idx = out->pad_idx;
  323. char *sample_fmts, *sample_rates, *channel_layouts;
  324. char name[255];
  325. int ret;
  326. AVABufferSinkParams *params = av_abuffersink_params_alloc();
  327. if (!params)
  328. return AVERROR(ENOMEM);
  329. params->all_channel_counts = 1;
  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("ffabuffersink"),
  333. name, NULL, params, fg->graph);
  334. av_freep(&params);
  335. if (ret < 0)
  336. return ret;
  337. #define AUTO_INSERT_FILTER(opt_name, filter_name, arg) do { \
  338. AVFilterContext *filt_ctx; \
  339. \
  340. av_log(NULL, AV_LOG_INFO, opt_name " is forwarded to lavfi " \
  341. "similarly to -af " filter_name "=%s.\n", arg); \
  342. \
  343. ret = avfilter_graph_create_filter(&filt_ctx, \
  344. avfilter_get_by_name(filter_name), \
  345. filter_name, arg, NULL, fg->graph); \
  346. if (ret < 0) \
  347. return ret; \
  348. \
  349. ret = avfilter_link(last_filter, pad_idx, filt_ctx, 0); \
  350. if (ret < 0) \
  351. return ret; \
  352. \
  353. last_filter = filt_ctx; \
  354. pad_idx = 0; \
  355. } while (0)
  356. if (ost->audio_channels_mapped) {
  357. int i;
  358. AVBPrint pan_buf;
  359. av_bprint_init(&pan_buf, 256, 8192);
  360. av_bprintf(&pan_buf, "0x%"PRIx64,
  361. av_get_default_channel_layout(ost->audio_channels_mapped));
  362. for (i = 0; i < ost->audio_channels_mapped; i++)
  363. if (ost->audio_channels_map[i] != -1)
  364. av_bprintf(&pan_buf, ":c%d=c%d", i, ost->audio_channels_map[i]);
  365. AUTO_INSERT_FILTER("-map_channel", "pan", pan_buf.str);
  366. av_bprint_finalize(&pan_buf, NULL);
  367. }
  368. if (codec->channels && !codec->channel_layout)
  369. codec->channel_layout = av_get_default_channel_layout(codec->channels);
  370. sample_fmts = choose_sample_fmts(ost);
  371. sample_rates = choose_sample_rates(ost);
  372. channel_layouts = choose_channel_layouts(ost);
  373. if (sample_fmts || sample_rates || channel_layouts) {
  374. AVFilterContext *format;
  375. char args[256];
  376. args[0] = 0;
  377. if (sample_fmts)
  378. av_strlcatf(args, sizeof(args), "sample_fmts=%s:",
  379. sample_fmts);
  380. if (sample_rates)
  381. av_strlcatf(args, sizeof(args), "sample_rates=%s:",
  382. sample_rates);
  383. if (channel_layouts)
  384. av_strlcatf(args, sizeof(args), "channel_layouts=%s:",
  385. channel_layouts);
  386. av_freep(&sample_fmts);
  387. av_freep(&sample_rates);
  388. av_freep(&channel_layouts);
  389. snprintf(name, sizeof(name), "audio format for output stream %d:%d",
  390. ost->file_index, ost->index);
  391. ret = avfilter_graph_create_filter(&format,
  392. avfilter_get_by_name("aformat"),
  393. name, args, NULL, fg->graph);
  394. if (ret < 0)
  395. return ret;
  396. ret = avfilter_link(last_filter, pad_idx, format, 0);
  397. if (ret < 0)
  398. return ret;
  399. last_filter = format;
  400. pad_idx = 0;
  401. }
  402. if (audio_volume != 256 && 0) {
  403. char args[256];
  404. snprintf(args, sizeof(args), "%f", audio_volume / 256.);
  405. AUTO_INSERT_FILTER("-vol", "volume", args);
  406. }
  407. if ((ret = avfilter_link(last_filter, pad_idx, ofilter->filter, 0)) < 0)
  408. return ret;
  409. return 0;
  410. }
  411. #define DESCRIBE_FILTER_LINK(f, inout, in) \
  412. { \
  413. AVFilterContext *ctx = inout->filter_ctx; \
  414. AVFilterPad *pads = in ? ctx->input_pads : ctx->output_pads; \
  415. int nb_pads = in ? ctx->input_count : ctx->output_count; \
  416. AVIOContext *pb; \
  417. \
  418. if (avio_open_dyn_buf(&pb) < 0) \
  419. exit(1); \
  420. \
  421. avio_printf(pb, "%s", ctx->filter->name); \
  422. if (nb_pads > 1) \
  423. avio_printf(pb, ":%s", avfilter_pad_get_name(pads, inout->pad_idx));\
  424. avio_w8(pb, 0); \
  425. avio_close_dyn_buf(pb, &f->name); \
  426. }
  427. int configure_output_filter(FilterGraph *fg, OutputFilter *ofilter, AVFilterInOut *out)
  428. {
  429. av_freep(&ofilter->name);
  430. DESCRIBE_FILTER_LINK(ofilter, out, 0);
  431. switch (avfilter_pad_get_type(out->filter_ctx->output_pads, out->pad_idx)) {
  432. case AVMEDIA_TYPE_VIDEO: return configure_output_video_filter(fg, ofilter, out);
  433. case AVMEDIA_TYPE_AUDIO: return configure_output_audio_filter(fg, ofilter, out);
  434. default: av_assert0(0);
  435. }
  436. }
  437. static int sub2video_prepare(InputStream *ist)
  438. {
  439. AVFormatContext *avf = input_files[ist->file_index]->ctx;
  440. int i, ret, w, h;
  441. uint8_t *image[4];
  442. int linesize[4];
  443. /* Compute the size of the canvas for the subtitles stream.
  444. If the subtitles codec has set a size, use it. Otherwise use the
  445. maximum dimensions of the video streams in the same file. */
  446. w = ist->st->codec->width;
  447. h = ist->st->codec->height;
  448. if (!(w && h)) {
  449. for (i = 0; i < avf->nb_streams; i++) {
  450. if (avf->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
  451. w = FFMAX(w, avf->streams[i]->codec->width);
  452. h = FFMAX(h, avf->streams[i]->codec->height);
  453. }
  454. }
  455. if (!(w && h)) {
  456. w = FFMAX(w, 720);
  457. h = FFMAX(h, 576);
  458. }
  459. av_log(avf, AV_LOG_INFO, "sub2video: using %dx%d canvas\n", w, h);
  460. }
  461. ist->sub2video.w = ist->st->codec->width = ist->resample_width = w;
  462. ist->sub2video.h = ist->st->codec->height = ist->resample_height = h;
  463. /* rectangles are AV_PIX_FMT_PAL8, but we have no guarantee that the
  464. palettes for all rectangles are identical or compatible */
  465. ist->st->codec->pix_fmt = AV_PIX_FMT_RGB32;
  466. ret = av_image_alloc(image, linesize, w, h, AV_PIX_FMT_RGB32, 32);
  467. if (ret < 0)
  468. return ret;
  469. memset(image[0], 0, h * linesize[0]);
  470. ist->sub2video.ref = avfilter_get_video_buffer_ref_from_arrays(
  471. image, linesize, AV_PERM_READ | AV_PERM_PRESERVE,
  472. w, h, AV_PIX_FMT_RGB32);
  473. if (!ist->sub2video.ref) {
  474. av_free(image[0]);
  475. return AVERROR(ENOMEM);
  476. }
  477. return 0;
  478. }
  479. static int configure_input_video_filter(FilterGraph *fg, InputFilter *ifilter,
  480. AVFilterInOut *in)
  481. {
  482. AVFilterContext *first_filter = in->filter_ctx;
  483. AVFilter *filter = avfilter_get_by_name("buffer");
  484. InputStream *ist = ifilter->ist;
  485. AVRational tb = ist->framerate.num ? av_inv_q(ist->framerate) :
  486. ist->st->time_base;
  487. AVRational fr = ist->framerate.num ? ist->framerate :
  488. ist->st->r_frame_rate;
  489. AVRational sar;
  490. AVBPrint args;
  491. char name[255];
  492. int pad_idx = in->pad_idx;
  493. int ret;
  494. if (!ist->framerate.num && ist->st->codec->ticks_per_frame>1) {
  495. AVRational codec_fr = av_inv_q(ist->st->codec->time_base);
  496. AVRational avg_fr = ist->st->avg_frame_rate;
  497. codec_fr.den *= ist->st->codec->ticks_per_frame;
  498. if ( codec_fr.num>0 && codec_fr.den>0 && av_q2d(codec_fr) < av_q2d(fr)*0.7
  499. && fabs(1.0 - av_q2d(av_div_q(avg_fr, fr)))>0.1)
  500. fr = codec_fr;
  501. }
  502. if (ist->st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE) {
  503. ret = sub2video_prepare(ist);
  504. if (ret < 0)
  505. return ret;
  506. }
  507. sar = ist->st->sample_aspect_ratio.num ?
  508. ist->st->sample_aspect_ratio :
  509. ist->st->codec->sample_aspect_ratio;
  510. if(!sar.den)
  511. sar = (AVRational){0,1};
  512. av_bprint_init(&args, 0, 1);
  513. av_bprintf(&args,
  514. "video_size=%dx%d:pix_fmt=%d:time_base=%d/%d:"
  515. "pixel_aspect=%d/%d:sws_param=flags=%d", ist->resample_width,
  516. ist->resample_height, ist->resample_pix_fmt,
  517. tb.num, tb.den, sar.num, sar.den,
  518. SWS_BILINEAR + ((ist->st->codec->flags&CODEC_FLAG_BITEXACT) ? SWS_BITEXACT:0));
  519. if (fr.num && fr.den)
  520. av_bprintf(&args, ":frame_rate=%d/%d", fr.num, fr.den);
  521. snprintf(name, sizeof(name), "graph %d input from stream %d:%d", fg->index,
  522. ist->file_index, ist->st->index);
  523. if ((ret = avfilter_graph_create_filter(&ifilter->filter, filter, name,
  524. args.str, NULL, fg->graph)) < 0)
  525. return ret;
  526. if (ist->framerate.num) {
  527. AVFilterContext *setpts;
  528. snprintf(name, sizeof(name), "force CFR for input from stream %d:%d",
  529. ist->file_index, ist->st->index);
  530. if ((ret = avfilter_graph_create_filter(&setpts,
  531. avfilter_get_by_name("setpts"),
  532. name, "N", NULL,
  533. fg->graph)) < 0)
  534. return ret;
  535. if ((ret = avfilter_link(setpts, 0, first_filter, pad_idx)) < 0)
  536. return ret;
  537. first_filter = setpts;
  538. pad_idx = 0;
  539. }
  540. if ((ret = avfilter_link(ifilter->filter, 0, first_filter, pad_idx)) < 0)
  541. return ret;
  542. return 0;
  543. }
  544. static int configure_input_audio_filter(FilterGraph *fg, InputFilter *ifilter,
  545. AVFilterInOut *in)
  546. {
  547. AVFilterContext *first_filter = in->filter_ctx;
  548. AVFilter *filter = avfilter_get_by_name("abuffer");
  549. InputStream *ist = ifilter->ist;
  550. int pad_idx = in->pad_idx;
  551. AVBPrint args;
  552. char name[255];
  553. int ret;
  554. av_bprint_init(&args, 0, AV_BPRINT_SIZE_AUTOMATIC);
  555. av_bprintf(&args, "time_base=%d/%d:sample_rate=%d:sample_fmt=%s",
  556. 1, ist->st->codec->sample_rate,
  557. ist->st->codec->sample_rate,
  558. av_get_sample_fmt_name(ist->st->codec->sample_fmt));
  559. if (ist->st->codec->channel_layout)
  560. av_bprintf(&args, ":channel_layout=0x%"PRIx64,
  561. ist->st->codec->channel_layout);
  562. else
  563. av_bprintf(&args, ":channels=%d", ist->st->codec->channels);
  564. snprintf(name, sizeof(name), "graph %d input from stream %d:%d", fg->index,
  565. ist->file_index, ist->st->index);
  566. if ((ret = avfilter_graph_create_filter(&ifilter->filter, filter,
  567. name, args.str, NULL,
  568. fg->graph)) < 0)
  569. return ret;
  570. #define AUTO_INSERT_FILTER_INPUT(opt_name, filter_name, arg) do { \
  571. AVFilterContext *filt_ctx; \
  572. \
  573. av_log(NULL, AV_LOG_INFO, opt_name " is forwarded to lavfi " \
  574. "similarly to -af " filter_name "=%s.\n", arg); \
  575. \
  576. snprintf(name, sizeof(name), "graph %d %s for input stream %d:%d", \
  577. fg->index, filter_name, ist->file_index, ist->st->index); \
  578. ret = avfilter_graph_create_filter(&filt_ctx, \
  579. avfilter_get_by_name(filter_name), \
  580. name, arg, NULL, fg->graph); \
  581. if (ret < 0) \
  582. return ret; \
  583. \
  584. ret = avfilter_link(filt_ctx, 0, first_filter, pad_idx); \
  585. if (ret < 0) \
  586. return ret; \
  587. \
  588. first_filter = filt_ctx; \
  589. } while (0)
  590. if (audio_sync_method > 0) {
  591. char args[256] = {0};
  592. av_strlcatf(args, sizeof(args), "async=%d", audio_sync_method);
  593. if (audio_drift_threshold != 0.1)
  594. av_strlcatf(args, sizeof(args), ":min_hard_comp=%f", audio_drift_threshold);
  595. AUTO_INSERT_FILTER_INPUT("-async", "aresample", args);
  596. }
  597. // if (ost->audio_channels_mapped) {
  598. // int i;
  599. // AVBPrint pan_buf;
  600. // av_bprint_init(&pan_buf, 256, 8192);
  601. // av_bprintf(&pan_buf, "0x%"PRIx64,
  602. // av_get_default_channel_layout(ost->audio_channels_mapped));
  603. // for (i = 0; i < ost->audio_channels_mapped; i++)
  604. // if (ost->audio_channels_map[i] != -1)
  605. // av_bprintf(&pan_buf, ":c%d=c%d", i, ost->audio_channels_map[i]);
  606. // AUTO_INSERT_FILTER_INPUT("-map_channel", "pan", pan_buf.str);
  607. // av_bprint_finalize(&pan_buf, NULL);
  608. // }
  609. if (audio_volume != 256) {
  610. char args[256];
  611. av_log(NULL, AV_LOG_WARNING, "-vol has been deprecated. Use the volume "
  612. "audio filter instead.\n");
  613. snprintf(args, sizeof(args), "%f", audio_volume / 256.);
  614. AUTO_INSERT_FILTER_INPUT("-vol", "volume", args);
  615. }
  616. if ((ret = avfilter_link(ifilter->filter, 0, first_filter, pad_idx)) < 0)
  617. return ret;
  618. return 0;
  619. }
  620. static int configure_input_filter(FilterGraph *fg, InputFilter *ifilter,
  621. AVFilterInOut *in)
  622. {
  623. av_freep(&ifilter->name);
  624. DESCRIBE_FILTER_LINK(ifilter, in, 1);
  625. switch (avfilter_pad_get_type(in->filter_ctx->input_pads, in->pad_idx)) {
  626. case AVMEDIA_TYPE_VIDEO: return configure_input_video_filter(fg, ifilter, in);
  627. case AVMEDIA_TYPE_AUDIO: return configure_input_audio_filter(fg, ifilter, in);
  628. default: av_assert0(0);
  629. }
  630. }
  631. int configure_filtergraph(FilterGraph *fg)
  632. {
  633. AVFilterInOut *inputs, *outputs, *cur;
  634. int ret, i, init = !fg->graph, simple = !fg->graph_desc;
  635. const char *graph_desc = simple ? fg->outputs[0]->ost->avfilter :
  636. fg->graph_desc;
  637. avfilter_graph_free(&fg->graph);
  638. if (!(fg->graph = avfilter_graph_alloc()))
  639. return AVERROR(ENOMEM);
  640. if (simple) {
  641. OutputStream *ost = fg->outputs[0]->ost;
  642. char args[255];
  643. snprintf(args, sizeof(args), "flags=0x%X", (unsigned)ost->sws_flags);
  644. fg->graph->scale_sws_opts = av_strdup(args);
  645. args[0] = 0;
  646. if (ost->swr_filter_type != SWR_FILTER_TYPE_KAISER)
  647. av_strlcatf(args, sizeof(args), "filter_type=%d:", (int)ost->swr_filter_type);
  648. if (ost->swr_dither_method)
  649. av_strlcatf(args, sizeof(args), "dither_method=%d:", (int)ost->swr_dither_method);
  650. if (ost->swr_dither_scale != 1.0)
  651. av_strlcatf(args, sizeof(args), "dither_scale=%f:", ost->swr_dither_scale);
  652. if (strlen(args))
  653. args[strlen(args)-1] = 0;
  654. av_opt_set(fg->graph, "aresample_swr_opts", args, 0);
  655. }
  656. if ((ret = avfilter_graph_parse2(fg->graph, graph_desc, &inputs, &outputs)) < 0)
  657. return ret;
  658. if (simple && (!inputs || inputs->next || !outputs || outputs->next)) {
  659. av_log(NULL, AV_LOG_ERROR, "Simple filtergraph '%s' does not have "
  660. "exactly one input and output.\n", graph_desc);
  661. return AVERROR(EINVAL);
  662. }
  663. for (cur = inputs; !simple && init && cur; cur = cur->next)
  664. init_input_filter(fg, cur);
  665. for (cur = inputs, i = 0; cur; cur = cur->next, i++)
  666. if ((ret = configure_input_filter(fg, fg->inputs[i], cur)) < 0)
  667. return ret;
  668. avfilter_inout_free(&inputs);
  669. if (!init || simple) {
  670. /* we already know the mappings between lavfi outputs and output streams,
  671. * so we can finish the setup */
  672. for (cur = outputs, i = 0; cur; cur = cur->next, i++)
  673. configure_output_filter(fg, fg->outputs[i], cur);
  674. avfilter_inout_free(&outputs);
  675. if ((ret = avfilter_graph_config(fg->graph, NULL)) < 0)
  676. return ret;
  677. } else {
  678. /* wait until output mappings are processed */
  679. for (cur = outputs; cur;) {
  680. GROW_ARRAY(fg->outputs, fg->nb_outputs);
  681. if (!(fg->outputs[fg->nb_outputs - 1] = av_mallocz(sizeof(*fg->outputs[0]))))
  682. exit(1);
  683. fg->outputs[fg->nb_outputs - 1]->graph = fg;
  684. fg->outputs[fg->nb_outputs - 1]->out_tmp = cur;
  685. cur = cur->next;
  686. fg->outputs[fg->nb_outputs - 1]->out_tmp->next = NULL;
  687. }
  688. }
  689. return 0;
  690. }
  691. int ist_in_filtergraph(FilterGraph *fg, InputStream *ist)
  692. {
  693. int i;
  694. for (i = 0; i < fg->nb_inputs; i++)
  695. if (fg->inputs[i]->ist == ist)
  696. return 1;
  697. return 0;
  698. }