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.

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