ffmpeg_filter.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780
  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. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(target);
  37. int has_alpha = desc ? desc->nb_components % 2 == 0 : 0;
  38. enum AVPixelFormat best= AV_PIX_FMT_NONE;
  39. if (st->codec->strict_std_compliance <= FF_COMPLIANCE_UNOFFICIAL) {
  40. if (st->codec->codec_id == AV_CODEC_ID_MJPEG) {
  41. p = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_NONE };
  42. } else if (st->codec->codec_id == AV_CODEC_ID_LJPEG) {
  43. p = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUV420P,
  44. AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_BGRA, AV_PIX_FMT_NONE };
  45. }
  46. }
  47. for (; *p != AV_PIX_FMT_NONE; p++) {
  48. best= avcodec_find_best_pix_fmt_of_2(best, *p, target, has_alpha, NULL);
  49. if (*p == target)
  50. break;
  51. }
  52. if (*p == AV_PIX_FMT_NONE) {
  53. if (target != AV_PIX_FMT_NONE)
  54. av_log(NULL, AV_LOG_WARNING,
  55. "Incompatible pixel format '%s' for codec '%s', auto-selecting format '%s'\n",
  56. av_get_pix_fmt_name(target),
  57. codec->name,
  58. av_get_pix_fmt_name(best));
  59. return best;
  60. }
  61. }
  62. return target;
  63. }
  64. void choose_sample_fmt(AVStream *st, AVCodec *codec)
  65. {
  66. if (codec && codec->sample_fmts) {
  67. const enum AVSampleFormat *p = codec->sample_fmts;
  68. for (; *p != -1; p++) {
  69. if (*p == st->codec->sample_fmt)
  70. break;
  71. }
  72. if (*p == -1) {
  73. if((codec->capabilities & CODEC_CAP_LOSSLESS) && av_get_sample_fmt_name(st->codec->sample_fmt) > av_get_sample_fmt_name(codec->sample_fmts[0]))
  74. av_log(NULL, AV_LOG_ERROR, "Conversion will not be lossless.\n");
  75. if(av_get_sample_fmt_name(st->codec->sample_fmt))
  76. av_log(NULL, AV_LOG_WARNING,
  77. "Incompatible sample format '%s' for codec '%s', auto-selecting format '%s'\n",
  78. av_get_sample_fmt_name(st->codec->sample_fmt),
  79. codec->name,
  80. av_get_sample_fmt_name(codec->sample_fmts[0]));
  81. st->codec->sample_fmt = codec->sample_fmts[0];
  82. }
  83. }
  84. }
  85. static char *choose_pix_fmts(OutputStream *ost)
  86. {
  87. if (ost->keep_pix_fmt) {
  88. if (ost->filter)
  89. avfilter_graph_set_auto_convert(ost->filter->graph->graph,
  90. AVFILTER_AUTO_CONVERT_NONE);
  91. if (ost->st->codec->pix_fmt == AV_PIX_FMT_NONE)
  92. return NULL;
  93. return av_strdup(av_get_pix_fmt_name(ost->st->codec->pix_fmt));
  94. }
  95. if (ost->st->codec->pix_fmt != AV_PIX_FMT_NONE) {
  96. return av_strdup(av_get_pix_fmt_name(choose_pixel_fmt(ost->st, ost->enc, ost->st->codec->pix_fmt)));
  97. } else if (ost->enc && ost->enc->pix_fmts) {
  98. const enum AVPixelFormat *p;
  99. AVIOContext *s = NULL;
  100. uint8_t *ret;
  101. int len;
  102. if (avio_open_dyn_buf(&s) < 0)
  103. exit(1);
  104. p = ost->enc->pix_fmts;
  105. if (ost->st->codec->strict_std_compliance <= FF_COMPLIANCE_UNOFFICIAL) {
  106. if (ost->st->codec->codec_id == AV_CODEC_ID_MJPEG) {
  107. p = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_NONE };
  108. } else if (ost->st->codec->codec_id == AV_CODEC_ID_LJPEG) {
  109. p = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUV420P,
  110. AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_BGRA, AV_PIX_FMT_NONE };
  111. }
  112. }
  113. for (; *p != AV_PIX_FMT_NONE; p++) {
  114. const char *name = av_get_pix_fmt_name(*p);
  115. avio_printf(s, "%s:", name);
  116. }
  117. len = avio_close_dyn_buf(s, &ret);
  118. ret[len - 1] = 0;
  119. return ret;
  120. } else
  121. return NULL;
  122. }
  123. /* Define a function for building a string containing a list of
  124. * allowed formats. */
  125. #define DEF_CHOOSE_FORMAT(type, var, supported_list, none, get_name, separator)\
  126. static char *choose_ ## var ## s(OutputStream *ost) \
  127. { \
  128. if (ost->st->codec->var != none) { \
  129. get_name(ost->st->codec->var); \
  130. return av_strdup(name); \
  131. } else if (ost->enc->supported_list) { \
  132. const type *p; \
  133. AVIOContext *s = NULL; \
  134. uint8_t *ret; \
  135. int len; \
  136. \
  137. if (avio_open_dyn_buf(&s) < 0) \
  138. exit(1); \
  139. \
  140. for (p = ost->enc->supported_list; *p != none; p++) { \
  141. get_name(*p); \
  142. avio_printf(s, "%s" separator, name); \
  143. } \
  144. len = avio_close_dyn_buf(s, &ret); \
  145. ret[len - 1] = 0; \
  146. return ret; \
  147. } else \
  148. return NULL; \
  149. }
  150. // DEF_CHOOSE_FORMAT(enum AVPixelFormat, pix_fmt, pix_fmts, AV_PIX_FMT_NONE,
  151. // GET_PIX_FMT_NAME, ":")
  152. DEF_CHOOSE_FORMAT(enum AVSampleFormat, sample_fmt, sample_fmts,
  153. AV_SAMPLE_FMT_NONE, GET_SAMPLE_FMT_NAME, ",")
  154. DEF_CHOOSE_FORMAT(int, sample_rate, supported_samplerates, 0,
  155. GET_SAMPLE_RATE_NAME, ",")
  156. DEF_CHOOSE_FORMAT(uint64_t, channel_layout, channel_layouts, 0,
  157. GET_CH_LAYOUT_NAME, ",")
  158. FilterGraph *init_simple_filtergraph(InputStream *ist, OutputStream *ost)
  159. {
  160. FilterGraph *fg = av_mallocz(sizeof(*fg));
  161. if (!fg)
  162. exit(1);
  163. fg->index = nb_filtergraphs;
  164. fg->outputs = grow_array(fg->outputs, sizeof(*fg->outputs), &fg->nb_outputs,
  165. fg->nb_outputs + 1);
  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. fg->inputs = grow_array(fg->inputs, sizeof(*fg->inputs), &fg->nb_inputs,
  172. fg->nb_inputs + 1);
  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. ist->filters = grow_array(ist->filters, sizeof(*ist->filters),
  178. &ist->nb_filters, ist->nb_filters + 1);
  179. ist->filters[ist->nb_filters - 1] = fg->inputs[0];
  180. filtergraphs = grow_array(filtergraphs, sizeof(*filtergraphs),
  181. &nb_filtergraphs, nb_filtergraphs + 1);
  182. filtergraphs[nb_filtergraphs - 1] = fg;
  183. return fg;
  184. }
  185. static void init_input_filter(FilterGraph *fg, AVFilterInOut *in)
  186. {
  187. InputStream *ist = NULL;
  188. enum AVMediaType type = avfilter_pad_get_type(in->filter_ctx->input_pads, in->pad_idx);
  189. int i;
  190. // TODO: support other filter types
  191. if (type != AVMEDIA_TYPE_VIDEO && type != AVMEDIA_TYPE_AUDIO) {
  192. av_log(NULL, AV_LOG_FATAL, "Only video and audio filters supported "
  193. "currently.\n");
  194. exit(1);
  195. }
  196. if (in->name) {
  197. AVFormatContext *s;
  198. AVStream *st = NULL;
  199. char *p;
  200. int file_idx = strtol(in->name, &p, 0);
  201. if (file_idx < 0 || file_idx >= nb_input_files) {
  202. av_log(NULL, AV_LOG_FATAL, "Invalid file index %d in filtergraph description %s.\n",
  203. file_idx, fg->graph_desc);
  204. exit(1);
  205. }
  206. s = input_files[file_idx]->ctx;
  207. for (i = 0; i < s->nb_streams; i++) {
  208. enum AVMediaType stream_type = s->streams[i]->codec->codec_type;
  209. if (stream_type != type &&
  210. !(stream_type == AVMEDIA_TYPE_SUBTITLE &&
  211. type == AVMEDIA_TYPE_VIDEO /* sub2video hack */))
  212. continue;
  213. if (check_stream_specifier(s, s->streams[i], *p == ':' ? p + 1 : p) == 1) {
  214. st = s->streams[i];
  215. break;
  216. }
  217. }
  218. if (!st) {
  219. av_log(NULL, AV_LOG_FATAL, "Stream specifier '%s' in filtergraph description %s "
  220. "matches no streams.\n", p, fg->graph_desc);
  221. exit(1);
  222. }
  223. ist = input_streams[input_files[file_idx]->ist_index + st->index];
  224. } else {
  225. /* find the first unused stream of corresponding type */
  226. for (i = 0; i < nb_input_streams; i++) {
  227. ist = input_streams[i];
  228. if (ist->st->codec->codec_type == type && ist->discard)
  229. break;
  230. }
  231. if (i == nb_input_streams) {
  232. av_log(NULL, AV_LOG_FATAL, "Cannot find a matching stream for "
  233. "unlabeled input pad %d on filter %s\n", in->pad_idx,
  234. in->filter_ctx->name);
  235. exit(1);
  236. }
  237. }
  238. av_assert0(ist);
  239. ist->discard = 0;
  240. ist->decoding_needed++;
  241. ist->st->discard = AVDISCARD_NONE;
  242. fg->inputs = grow_array(fg->inputs, sizeof(*fg->inputs),
  243. &fg->nb_inputs, fg->nb_inputs + 1);
  244. if (!(fg->inputs[fg->nb_inputs - 1] = av_mallocz(sizeof(*fg->inputs[0]))))
  245. exit(1);
  246. fg->inputs[fg->nb_inputs - 1]->ist = ist;
  247. fg->inputs[fg->nb_inputs - 1]->graph = fg;
  248. ist->filters = grow_array(ist->filters, sizeof(*ist->filters),
  249. &ist->nb_filters, ist->nb_filters + 1);
  250. ist->filters[ist->nb_filters - 1] = fg->inputs[fg->nb_inputs - 1];
  251. }
  252. static int configure_output_video_filter(FilterGraph *fg, OutputFilter *ofilter, AVFilterInOut *out)
  253. {
  254. char *pix_fmts;
  255. OutputStream *ost = ofilter->ost;
  256. AVCodecContext *codec = ost->st->codec;
  257. AVFilterContext *last_filter = out->filter_ctx;
  258. int pad_idx = out->pad_idx;
  259. int ret;
  260. char name[255];
  261. AVBufferSinkParams *buffersink_params = av_buffersink_params_alloc();
  262. snprintf(name, sizeof(name), "output stream %d:%d", ost->file_index, ost->index);
  263. ret = avfilter_graph_create_filter(&ofilter->filter,
  264. avfilter_get_by_name("ffbuffersink"),
  265. name, NULL, NULL, fg->graph);
  266. av_freep(&buffersink_params);
  267. if (ret < 0)
  268. return ret;
  269. if (codec->width || codec->height) {
  270. char args[255];
  271. AVFilterContext *filter;
  272. snprintf(args, sizeof(args), "%d:%d:flags=0x%X",
  273. codec->width,
  274. codec->height,
  275. (unsigned)ost->sws_flags);
  276. snprintf(name, sizeof(name), "scaler for output stream %d:%d",
  277. ost->file_index, ost->index);
  278. if ((ret = avfilter_graph_create_filter(&filter, avfilter_get_by_name("scale"),
  279. name, args, NULL, fg->graph)) < 0)
  280. return ret;
  281. if ((ret = avfilter_link(last_filter, pad_idx, filter, 0)) < 0)
  282. return ret;
  283. last_filter = filter;
  284. pad_idx = 0;
  285. }
  286. if ((pix_fmts = choose_pix_fmts(ost))) {
  287. AVFilterContext *filter;
  288. snprintf(name, sizeof(name), "pixel format for output stream %d:%d",
  289. ost->file_index, ost->index);
  290. if ((ret = avfilter_graph_create_filter(&filter,
  291. avfilter_get_by_name("format"),
  292. "format", pix_fmts, NULL,
  293. fg->graph)) < 0)
  294. return ret;
  295. if ((ret = avfilter_link(last_filter, pad_idx, filter, 0)) < 0)
  296. return ret;
  297. last_filter = filter;
  298. pad_idx = 0;
  299. av_freep(&pix_fmts);
  300. }
  301. if (ost->frame_rate.num && 0) {
  302. AVFilterContext *fps;
  303. char args[255];
  304. snprintf(args, sizeof(args), "fps=%d/%d", ost->frame_rate.num,
  305. ost->frame_rate.den);
  306. snprintf(name, sizeof(name), "fps for output stream %d:%d",
  307. ost->file_index, ost->index);
  308. ret = avfilter_graph_create_filter(&fps, avfilter_get_by_name("fps"),
  309. name, args, NULL, fg->graph);
  310. if (ret < 0)
  311. return ret;
  312. ret = avfilter_link(last_filter, pad_idx, fps, 0);
  313. if (ret < 0)
  314. return ret;
  315. last_filter = fps;
  316. pad_idx = 0;
  317. }
  318. if ((ret = avfilter_link(last_filter, pad_idx, ofilter->filter, 0)) < 0)
  319. return ret;
  320. return 0;
  321. }
  322. static int configure_output_audio_filter(FilterGraph *fg, OutputFilter *ofilter, AVFilterInOut *out)
  323. {
  324. OutputStream *ost = ofilter->ost;
  325. AVCodecContext *codec = ost->st->codec;
  326. AVFilterContext *last_filter = out->filter_ctx;
  327. int pad_idx = out->pad_idx;
  328. char *sample_fmts, *sample_rates, *channel_layouts;
  329. char name[255];
  330. int ret;
  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, NULL, fg->graph);
  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 = w;
  462. ist->sub2video.h = ist->st->codec->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. codec_fr.den *= ist->st->codec->ticks_per_frame;
  497. if(codec_fr.num>0 && codec_fr.den>0 && av_q2d(codec_fr) < av_q2d(fr)*0.7)
  498. fr = codec_fr;
  499. }
  500. if (ist->st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE) {
  501. ret = sub2video_prepare(ist);
  502. if (ret < 0)
  503. return ret;
  504. }
  505. sar = ist->st->sample_aspect_ratio.num ?
  506. ist->st->sample_aspect_ratio :
  507. ist->st->codec->sample_aspect_ratio;
  508. if(!sar.den)
  509. sar = (AVRational){0,1};
  510. av_bprint_init(&args, 0, 1);
  511. av_bprintf(&args,
  512. "video_size=%dx%d:pix_fmt=%d:time_base=%d/%d:"
  513. "pixel_aspect=%d/%d:sws_param=flags=%d", ist->st->codec->width,
  514. ist->st->codec->height, ist->st->codec->pix_fmt,
  515. tb.num, tb.den, sar.num, sar.den,
  516. SWS_BILINEAR + ((ist->st->codec->flags&CODEC_FLAG_BITEXACT) ? SWS_BITEXACT:0));
  517. if (fr.num && fr.den)
  518. av_bprintf(&args, ":frame_rate=%d/%d", fr.num, fr.den);
  519. snprintf(name, sizeof(name), "graph %d input from stream %d:%d", fg->index,
  520. ist->file_index, ist->st->index);
  521. if ((ret = avfilter_graph_create_filter(&ifilter->filter, filter, name,
  522. args.str, NULL, fg->graph)) < 0)
  523. return ret;
  524. if (ist->framerate.num) {
  525. AVFilterContext *setpts;
  526. snprintf(name, sizeof(name), "force CFR for input from stream %d:%d",
  527. ist->file_index, ist->st->index);
  528. if ((ret = avfilter_graph_create_filter(&setpts,
  529. avfilter_get_by_name("setpts"),
  530. name, "N", NULL,
  531. fg->graph)) < 0)
  532. return ret;
  533. if ((ret = avfilter_link(setpts, 0, first_filter, pad_idx)) < 0)
  534. return ret;
  535. first_filter = setpts;
  536. pad_idx = 0;
  537. }
  538. if ((ret = avfilter_link(ifilter->filter, 0, first_filter, pad_idx)) < 0)
  539. return ret;
  540. return 0;
  541. }
  542. static int configure_input_audio_filter(FilterGraph *fg, InputFilter *ifilter,
  543. AVFilterInOut *in)
  544. {
  545. AVFilterContext *first_filter = in->filter_ctx;
  546. AVFilter *filter = avfilter_get_by_name("abuffer");
  547. InputStream *ist = ifilter->ist;
  548. int pad_idx = in->pad_idx;
  549. char args[255], name[255];
  550. int ret;
  551. snprintf(args, sizeof(args), "time_base=%d/%d:sample_rate=%d:sample_fmt=%s"
  552. ":channel_layout=0x%"PRIx64,
  553. 1, ist->st->codec->sample_rate,
  554. ist->st->codec->sample_rate,
  555. av_get_sample_fmt_name(ist->st->codec->sample_fmt),
  556. ist->st->codec->channel_layout);
  557. snprintf(name, sizeof(name), "graph %d input from stream %d:%d", fg->index,
  558. ist->file_index, ist->st->index);
  559. if ((ret = avfilter_graph_create_filter(&ifilter->filter, filter,
  560. name, args, NULL,
  561. fg->graph)) < 0)
  562. return ret;
  563. #define AUTO_INSERT_FILTER_INPUT(opt_name, filter_name, arg) do { \
  564. AVFilterContext *filt_ctx; \
  565. \
  566. av_log(NULL, AV_LOG_INFO, opt_name " is forwarded to lavfi " \
  567. "similarly to -af " filter_name "=%s.\n", arg); \
  568. \
  569. snprintf(name, sizeof(name), "graph %d %s for input stream %d:%d", \
  570. fg->index, filter_name, ist->file_index, ist->st->index); \
  571. ret = avfilter_graph_create_filter(&filt_ctx, \
  572. avfilter_get_by_name(filter_name), \
  573. name, arg, NULL, fg->graph); \
  574. if (ret < 0) \
  575. return ret; \
  576. \
  577. ret = avfilter_link(filt_ctx, 0, first_filter, pad_idx); \
  578. if (ret < 0) \
  579. return ret; \
  580. \
  581. first_filter = filt_ctx; \
  582. } while (0)
  583. if (audio_sync_method > 0) {
  584. char args[256] = {0};
  585. av_strlcatf(args, sizeof(args), "min_comp=0.001:min_hard_comp=%f", audio_drift_threshold);
  586. if (audio_sync_method > 1)
  587. av_strlcatf(args, sizeof(args), ":max_soft_comp=%f", audio_sync_method/(double)ist->st->codec->sample_rate);
  588. AUTO_INSERT_FILTER_INPUT("-async", "aresample", args);
  589. }
  590. // if (ost->audio_channels_mapped) {
  591. // int i;
  592. // AVBPrint pan_buf;
  593. // av_bprint_init(&pan_buf, 256, 8192);
  594. // av_bprintf(&pan_buf, "0x%"PRIx64,
  595. // av_get_default_channel_layout(ost->audio_channels_mapped));
  596. // for (i = 0; i < ost->audio_channels_mapped; i++)
  597. // if (ost->audio_channels_map[i] != -1)
  598. // av_bprintf(&pan_buf, ":c%d=c%d", i, ost->audio_channels_map[i]);
  599. // AUTO_INSERT_FILTER_INPUT("-map_channel", "pan", pan_buf.str);
  600. // av_bprint_finalize(&pan_buf, NULL);
  601. // }
  602. if (audio_volume != 256) {
  603. char args[256];
  604. snprintf(args, sizeof(args), "%f", audio_volume / 256.);
  605. AUTO_INSERT_FILTER_INPUT("-vol", "volume", args);
  606. }
  607. if ((ret = avfilter_link(ifilter->filter, 0, first_filter, pad_idx)) < 0)
  608. return ret;
  609. return 0;
  610. }
  611. static int configure_input_filter(FilterGraph *fg, InputFilter *ifilter,
  612. AVFilterInOut *in)
  613. {
  614. av_freep(&ifilter->name);
  615. DESCRIBE_FILTER_LINK(ifilter, in, 1);
  616. switch (avfilter_pad_get_type(in->filter_ctx->input_pads, in->pad_idx)) {
  617. case AVMEDIA_TYPE_VIDEO: return configure_input_video_filter(fg, ifilter, in);
  618. case AVMEDIA_TYPE_AUDIO: return configure_input_audio_filter(fg, ifilter, in);
  619. default: av_assert0(0);
  620. }
  621. }
  622. int configure_filtergraph(FilterGraph *fg)
  623. {
  624. AVFilterInOut *inputs, *outputs, *cur;
  625. int ret, i, init = !fg->graph, simple = !fg->graph_desc;
  626. const char *graph_desc = simple ? fg->outputs[0]->ost->avfilter :
  627. fg->graph_desc;
  628. avfilter_graph_free(&fg->graph);
  629. if (!(fg->graph = avfilter_graph_alloc()))
  630. return AVERROR(ENOMEM);
  631. if (simple) {
  632. OutputStream *ost = fg->outputs[0]->ost;
  633. char args[255];
  634. snprintf(args, sizeof(args), "flags=0x%X", (unsigned)ost->sws_flags);
  635. fg->graph->scale_sws_opts = av_strdup(args);
  636. }
  637. if ((ret = avfilter_graph_parse2(fg->graph, graph_desc, &inputs, &outputs)) < 0)
  638. return ret;
  639. if (simple && (!inputs || inputs->next || !outputs || outputs->next)) {
  640. av_log(NULL, AV_LOG_ERROR, "Simple filtergraph '%s' does not have "
  641. "exactly one input and output.\n", graph_desc);
  642. return AVERROR(EINVAL);
  643. }
  644. for (cur = inputs; !simple && init && cur; cur = cur->next)
  645. init_input_filter(fg, cur);
  646. for (cur = inputs, i = 0; cur; cur = cur->next, i++)
  647. if ((ret = configure_input_filter(fg, fg->inputs[i], cur)) < 0)
  648. return ret;
  649. avfilter_inout_free(&inputs);
  650. if (!init || simple) {
  651. /* we already know the mappings between lavfi outputs and output streams,
  652. * so we can finish the setup */
  653. for (cur = outputs, i = 0; cur; cur = cur->next, i++)
  654. configure_output_filter(fg, fg->outputs[i], cur);
  655. avfilter_inout_free(&outputs);
  656. if ((ret = avfilter_graph_config(fg->graph, NULL)) < 0)
  657. return ret;
  658. } else {
  659. /* wait until output mappings are processed */
  660. for (cur = outputs; cur;) {
  661. fg->outputs = grow_array(fg->outputs, sizeof(*fg->outputs),
  662. &fg->nb_outputs, fg->nb_outputs + 1);
  663. if (!(fg->outputs[fg->nb_outputs - 1] = av_mallocz(sizeof(*fg->outputs[0]))))
  664. exit(1);
  665. fg->outputs[fg->nb_outputs - 1]->graph = fg;
  666. fg->outputs[fg->nb_outputs - 1]->out_tmp = cur;
  667. cur = cur->next;
  668. fg->outputs[fg->nb_outputs - 1]->out_tmp->next = NULL;
  669. }
  670. }
  671. return 0;
  672. }
  673. int ist_in_filtergraph(FilterGraph *fg, InputStream *ist)
  674. {
  675. int i;
  676. for (i = 0; i < fg->nb_inputs; i++)
  677. if (fg->inputs[i]->ist == ist)
  678. return 1;
  679. return 0;
  680. }