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.

1056 lines
40KB

  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 <stdint.h>
  21. #include "ffmpeg.h"
  22. #include "libavfilter/avfilter.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/display.h"
  30. #include "libavutil/opt.h"
  31. #include "libavutil/pixdesc.h"
  32. #include "libavutil/pixfmt.h"
  33. #include "libavutil/imgutils.h"
  34. #include "libavutil/samplefmt.h"
  35. enum AVPixelFormat choose_pixel_fmt(AVStream *st, AVCodecContext *enc_ctx, AVCodec *codec, enum AVPixelFormat target)
  36. {
  37. if (codec && codec->pix_fmts) {
  38. const enum AVPixelFormat *p = codec->pix_fmts;
  39. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(target);
  40. int has_alpha = desc ? desc->nb_components % 2 == 0 : 0;
  41. enum AVPixelFormat best= AV_PIX_FMT_NONE;
  42. static const enum AVPixelFormat mjpeg_formats[] =
  43. { AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_NONE };
  44. static const enum AVPixelFormat ljpeg_formats[] =
  45. { 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. if (enc_ctx->strict_std_compliance <= FF_COMPLIANCE_UNOFFICIAL) {
  48. if (enc_ctx->codec_id == AV_CODEC_ID_MJPEG) {
  49. p = mjpeg_formats;
  50. } else if (enc_ctx->codec_id == AV_CODEC_ID_LJPEG) {
  51. p =ljpeg_formats;
  52. }
  53. }
  54. for (; *p != AV_PIX_FMT_NONE; p++) {
  55. best= avcodec_find_best_pix_fmt_of_2(best, *p, target, has_alpha, NULL);
  56. if (*p == target)
  57. break;
  58. }
  59. if (*p == AV_PIX_FMT_NONE) {
  60. if (target != AV_PIX_FMT_NONE)
  61. av_log(NULL, AV_LOG_WARNING,
  62. "Incompatible pixel format '%s' for codec '%s', auto-selecting format '%s'\n",
  63. av_get_pix_fmt_name(target),
  64. codec->name,
  65. av_get_pix_fmt_name(best));
  66. return best;
  67. }
  68. }
  69. return target;
  70. }
  71. void choose_sample_fmt(AVStream *st, AVCodec *codec)
  72. {
  73. if (codec && codec->sample_fmts) {
  74. const enum AVSampleFormat *p = codec->sample_fmts;
  75. for (; *p != -1; p++) {
  76. if (*p == st->codec->sample_fmt)
  77. break;
  78. }
  79. if (*p == -1) {
  80. if((codec->capabilities & CODEC_CAP_LOSSLESS) && av_get_sample_fmt_name(st->codec->sample_fmt) > av_get_sample_fmt_name(codec->sample_fmts[0]))
  81. av_log(NULL, AV_LOG_ERROR, "Conversion will not be lossless.\n");
  82. if(av_get_sample_fmt_name(st->codec->sample_fmt))
  83. av_log(NULL, AV_LOG_WARNING,
  84. "Incompatible sample format '%s' for codec '%s', auto-selecting format '%s'\n",
  85. av_get_sample_fmt_name(st->codec->sample_fmt),
  86. codec->name,
  87. av_get_sample_fmt_name(codec->sample_fmts[0]));
  88. st->codec->sample_fmt = codec->sample_fmts[0];
  89. }
  90. }
  91. }
  92. static char *choose_pix_fmts(OutputStream *ost)
  93. {
  94. AVDictionaryEntry *strict_dict = av_dict_get(ost->encoder_opts, "strict", NULL, 0);
  95. if (strict_dict)
  96. // used by choose_pixel_fmt() and below
  97. av_opt_set(ost->enc_ctx, "strict", strict_dict->value, 0);
  98. if (ost->keep_pix_fmt) {
  99. if (ost->filter)
  100. avfilter_graph_set_auto_convert(ost->filter->graph->graph,
  101. AVFILTER_AUTO_CONVERT_NONE);
  102. if (ost->enc_ctx->pix_fmt == AV_PIX_FMT_NONE)
  103. return NULL;
  104. return av_strdup(av_get_pix_fmt_name(ost->enc_ctx->pix_fmt));
  105. }
  106. if (ost->enc_ctx->pix_fmt != AV_PIX_FMT_NONE) {
  107. return av_strdup(av_get_pix_fmt_name(choose_pixel_fmt(ost->st, ost->enc_ctx, ost->enc, ost->enc_ctx->pix_fmt)));
  108. } else if (ost->enc && ost->enc->pix_fmts) {
  109. const enum AVPixelFormat *p;
  110. AVIOContext *s = NULL;
  111. uint8_t *ret;
  112. int len;
  113. if (avio_open_dyn_buf(&s) < 0)
  114. exit_program(1);
  115. p = ost->enc->pix_fmts;
  116. if (ost->enc_ctx->strict_std_compliance <= FF_COMPLIANCE_UNOFFICIAL) {
  117. if (ost->enc_ctx->codec_id == AV_CODEC_ID_MJPEG) {
  118. p = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_NONE };
  119. } else if (ost->enc_ctx->codec_id == AV_CODEC_ID_LJPEG) {
  120. p = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUV420P,
  121. AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_BGRA, AV_PIX_FMT_NONE };
  122. }
  123. }
  124. for (; *p != AV_PIX_FMT_NONE; p++) {
  125. const char *name = av_get_pix_fmt_name(*p);
  126. avio_printf(s, "%s|", name);
  127. }
  128. len = avio_close_dyn_buf(s, &ret);
  129. ret[len - 1] = 0;
  130. return ret;
  131. } else
  132. return NULL;
  133. }
  134. /* Define a function for building a string containing a list of
  135. * allowed formats. */
  136. #define DEF_CHOOSE_FORMAT(type, var, supported_list, none, get_name) \
  137. static char *choose_ ## var ## s(OutputStream *ost) \
  138. { \
  139. if (ost->enc_ctx->var != none) { \
  140. get_name(ost->enc_ctx->var); \
  141. return av_strdup(name); \
  142. } else if (ost->enc && ost->enc->supported_list) { \
  143. const type *p; \
  144. AVIOContext *s = NULL; \
  145. uint8_t *ret; \
  146. int len; \
  147. \
  148. if (avio_open_dyn_buf(&s) < 0) \
  149. exit_program(1); \
  150. \
  151. for (p = ost->enc->supported_list; *p != none; p++) { \
  152. get_name(*p); \
  153. avio_printf(s, "%s|", name); \
  154. } \
  155. len = avio_close_dyn_buf(s, &ret); \
  156. ret[len - 1] = 0; \
  157. return ret; \
  158. } else \
  159. return NULL; \
  160. }
  161. // DEF_CHOOSE_FORMAT(enum AVPixelFormat, pix_fmt, pix_fmts, AV_PIX_FMT_NONE,
  162. // GET_PIX_FMT_NAME)
  163. DEF_CHOOSE_FORMAT(enum AVSampleFormat, sample_fmt, sample_fmts,
  164. AV_SAMPLE_FMT_NONE, GET_SAMPLE_FMT_NAME)
  165. DEF_CHOOSE_FORMAT(int, sample_rate, supported_samplerates, 0,
  166. GET_SAMPLE_RATE_NAME)
  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. GROW_ARRAY(fg->outputs, fg->nb_outputs);
  176. if (!(fg->outputs[0] = av_mallocz(sizeof(*fg->outputs[0]))))
  177. exit_program(1);
  178. fg->outputs[0]->ost = ost;
  179. fg->outputs[0]->graph = fg;
  180. ost->filter = fg->outputs[0];
  181. GROW_ARRAY(fg->inputs, fg->nb_inputs);
  182. if (!(fg->inputs[0] = av_mallocz(sizeof(*fg->inputs[0]))))
  183. exit_program(1);
  184. fg->inputs[0]->ist = ist;
  185. fg->inputs[0]->graph = fg;
  186. GROW_ARRAY(ist->filters, ist->nb_filters);
  187. ist->filters[ist->nb_filters - 1] = fg->inputs[0];
  188. GROW_ARRAY(filtergraphs, nb_filtergraphs);
  189. filtergraphs[nb_filtergraphs - 1] = fg;
  190. return fg;
  191. }
  192. static void init_input_filter(FilterGraph *fg, AVFilterInOut *in)
  193. {
  194. InputStream *ist = NULL;
  195. enum AVMediaType type = avfilter_pad_get_type(in->filter_ctx->input_pads, in->pad_idx);
  196. int i;
  197. // TODO: support other filter types
  198. if (type != AVMEDIA_TYPE_VIDEO && type != AVMEDIA_TYPE_AUDIO) {
  199. av_log(NULL, AV_LOG_FATAL, "Only video and audio filters supported "
  200. "currently.\n");
  201. exit_program(1);
  202. }
  203. if (in->name) {
  204. AVFormatContext *s;
  205. AVStream *st = NULL;
  206. char *p;
  207. int file_idx = strtol(in->name, &p, 0);
  208. if (file_idx < 0 || file_idx >= nb_input_files) {
  209. av_log(NULL, AV_LOG_FATAL, "Invalid file index %d in filtergraph description %s.\n",
  210. file_idx, fg->graph_desc);
  211. exit_program(1);
  212. }
  213. s = input_files[file_idx]->ctx;
  214. for (i = 0; i < s->nb_streams; i++) {
  215. enum AVMediaType stream_type = s->streams[i]->codec->codec_type;
  216. if (stream_type != type &&
  217. !(stream_type == AVMEDIA_TYPE_SUBTITLE &&
  218. type == AVMEDIA_TYPE_VIDEO /* sub2video hack */))
  219. continue;
  220. if (check_stream_specifier(s, s->streams[i], *p == ':' ? p + 1 : p) == 1) {
  221. st = s->streams[i];
  222. break;
  223. }
  224. }
  225. if (!st) {
  226. av_log(NULL, AV_LOG_FATAL, "Stream specifier '%s' in filtergraph description %s "
  227. "matches no streams.\n", p, fg->graph_desc);
  228. exit_program(1);
  229. }
  230. ist = input_streams[input_files[file_idx]->ist_index + st->index];
  231. } else {
  232. /* find the first unused stream of corresponding type */
  233. for (i = 0; i < nb_input_streams; i++) {
  234. ist = input_streams[i];
  235. if (ist->dec_ctx->codec_type == type && ist->discard)
  236. break;
  237. }
  238. if (i == nb_input_streams) {
  239. av_log(NULL, AV_LOG_FATAL, "Cannot find a matching stream for "
  240. "unlabeled input pad %d on filter %s\n", in->pad_idx,
  241. in->filter_ctx->name);
  242. exit_program(1);
  243. }
  244. }
  245. av_assert0(ist);
  246. ist->discard = 0;
  247. ist->decoding_needed |= DECODING_FOR_FILTER;
  248. ist->st->discard = AVDISCARD_NONE;
  249. GROW_ARRAY(fg->inputs, fg->nb_inputs);
  250. if (!(fg->inputs[fg->nb_inputs - 1] = av_mallocz(sizeof(*fg->inputs[0]))))
  251. exit_program(1);
  252. fg->inputs[fg->nb_inputs - 1]->ist = ist;
  253. fg->inputs[fg->nb_inputs - 1]->graph = fg;
  254. GROW_ARRAY(ist->filters, ist->nb_filters);
  255. ist->filters[ist->nb_filters - 1] = fg->inputs[fg->nb_inputs - 1];
  256. }
  257. int init_complex_filtergraph(FilterGraph *fg)
  258. {
  259. AVFilterInOut *inputs, *outputs, *cur;
  260. AVFilterGraph *graph;
  261. int ret = 0;
  262. /* this graph is only used for determining the kinds of inputs
  263. * and outputs we have, and is discarded on exit from this function */
  264. graph = avfilter_graph_alloc();
  265. if (!graph)
  266. return AVERROR(ENOMEM);
  267. ret = avfilter_graph_parse2(graph, fg->graph_desc, &inputs, &outputs);
  268. if (ret < 0)
  269. goto fail;
  270. for (cur = inputs; cur; cur = cur->next)
  271. init_input_filter(fg, cur);
  272. for (cur = outputs; cur;) {
  273. GROW_ARRAY(fg->outputs, fg->nb_outputs);
  274. fg->outputs[fg->nb_outputs - 1] = av_mallocz(sizeof(*fg->outputs[0]));
  275. if (!fg->outputs[fg->nb_outputs - 1])
  276. exit_program(1);
  277. fg->outputs[fg->nb_outputs - 1]->graph = fg;
  278. fg->outputs[fg->nb_outputs - 1]->out_tmp = cur;
  279. fg->outputs[fg->nb_outputs - 1]->type = avfilter_pad_get_type(cur->filter_ctx->output_pads,
  280. cur->pad_idx);
  281. cur = cur->next;
  282. fg->outputs[fg->nb_outputs - 1]->out_tmp->next = NULL;
  283. }
  284. fail:
  285. avfilter_inout_free(&inputs);
  286. avfilter_graph_free(&graph);
  287. return ret;
  288. }
  289. static int insert_trim(int64_t start_time, int64_t duration,
  290. AVFilterContext **last_filter, int *pad_idx,
  291. const char *filter_name)
  292. {
  293. AVFilterGraph *graph = (*last_filter)->graph;
  294. AVFilterContext *ctx;
  295. const AVFilter *trim;
  296. enum AVMediaType type = avfilter_pad_get_type((*last_filter)->output_pads, *pad_idx);
  297. const char *name = (type == AVMEDIA_TYPE_VIDEO) ? "trim" : "atrim";
  298. int ret = 0;
  299. if (duration == INT64_MAX && start_time == AV_NOPTS_VALUE)
  300. return 0;
  301. trim = avfilter_get_by_name(name);
  302. if (!trim) {
  303. av_log(NULL, AV_LOG_ERROR, "%s filter not present, cannot limit "
  304. "recording time.\n", name);
  305. return AVERROR_FILTER_NOT_FOUND;
  306. }
  307. ctx = avfilter_graph_alloc_filter(graph, trim, filter_name);
  308. if (!ctx)
  309. return AVERROR(ENOMEM);
  310. if (duration != INT64_MAX) {
  311. ret = av_opt_set_int(ctx, "durationi", duration,
  312. AV_OPT_SEARCH_CHILDREN);
  313. }
  314. if (ret >= 0 && start_time != AV_NOPTS_VALUE) {
  315. ret = av_opt_set_int(ctx, "starti", start_time,
  316. AV_OPT_SEARCH_CHILDREN);
  317. }
  318. if (ret < 0) {
  319. av_log(ctx, AV_LOG_ERROR, "Error configuring the %s filter", name);
  320. return ret;
  321. }
  322. ret = avfilter_init_str(ctx, NULL);
  323. if (ret < 0)
  324. return ret;
  325. ret = avfilter_link(*last_filter, *pad_idx, ctx, 0);
  326. if (ret < 0)
  327. return ret;
  328. *last_filter = ctx;
  329. *pad_idx = 0;
  330. return 0;
  331. }
  332. static int insert_filter(AVFilterContext **last_filter, int *pad_idx,
  333. const char *filter_name, const char *args)
  334. {
  335. AVFilterGraph *graph = (*last_filter)->graph;
  336. AVFilterContext *ctx;
  337. int ret;
  338. ret = avfilter_graph_create_filter(&ctx,
  339. avfilter_get_by_name(filter_name),
  340. filter_name, args, NULL, graph);
  341. if (ret < 0)
  342. return ret;
  343. ret = avfilter_link(*last_filter, *pad_idx, ctx, 0);
  344. if (ret < 0)
  345. return ret;
  346. *last_filter = ctx;
  347. *pad_idx = 0;
  348. return 0;
  349. }
  350. static int configure_output_video_filter(FilterGraph *fg, OutputFilter *ofilter, AVFilterInOut *out)
  351. {
  352. char *pix_fmts;
  353. OutputStream *ost = ofilter->ost;
  354. OutputFile *of = output_files[ost->file_index];
  355. AVCodecContext *codec = ost->enc_ctx;
  356. AVFilterContext *last_filter = out->filter_ctx;
  357. int pad_idx = out->pad_idx;
  358. int ret;
  359. char name[255];
  360. snprintf(name, sizeof(name), "output stream %d:%d", ost->file_index, ost->index);
  361. ret = avfilter_graph_create_filter(&ofilter->filter,
  362. avfilter_get_by_name("buffersink"),
  363. name, NULL, NULL, fg->graph);
  364. if (ret < 0)
  365. return ret;
  366. if (codec->width || codec->height) {
  367. char args[255];
  368. AVFilterContext *filter;
  369. snprintf(args, sizeof(args), "%d:%d:0x%X",
  370. codec->width,
  371. codec->height,
  372. (unsigned)ost->sws_flags);
  373. snprintf(name, sizeof(name), "scaler for output stream %d:%d",
  374. ost->file_index, ost->index);
  375. if ((ret = avfilter_graph_create_filter(&filter, avfilter_get_by_name("scale"),
  376. name, args, NULL, fg->graph)) < 0)
  377. return ret;
  378. if ((ret = avfilter_link(last_filter, pad_idx, filter, 0)) < 0)
  379. return ret;
  380. last_filter = filter;
  381. pad_idx = 0;
  382. }
  383. if ((pix_fmts = choose_pix_fmts(ost))) {
  384. AVFilterContext *filter;
  385. snprintf(name, sizeof(name), "pixel format for output stream %d:%d",
  386. ost->file_index, ost->index);
  387. ret = avfilter_graph_create_filter(&filter,
  388. avfilter_get_by_name("format"),
  389. "format", pix_fmts, NULL, fg->graph);
  390. av_freep(&pix_fmts);
  391. if (ret < 0)
  392. return ret;
  393. if ((ret = avfilter_link(last_filter, pad_idx, filter, 0)) < 0)
  394. return ret;
  395. last_filter = filter;
  396. pad_idx = 0;
  397. }
  398. if (ost->frame_rate.num && 0) {
  399. AVFilterContext *fps;
  400. char args[255];
  401. snprintf(args, sizeof(args), "fps=%d/%d", ost->frame_rate.num,
  402. ost->frame_rate.den);
  403. snprintf(name, sizeof(name), "fps for output stream %d:%d",
  404. ost->file_index, ost->index);
  405. ret = avfilter_graph_create_filter(&fps, avfilter_get_by_name("fps"),
  406. name, args, NULL, fg->graph);
  407. if (ret < 0)
  408. return ret;
  409. ret = avfilter_link(last_filter, pad_idx, fps, 0);
  410. if (ret < 0)
  411. return ret;
  412. last_filter = fps;
  413. pad_idx = 0;
  414. }
  415. snprintf(name, sizeof(name), "trim for output stream %d:%d",
  416. ost->file_index, ost->index);
  417. ret = insert_trim(of->start_time, of->recording_time,
  418. &last_filter, &pad_idx, name);
  419. if (ret < 0)
  420. return ret;
  421. if ((ret = avfilter_link(last_filter, pad_idx, ofilter->filter, 0)) < 0)
  422. return ret;
  423. return 0;
  424. }
  425. static int configure_output_audio_filter(FilterGraph *fg, OutputFilter *ofilter, AVFilterInOut *out)
  426. {
  427. OutputStream *ost = ofilter->ost;
  428. OutputFile *of = output_files[ost->file_index];
  429. AVCodecContext *codec = ost->enc_ctx;
  430. AVFilterContext *last_filter = out->filter_ctx;
  431. int pad_idx = out->pad_idx;
  432. char *sample_fmts, *sample_rates, *channel_layouts;
  433. char name[255];
  434. int ret;
  435. snprintf(name, sizeof(name), "output stream %d:%d", ost->file_index, ost->index);
  436. ret = avfilter_graph_create_filter(&ofilter->filter,
  437. avfilter_get_by_name("abuffersink"),
  438. name, NULL, NULL, fg->graph);
  439. if (ret < 0)
  440. return ret;
  441. if ((ret = av_opt_set_int(ofilter->filter, "all_channel_counts", 1, AV_OPT_SEARCH_CHILDREN)) < 0)
  442. return ret;
  443. #define AUTO_INSERT_FILTER(opt_name, filter_name, arg) do { \
  444. AVFilterContext *filt_ctx; \
  445. \
  446. av_log(NULL, AV_LOG_INFO, opt_name " is forwarded to lavfi " \
  447. "similarly to -af " filter_name "=%s.\n", arg); \
  448. \
  449. ret = avfilter_graph_create_filter(&filt_ctx, \
  450. avfilter_get_by_name(filter_name), \
  451. filter_name, arg, NULL, fg->graph); \
  452. if (ret < 0) \
  453. return ret; \
  454. \
  455. ret = avfilter_link(last_filter, pad_idx, filt_ctx, 0); \
  456. if (ret < 0) \
  457. return ret; \
  458. \
  459. last_filter = filt_ctx; \
  460. pad_idx = 0; \
  461. } while (0)
  462. if (ost->audio_channels_mapped) {
  463. int i;
  464. AVBPrint pan_buf;
  465. av_bprint_init(&pan_buf, 256, 8192);
  466. av_bprintf(&pan_buf, "0x%"PRIx64,
  467. av_get_default_channel_layout(ost->audio_channels_mapped));
  468. for (i = 0; i < ost->audio_channels_mapped; i++)
  469. if (ost->audio_channels_map[i] != -1)
  470. av_bprintf(&pan_buf, ":c%d=c%d", i, ost->audio_channels_map[i]);
  471. AUTO_INSERT_FILTER("-map_channel", "pan", pan_buf.str);
  472. av_bprint_finalize(&pan_buf, NULL);
  473. }
  474. if (codec->channels && !codec->channel_layout)
  475. codec->channel_layout = av_get_default_channel_layout(codec->channels);
  476. sample_fmts = choose_sample_fmts(ost);
  477. sample_rates = choose_sample_rates(ost);
  478. channel_layouts = choose_channel_layouts(ost);
  479. if (sample_fmts || sample_rates || channel_layouts) {
  480. AVFilterContext *format;
  481. char args[256];
  482. args[0] = 0;
  483. if (sample_fmts)
  484. av_strlcatf(args, sizeof(args), "sample_fmts=%s:",
  485. sample_fmts);
  486. if (sample_rates)
  487. av_strlcatf(args, sizeof(args), "sample_rates=%s:",
  488. sample_rates);
  489. if (channel_layouts)
  490. av_strlcatf(args, sizeof(args), "channel_layouts=%s:",
  491. channel_layouts);
  492. av_freep(&sample_fmts);
  493. av_freep(&sample_rates);
  494. av_freep(&channel_layouts);
  495. snprintf(name, sizeof(name), "audio format for output stream %d:%d",
  496. ost->file_index, ost->index);
  497. ret = avfilter_graph_create_filter(&format,
  498. avfilter_get_by_name("aformat"),
  499. name, args, NULL, fg->graph);
  500. if (ret < 0)
  501. return ret;
  502. ret = avfilter_link(last_filter, pad_idx, format, 0);
  503. if (ret < 0)
  504. return ret;
  505. last_filter = format;
  506. pad_idx = 0;
  507. }
  508. if (audio_volume != 256 && 0) {
  509. char args[256];
  510. snprintf(args, sizeof(args), "%f", audio_volume / 256.);
  511. AUTO_INSERT_FILTER("-vol", "volume", args);
  512. }
  513. if (ost->apad && of->shortest) {
  514. char args[256];
  515. int i;
  516. for (i=0; i<of->ctx->nb_streams; i++)
  517. if (of->ctx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO)
  518. break;
  519. if (i<of->ctx->nb_streams) {
  520. snprintf(args, sizeof(args), "%s", ost->apad);
  521. AUTO_INSERT_FILTER("-apad", "apad", args);
  522. }
  523. }
  524. snprintf(name, sizeof(name), "trim for output stream %d:%d",
  525. ost->file_index, ost->index);
  526. ret = insert_trim(of->start_time, of->recording_time,
  527. &last_filter, &pad_idx, name);
  528. if (ret < 0)
  529. return ret;
  530. if ((ret = avfilter_link(last_filter, pad_idx, ofilter->filter, 0)) < 0)
  531. return ret;
  532. return 0;
  533. }
  534. #define DESCRIBE_FILTER_LINK(f, inout, in) \
  535. { \
  536. AVFilterContext *ctx = inout->filter_ctx; \
  537. AVFilterPad *pads = in ? ctx->input_pads : ctx->output_pads; \
  538. int nb_pads = in ? ctx->nb_inputs : ctx->nb_outputs; \
  539. AVIOContext *pb; \
  540. \
  541. if (avio_open_dyn_buf(&pb) < 0) \
  542. exit_program(1); \
  543. \
  544. avio_printf(pb, "%s", ctx->filter->name); \
  545. if (nb_pads > 1) \
  546. avio_printf(pb, ":%s", avfilter_pad_get_name(pads, inout->pad_idx));\
  547. avio_w8(pb, 0); \
  548. avio_close_dyn_buf(pb, &f->name); \
  549. }
  550. int configure_output_filter(FilterGraph *fg, OutputFilter *ofilter, AVFilterInOut *out)
  551. {
  552. av_freep(&ofilter->name);
  553. DESCRIBE_FILTER_LINK(ofilter, out, 0);
  554. if (!ofilter->ost) {
  555. av_log(NULL, AV_LOG_FATAL, "Filter %s has a unconnected output\n", ofilter->name);
  556. exit_program(1);
  557. }
  558. switch (avfilter_pad_get_type(out->filter_ctx->output_pads, out->pad_idx)) {
  559. case AVMEDIA_TYPE_VIDEO: return configure_output_video_filter(fg, ofilter, out);
  560. case AVMEDIA_TYPE_AUDIO: return configure_output_audio_filter(fg, ofilter, out);
  561. default: av_assert0(0);
  562. }
  563. }
  564. static int sub2video_prepare(InputStream *ist)
  565. {
  566. AVFormatContext *avf = input_files[ist->file_index]->ctx;
  567. int i, w, h;
  568. /* Compute the size of the canvas for the subtitles stream.
  569. If the subtitles codec has set a size, use it. Otherwise use the
  570. maximum dimensions of the video streams in the same file. */
  571. w = ist->dec_ctx->width;
  572. h = ist->dec_ctx->height;
  573. if (!(w && h)) {
  574. for (i = 0; i < avf->nb_streams; i++) {
  575. if (avf->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
  576. w = FFMAX(w, avf->streams[i]->codec->width);
  577. h = FFMAX(h, avf->streams[i]->codec->height);
  578. }
  579. }
  580. if (!(w && h)) {
  581. w = FFMAX(w, 720);
  582. h = FFMAX(h, 576);
  583. }
  584. av_log(avf, AV_LOG_INFO, "sub2video: using %dx%d canvas\n", w, h);
  585. }
  586. ist->sub2video.w = ist->dec_ctx->width = ist->resample_width = w;
  587. ist->sub2video.h = ist->dec_ctx->height = ist->resample_height = h;
  588. /* rectangles are AV_PIX_FMT_PAL8, but we have no guarantee that the
  589. palettes for all rectangles are identical or compatible */
  590. ist->resample_pix_fmt = ist->dec_ctx->pix_fmt = AV_PIX_FMT_RGB32;
  591. ist->sub2video.frame = av_frame_alloc();
  592. if (!ist->sub2video.frame)
  593. return AVERROR(ENOMEM);
  594. ist->sub2video.last_pts = INT64_MIN;
  595. return 0;
  596. }
  597. static int configure_input_video_filter(FilterGraph *fg, InputFilter *ifilter,
  598. AVFilterInOut *in)
  599. {
  600. AVFilterContext *last_filter;
  601. const AVFilter *buffer_filt = avfilter_get_by_name("buffer");
  602. InputStream *ist = ifilter->ist;
  603. InputFile *f = input_files[ist->file_index];
  604. AVRational tb = ist->framerate.num ? av_inv_q(ist->framerate) :
  605. ist->st->time_base;
  606. AVRational fr = ist->framerate;
  607. AVRational sar;
  608. AVBPrint args;
  609. char name[255];
  610. int ret, pad_idx = 0;
  611. int64_t tsoffset = 0;
  612. if (ist->dec_ctx->codec_type == AVMEDIA_TYPE_AUDIO) {
  613. av_log(NULL, AV_LOG_ERROR, "Cannot connect video filter to audio input\n");
  614. return AVERROR(EINVAL);
  615. }
  616. if (!fr.num)
  617. fr = av_guess_frame_rate(input_files[ist->file_index]->ctx, ist->st, NULL);
  618. if (ist->dec_ctx->codec_type == AVMEDIA_TYPE_SUBTITLE) {
  619. ret = sub2video_prepare(ist);
  620. if (ret < 0)
  621. return ret;
  622. }
  623. sar = ist->st->sample_aspect_ratio.num ?
  624. ist->st->sample_aspect_ratio :
  625. ist->dec_ctx->sample_aspect_ratio;
  626. if(!sar.den)
  627. sar = (AVRational){0,1};
  628. av_bprint_init(&args, 0, 1);
  629. av_bprintf(&args,
  630. "video_size=%dx%d:pix_fmt=%d:time_base=%d/%d:"
  631. "pixel_aspect=%d/%d:sws_param=flags=%d", ist->resample_width,
  632. ist->resample_height,
  633. ist->hwaccel_retrieve_data ? ist->hwaccel_retrieved_pix_fmt : ist->resample_pix_fmt,
  634. tb.num, tb.den, sar.num, sar.den,
  635. SWS_BILINEAR + ((ist->dec_ctx->flags&CODEC_FLAG_BITEXACT) ? SWS_BITEXACT:0));
  636. if (fr.num && fr.den)
  637. av_bprintf(&args, ":frame_rate=%d/%d", fr.num, fr.den);
  638. snprintf(name, sizeof(name), "graph %d input from stream %d:%d", fg->index,
  639. ist->file_index, ist->st->index);
  640. if ((ret = avfilter_graph_create_filter(&ifilter->filter, buffer_filt, name,
  641. args.str, NULL, fg->graph)) < 0)
  642. return ret;
  643. last_filter = ifilter->filter;
  644. if (ist->autorotate) {
  645. double theta = get_rotation(ist->st);
  646. if (fabs(theta - 90) < 1.0) {
  647. ret = insert_filter(&last_filter, &pad_idx, "transpose", "clock");
  648. } else if (fabs(theta - 180) < 1.0) {
  649. ret = insert_filter(&last_filter, &pad_idx, "hflip", NULL);
  650. if (ret < 0)
  651. return ret;
  652. ret = insert_filter(&last_filter, &pad_idx, "vflip", NULL);
  653. } else if (fabs(theta - 270) < 1.0) {
  654. ret = insert_filter(&last_filter, &pad_idx, "transpose", "cclock");
  655. } else if (fabs(theta) > 1.0) {
  656. char rotate_buf[64];
  657. snprintf(rotate_buf, sizeof(rotate_buf), "%f*PI/180", theta);
  658. ret = insert_filter(&last_filter, &pad_idx, "rotate", rotate_buf);
  659. }
  660. if (ret < 0)
  661. return ret;
  662. }
  663. if (ist->framerate.num) {
  664. AVFilterContext *setpts;
  665. snprintf(name, sizeof(name), "force CFR for input from stream %d:%d",
  666. ist->file_index, ist->st->index);
  667. if ((ret = avfilter_graph_create_filter(&setpts,
  668. avfilter_get_by_name("setpts"),
  669. name, "N", NULL,
  670. fg->graph)) < 0)
  671. return ret;
  672. if ((ret = avfilter_link(last_filter, 0, setpts, 0)) < 0)
  673. return ret;
  674. last_filter = setpts;
  675. }
  676. if (do_deinterlace) {
  677. AVFilterContext *yadif;
  678. snprintf(name, sizeof(name), "deinterlace input from stream %d:%d",
  679. ist->file_index, ist->st->index);
  680. if ((ret = avfilter_graph_create_filter(&yadif,
  681. avfilter_get_by_name("yadif"),
  682. name, "", NULL,
  683. fg->graph)) < 0)
  684. return ret;
  685. if ((ret = avfilter_link(last_filter, 0, yadif, 0)) < 0)
  686. return ret;
  687. last_filter = yadif;
  688. }
  689. snprintf(name, sizeof(name), "trim for input stream %d:%d",
  690. ist->file_index, ist->st->index);
  691. if (copy_ts) {
  692. tsoffset = f->start_time == AV_NOPTS_VALUE ? 0 : f->start_time;
  693. if (!start_at_zero && f->ctx->start_time != AV_NOPTS_VALUE)
  694. tsoffset += f->ctx->start_time;
  695. }
  696. ret = insert_trim(((f->start_time == AV_NOPTS_VALUE) || !f->accurate_seek) ?
  697. AV_NOPTS_VALUE : tsoffset, f->recording_time,
  698. &last_filter, &pad_idx, name);
  699. if (ret < 0)
  700. return ret;
  701. if ((ret = avfilter_link(last_filter, 0, in->filter_ctx, in->pad_idx)) < 0)
  702. return ret;
  703. return 0;
  704. }
  705. static int configure_input_audio_filter(FilterGraph *fg, InputFilter *ifilter,
  706. AVFilterInOut *in)
  707. {
  708. AVFilterContext *last_filter;
  709. const AVFilter *abuffer_filt = avfilter_get_by_name("abuffer");
  710. InputStream *ist = ifilter->ist;
  711. InputFile *f = input_files[ist->file_index];
  712. AVBPrint args;
  713. char name[255];
  714. int ret, pad_idx = 0;
  715. int64_t tsoffset = 0;
  716. if (ist->dec_ctx->codec_type != AVMEDIA_TYPE_AUDIO) {
  717. av_log(NULL, AV_LOG_ERROR, "Cannot connect audio filter to non audio input\n");
  718. return AVERROR(EINVAL);
  719. }
  720. av_bprint_init(&args, 0, AV_BPRINT_SIZE_AUTOMATIC);
  721. av_bprintf(&args, "time_base=%d/%d:sample_rate=%d:sample_fmt=%s",
  722. 1, ist->dec_ctx->sample_rate,
  723. ist->dec_ctx->sample_rate,
  724. av_get_sample_fmt_name(ist->dec_ctx->sample_fmt));
  725. if (ist->dec_ctx->channel_layout)
  726. av_bprintf(&args, ":channel_layout=0x%"PRIx64,
  727. ist->dec_ctx->channel_layout);
  728. else
  729. av_bprintf(&args, ":channels=%d", ist->dec_ctx->channels);
  730. snprintf(name, sizeof(name), "graph %d input from stream %d:%d", fg->index,
  731. ist->file_index, ist->st->index);
  732. if ((ret = avfilter_graph_create_filter(&ifilter->filter, abuffer_filt,
  733. name, args.str, NULL,
  734. fg->graph)) < 0)
  735. return ret;
  736. last_filter = ifilter->filter;
  737. #define AUTO_INSERT_FILTER_INPUT(opt_name, filter_name, arg) do { \
  738. AVFilterContext *filt_ctx; \
  739. \
  740. av_log(NULL, AV_LOG_INFO, opt_name " is forwarded to lavfi " \
  741. "similarly to -af " filter_name "=%s.\n", arg); \
  742. \
  743. snprintf(name, sizeof(name), "graph %d %s for input stream %d:%d", \
  744. fg->index, filter_name, ist->file_index, ist->st->index); \
  745. ret = avfilter_graph_create_filter(&filt_ctx, \
  746. avfilter_get_by_name(filter_name), \
  747. name, arg, NULL, fg->graph); \
  748. if (ret < 0) \
  749. return ret; \
  750. \
  751. ret = avfilter_link(last_filter, 0, filt_ctx, 0); \
  752. if (ret < 0) \
  753. return ret; \
  754. \
  755. last_filter = filt_ctx; \
  756. } while (0)
  757. if (audio_sync_method > 0) {
  758. char args[256] = {0};
  759. av_strlcatf(args, sizeof(args), "async=%d", audio_sync_method);
  760. if (audio_drift_threshold != 0.1)
  761. av_strlcatf(args, sizeof(args), ":min_hard_comp=%f", audio_drift_threshold);
  762. if (!fg->reconfiguration)
  763. av_strlcatf(args, sizeof(args), ":first_pts=0");
  764. AUTO_INSERT_FILTER_INPUT("-async", "aresample", args);
  765. }
  766. // if (ost->audio_channels_mapped) {
  767. // int i;
  768. // AVBPrint pan_buf;
  769. // av_bprint_init(&pan_buf, 256, 8192);
  770. // av_bprintf(&pan_buf, "0x%"PRIx64,
  771. // av_get_default_channel_layout(ost->audio_channels_mapped));
  772. // for (i = 0; i < ost->audio_channels_mapped; i++)
  773. // if (ost->audio_channels_map[i] != -1)
  774. // av_bprintf(&pan_buf, ":c%d=c%d", i, ost->audio_channels_map[i]);
  775. // AUTO_INSERT_FILTER_INPUT("-map_channel", "pan", pan_buf.str);
  776. // av_bprint_finalize(&pan_buf, NULL);
  777. // }
  778. if (audio_volume != 256) {
  779. char args[256];
  780. av_log(NULL, AV_LOG_WARNING, "-vol has been deprecated. Use the volume "
  781. "audio filter instead.\n");
  782. snprintf(args, sizeof(args), "%f", audio_volume / 256.);
  783. AUTO_INSERT_FILTER_INPUT("-vol", "volume", args);
  784. }
  785. snprintf(name, sizeof(name), "trim for input stream %d:%d",
  786. ist->file_index, ist->st->index);
  787. if (copy_ts) {
  788. tsoffset = f->start_time == AV_NOPTS_VALUE ? 0 : f->start_time;
  789. if (!start_at_zero && f->ctx->start_time != AV_NOPTS_VALUE)
  790. tsoffset += f->ctx->start_time;
  791. }
  792. ret = insert_trim(((f->start_time == AV_NOPTS_VALUE) || !f->accurate_seek) ?
  793. AV_NOPTS_VALUE : tsoffset, f->recording_time,
  794. &last_filter, &pad_idx, name);
  795. if (ret < 0)
  796. return ret;
  797. if ((ret = avfilter_link(last_filter, 0, in->filter_ctx, in->pad_idx)) < 0)
  798. return ret;
  799. return 0;
  800. }
  801. static int configure_input_filter(FilterGraph *fg, InputFilter *ifilter,
  802. AVFilterInOut *in)
  803. {
  804. av_freep(&ifilter->name);
  805. DESCRIBE_FILTER_LINK(ifilter, in, 1);
  806. if (!ifilter->ist->dec) {
  807. av_log(NULL, AV_LOG_ERROR,
  808. "No decoder for stream #%d:%d, filtering impossible\n",
  809. ifilter->ist->file_index, ifilter->ist->st->index);
  810. return AVERROR_DECODER_NOT_FOUND;
  811. }
  812. switch (avfilter_pad_get_type(in->filter_ctx->input_pads, in->pad_idx)) {
  813. case AVMEDIA_TYPE_VIDEO: return configure_input_video_filter(fg, ifilter, in);
  814. case AVMEDIA_TYPE_AUDIO: return configure_input_audio_filter(fg, ifilter, in);
  815. default: av_assert0(0);
  816. }
  817. }
  818. int configure_filtergraph(FilterGraph *fg)
  819. {
  820. AVFilterInOut *inputs, *outputs, *cur;
  821. int ret, i, simple = !fg->graph_desc;
  822. const char *graph_desc = simple ? fg->outputs[0]->ost->avfilter :
  823. fg->graph_desc;
  824. avfilter_graph_free(&fg->graph);
  825. if (!(fg->graph = avfilter_graph_alloc()))
  826. return AVERROR(ENOMEM);
  827. if (simple) {
  828. OutputStream *ost = fg->outputs[0]->ost;
  829. char args[512];
  830. AVDictionaryEntry *e = NULL;
  831. snprintf(args, sizeof(args), "flags=0x%X", (unsigned)ost->sws_flags);
  832. fg->graph->scale_sws_opts = av_strdup(args);
  833. args[0] = 0;
  834. while ((e = av_dict_get(ost->swr_opts, "", e,
  835. AV_DICT_IGNORE_SUFFIX))) {
  836. av_strlcatf(args, sizeof(args), "%s=%s:", e->key, e->value);
  837. }
  838. if (strlen(args))
  839. args[strlen(args)-1] = 0;
  840. av_opt_set(fg->graph, "aresample_swr_opts", args, 0);
  841. args[0] = '\0';
  842. while ((e = av_dict_get(fg->outputs[0]->ost->resample_opts, "", e,
  843. AV_DICT_IGNORE_SUFFIX))) {
  844. av_strlcatf(args, sizeof(args), "%s=%s:", e->key, e->value);
  845. }
  846. if (strlen(args))
  847. args[strlen(args) - 1] = '\0';
  848. fg->graph->resample_lavr_opts = av_strdup(args);
  849. e = av_dict_get(ost->encoder_opts, "threads", NULL, 0);
  850. if (e)
  851. av_opt_set(fg->graph, "threads", e->value, 0);
  852. }
  853. if ((ret = avfilter_graph_parse2(fg->graph, graph_desc, &inputs, &outputs)) < 0)
  854. return ret;
  855. if (simple && (!inputs || inputs->next || !outputs || outputs->next)) {
  856. const char *num_inputs;
  857. const char *num_outputs;
  858. if (!outputs) {
  859. num_outputs = "0";
  860. } else if (outputs->next) {
  861. num_outputs = ">1";
  862. } else {
  863. num_outputs = "1";
  864. }
  865. if (!inputs) {
  866. num_inputs = "0";
  867. } else if (inputs->next) {
  868. num_inputs = ">1";
  869. } else {
  870. num_inputs = "1";
  871. }
  872. av_log(NULL, AV_LOG_ERROR, "Simple filtergraph '%s' was expected "
  873. "to have exactly 1 input and 1 output."
  874. " However, it had %s input(s) and %s output(s)."
  875. " Please adjust, or use a complex filtergraph (-filter_complex) instead.\n",
  876. graph_desc, num_inputs, num_outputs);
  877. return AVERROR(EINVAL);
  878. }
  879. for (cur = inputs, i = 0; cur; cur = cur->next, i++)
  880. if ((ret = configure_input_filter(fg, fg->inputs[i], cur)) < 0) {
  881. avfilter_inout_free(&inputs);
  882. avfilter_inout_free(&outputs);
  883. return ret;
  884. }
  885. avfilter_inout_free(&inputs);
  886. for (cur = outputs, i = 0; cur; cur = cur->next, i++)
  887. configure_output_filter(fg, fg->outputs[i], cur);
  888. avfilter_inout_free(&outputs);
  889. if ((ret = avfilter_graph_config(fg->graph, NULL)) < 0)
  890. return ret;
  891. fg->reconfiguration = 1;
  892. for (i = 0; i < fg->nb_outputs; i++) {
  893. OutputStream *ost = fg->outputs[i]->ost;
  894. if (ost &&
  895. ost->enc->type == AVMEDIA_TYPE_AUDIO &&
  896. !(ost->enc->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE))
  897. av_buffersink_set_frame_size(ost->filter->filter,
  898. ost->enc_ctx->frame_size);
  899. }
  900. return 0;
  901. }
  902. int ist_in_filtergraph(FilterGraph *fg, InputStream *ist)
  903. {
  904. int i;
  905. for (i = 0; i < fg->nb_inputs; i++)
  906. if (fg->inputs[i]->ist == ist)
  907. return 1;
  908. return 0;
  909. }