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.

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