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.

780 lines
31KB

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