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.

1076 lines
41KB

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