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.

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