ffmpeg_filter.c 32 KB

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