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.

561 lines
21KB

  1. /*
  2. * avconv filter configuration
  3. *
  4. * This file is part of Libav.
  5. *
  6. * Libav 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. * Libav 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 Libav; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include "avconv.h"
  21. #include "libavfilter/avfilter.h"
  22. #include "libavfilter/avfiltergraph.h"
  23. #include "libavutil/audioconvert.h"
  24. #include "libavutil/avassert.h"
  25. #include "libavutil/pixdesc.h"
  26. #include "libavutil/pixfmt.h"
  27. #include "libavutil/samplefmt.h"
  28. /**
  29. * Define a function for building a string containing a list of
  30. * allowed formats,
  31. */
  32. #define DEF_CHOOSE_FORMAT(type, var, supported_list, none, get_name, separator)\
  33. static char *choose_ ## var ## s(OutputStream *ost) \
  34. { \
  35. if (ost->st->codec->var != none) { \
  36. get_name(ost->st->codec->var); \
  37. return av_strdup(name); \
  38. } else if (ost->enc->supported_list) { \
  39. const type *p; \
  40. AVIOContext *s = NULL; \
  41. uint8_t *ret; \
  42. int len; \
  43. \
  44. if (avio_open_dyn_buf(&s) < 0) \
  45. exit_program(1); \
  46. \
  47. for (p = ost->enc->supported_list; *p != none; p++) { \
  48. get_name(*p); \
  49. avio_printf(s, "%s" separator, name); \
  50. } \
  51. len = avio_close_dyn_buf(s, &ret); \
  52. ret[len - 1] = 0; \
  53. return ret; \
  54. } else \
  55. return NULL; \
  56. }
  57. #define GET_PIX_FMT_NAME(pix_fmt)\
  58. const char *name = av_get_pix_fmt_name(pix_fmt);
  59. DEF_CHOOSE_FORMAT(enum PixelFormat, pix_fmt, pix_fmts, PIX_FMT_NONE,
  60. GET_PIX_FMT_NAME, ":")
  61. #define GET_SAMPLE_FMT_NAME(sample_fmt)\
  62. const char *name = av_get_sample_fmt_name(sample_fmt)
  63. DEF_CHOOSE_FORMAT(enum AVSampleFormat, sample_fmt, sample_fmts,
  64. AV_SAMPLE_FMT_NONE, GET_SAMPLE_FMT_NAME, ",")
  65. #define GET_SAMPLE_RATE_NAME(rate)\
  66. char name[16];\
  67. snprintf(name, sizeof(name), "%d", rate);
  68. DEF_CHOOSE_FORMAT(int, sample_rate, supported_samplerates, 0,
  69. GET_SAMPLE_RATE_NAME, ",")
  70. #define GET_CH_LAYOUT_NAME(ch_layout)\
  71. char name[16];\
  72. snprintf(name, sizeof(name), "0x%"PRIx64, ch_layout);
  73. DEF_CHOOSE_FORMAT(uint64_t, channel_layout, channel_layouts, 0,
  74. GET_CH_LAYOUT_NAME, ",")
  75. FilterGraph *init_simple_filtergraph(InputStream *ist, OutputStream *ost)
  76. {
  77. FilterGraph *fg = av_mallocz(sizeof(*fg));
  78. if (!fg)
  79. exit_program(1);
  80. fg->index = nb_filtergraphs;
  81. fg->outputs = grow_array(fg->outputs, sizeof(*fg->outputs), &fg->nb_outputs,
  82. fg->nb_outputs + 1);
  83. if (!(fg->outputs[0] = av_mallocz(sizeof(*fg->outputs[0]))))
  84. exit_program(1);
  85. fg->outputs[0]->ost = ost;
  86. fg->outputs[0]->graph = fg;
  87. ost->filter = fg->outputs[0];
  88. fg->inputs = grow_array(fg->inputs, sizeof(*fg->inputs), &fg->nb_inputs,
  89. fg->nb_inputs + 1);
  90. if (!(fg->inputs[0] = av_mallocz(sizeof(*fg->inputs[0]))))
  91. exit_program(1);
  92. fg->inputs[0]->ist = ist;
  93. fg->inputs[0]->graph = fg;
  94. ist->filters = grow_array(ist->filters, sizeof(*ist->filters),
  95. &ist->nb_filters, ist->nb_filters + 1);
  96. ist->filters[ist->nb_filters - 1] = fg->inputs[0];
  97. filtergraphs = grow_array(filtergraphs, sizeof(*filtergraphs),
  98. &nb_filtergraphs, nb_filtergraphs + 1);
  99. filtergraphs[nb_filtergraphs - 1] = fg;
  100. return fg;
  101. }
  102. static void init_input_filter(FilterGraph *fg, AVFilterInOut *in)
  103. {
  104. InputStream *ist = NULL;
  105. enum AVMediaType type = avfilter_pad_get_type(in->filter_ctx->input_pads, in->pad_idx);
  106. int i;
  107. // TODO: support other filter types
  108. if (type != AVMEDIA_TYPE_VIDEO && type != AVMEDIA_TYPE_AUDIO) {
  109. av_log(NULL, AV_LOG_FATAL, "Only video and audio filters supported "
  110. "currently.\n");
  111. exit_program(1);
  112. }
  113. if (in->name) {
  114. AVFormatContext *s;
  115. AVStream *st = NULL;
  116. char *p;
  117. int file_idx = strtol(in->name, &p, 0);
  118. if (file_idx < 0 || file_idx >= nb_input_files) {
  119. av_log(NULL, AV_LOG_FATAL, "Invalid file index %d in filtegraph description %s.\n",
  120. file_idx, fg->graph_desc);
  121. exit_program(1);
  122. }
  123. s = input_files[file_idx]->ctx;
  124. for (i = 0; i < s->nb_streams; i++) {
  125. if (s->streams[i]->codec->codec_type != type)
  126. continue;
  127. if (check_stream_specifier(s, s->streams[i], *p == ':' ? p + 1 : p) == 1) {
  128. st = s->streams[i];
  129. break;
  130. }
  131. }
  132. if (!st) {
  133. av_log(NULL, AV_LOG_FATAL, "Stream specifier '%s' in filtergraph description %s "
  134. "matches no streams.\n", p, fg->graph_desc);
  135. exit_program(1);
  136. }
  137. ist = input_streams[input_files[file_idx]->ist_index + st->index];
  138. } else {
  139. /* find the first unused stream of corresponding type */
  140. for (i = 0; i < nb_input_streams; i++) {
  141. ist = input_streams[i];
  142. if (ist->st->codec->codec_type == type && ist->discard)
  143. break;
  144. }
  145. if (i == nb_input_streams) {
  146. av_log(NULL, AV_LOG_FATAL, "Cannot find a matching stream for "
  147. "unlabeled input pad %d on filter %s", in->pad_idx,
  148. in->filter_ctx->name);
  149. exit_program(1);
  150. }
  151. }
  152. av_assert0(ist);
  153. ist->discard = 0;
  154. ist->decoding_needed = 1;
  155. ist->st->discard = AVDISCARD_NONE;
  156. fg->inputs = grow_array(fg->inputs, sizeof(*fg->inputs),
  157. &fg->nb_inputs, fg->nb_inputs + 1);
  158. if (!(fg->inputs[fg->nb_inputs - 1] = av_mallocz(sizeof(*fg->inputs[0]))))
  159. exit_program(1);
  160. fg->inputs[fg->nb_inputs - 1]->ist = ist;
  161. fg->inputs[fg->nb_inputs - 1]->graph = fg;
  162. ist->filters = grow_array(ist->filters, sizeof(*ist->filters),
  163. &ist->nb_filters, ist->nb_filters + 1);
  164. ist->filters[ist->nb_filters - 1] = fg->inputs[fg->nb_inputs - 1];
  165. }
  166. static int configure_output_video_filter(FilterGraph *fg, OutputFilter *ofilter, AVFilterInOut *out)
  167. {
  168. char *pix_fmts;
  169. OutputStream *ost = ofilter->ost;
  170. AVCodecContext *codec = ost->st->codec;
  171. AVFilterContext *last_filter = out->filter_ctx;
  172. int pad_idx = out->pad_idx;
  173. int ret;
  174. char name[255];
  175. snprintf(name, sizeof(name), "output stream %d:%d", ost->file_index, ost->index);
  176. ret = avfilter_graph_create_filter(&ofilter->filter,
  177. avfilter_get_by_name("buffersink"),
  178. name, NULL, pix_fmts, fg->graph);
  179. if (ret < 0)
  180. return ret;
  181. if (codec->width || codec->height) {
  182. char args[255];
  183. AVFilterContext *filter;
  184. snprintf(args, sizeof(args), "%d:%d:flags=0x%X",
  185. codec->width,
  186. codec->height,
  187. (unsigned)ost->sws_flags);
  188. snprintf(name, sizeof(name), "scaler for output stream %d:%d",
  189. ost->file_index, ost->index);
  190. if ((ret = avfilter_graph_create_filter(&filter, avfilter_get_by_name("scale"),
  191. name, args, NULL, fg->graph)) < 0)
  192. return ret;
  193. if ((ret = avfilter_link(last_filter, pad_idx, filter, 0)) < 0)
  194. return ret;
  195. last_filter = filter;
  196. pad_idx = 0;
  197. }
  198. if ((pix_fmts = choose_pix_fmts(ost))) {
  199. AVFilterContext *filter;
  200. snprintf(name, sizeof(name), "pixel format for output stream %d:%d",
  201. ost->file_index, ost->index);
  202. if ((ret = avfilter_graph_create_filter(&filter,
  203. avfilter_get_by_name("format"),
  204. "format", pix_fmts, NULL,
  205. fg->graph)) < 0)
  206. return ret;
  207. if ((ret = avfilter_link(last_filter, pad_idx, filter, 0)) < 0)
  208. return ret;
  209. last_filter = filter;
  210. pad_idx = 0;
  211. av_freep(&pix_fmts);
  212. }
  213. if (ost->frame_rate.num) {
  214. AVFilterContext *fps;
  215. char args[255];
  216. snprintf(args, sizeof(args), "fps=%d/%d", ost->frame_rate.num,
  217. ost->frame_rate.den);
  218. snprintf(name, sizeof(name), "fps for output stream %d:%d",
  219. ost->file_index, ost->index);
  220. ret = avfilter_graph_create_filter(&fps, avfilter_get_by_name("fps"),
  221. name, args, NULL, fg->graph);
  222. if (ret < 0)
  223. return ret;
  224. ret = avfilter_link(last_filter, pad_idx, fps, 0);
  225. if (ret < 0)
  226. return ret;
  227. last_filter = fps;
  228. pad_idx = 0;
  229. }
  230. if ((ret = avfilter_link(last_filter, pad_idx, ofilter->filter, 0)) < 0)
  231. return ret;
  232. return 0;
  233. }
  234. static int configure_output_audio_filter(FilterGraph *fg, OutputFilter *ofilter, AVFilterInOut *out)
  235. {
  236. OutputStream *ost = ofilter->ost;
  237. AVCodecContext *codec = ost->st->codec;
  238. AVFilterContext *last_filter = out->filter_ctx;
  239. int pad_idx = out->pad_idx;
  240. char *sample_fmts, *sample_rates, *channel_layouts;
  241. char name[255];
  242. int ret;
  243. snprintf(name, sizeof(name), "output stream %d:%d", ost->file_index, ost->index);
  244. ret = avfilter_graph_create_filter(&ofilter->filter,
  245. avfilter_get_by_name("abuffersink"),
  246. name, NULL, NULL, fg->graph);
  247. if (ret < 0)
  248. return ret;
  249. if (codec->channels && !codec->channel_layout)
  250. codec->channel_layout = av_get_default_channel_layout(codec->channels);
  251. sample_fmts = choose_sample_fmts(ost);
  252. sample_rates = choose_sample_rates(ost);
  253. channel_layouts = choose_channel_layouts(ost);
  254. if (sample_fmts || sample_rates || channel_layouts) {
  255. AVFilterContext *format;
  256. char args[256];
  257. int len = 0;
  258. if (sample_fmts)
  259. len += snprintf(args + len, sizeof(args) - len, "sample_fmts=%s:",
  260. sample_fmts);
  261. if (sample_rates)
  262. len += snprintf(args + len, sizeof(args) - len, "sample_rates=%s:",
  263. sample_rates);
  264. if (channel_layouts)
  265. len += snprintf(args + len, sizeof(args) - len, "channel_layouts=%s:",
  266. channel_layouts);
  267. args[len - 1] = 0;
  268. av_freep(&sample_fmts);
  269. av_freep(&sample_rates);
  270. av_freep(&channel_layouts);
  271. snprintf(name, sizeof(name), "audio format for output stream %d:%d",
  272. ost->file_index, ost->index);
  273. ret = avfilter_graph_create_filter(&format,
  274. avfilter_get_by_name("aformat"),
  275. name, args, NULL, fg->graph);
  276. if (ret < 0)
  277. return ret;
  278. ret = avfilter_link(last_filter, pad_idx, format, 0);
  279. if (ret < 0)
  280. return ret;
  281. last_filter = format;
  282. pad_idx = 0;
  283. }
  284. if ((ret = avfilter_link(last_filter, pad_idx, ofilter->filter, 0)) < 0)
  285. return ret;
  286. return 0;
  287. }
  288. #define DESCRIBE_FILTER_LINK(f, inout, in) \
  289. { \
  290. AVFilterContext *ctx = inout->filter_ctx; \
  291. AVFilterPad *pads = in ? ctx->input_pads : ctx->output_pads; \
  292. int nb_pads = in ? ctx->input_count : ctx->output_count; \
  293. AVIOContext *pb; \
  294. \
  295. if (avio_open_dyn_buf(&pb) < 0) \
  296. exit_program(1); \
  297. \
  298. avio_printf(pb, "%s", ctx->filter->name); \
  299. if (nb_pads > 1) \
  300. avio_printf(pb, ":%s", avfilter_pad_get_name(pads, inout->pad_idx));\
  301. avio_w8(pb, 0); \
  302. avio_close_dyn_buf(pb, &f->name); \
  303. }
  304. int configure_output_filter(FilterGraph *fg, OutputFilter *ofilter, AVFilterInOut *out)
  305. {
  306. av_freep(&ofilter->name);
  307. DESCRIBE_FILTER_LINK(ofilter, out, 0);
  308. switch (avfilter_pad_get_type(out->filter_ctx->output_pads, out->pad_idx)) {
  309. case AVMEDIA_TYPE_VIDEO: return configure_output_video_filter(fg, ofilter, out);
  310. case AVMEDIA_TYPE_AUDIO: return configure_output_audio_filter(fg, ofilter, out);
  311. default: av_assert0(0);
  312. }
  313. }
  314. static int configure_input_video_filter(FilterGraph *fg, InputFilter *ifilter,
  315. AVFilterInOut *in)
  316. {
  317. AVFilterContext *first_filter = in->filter_ctx;
  318. AVFilter *filter = avfilter_get_by_name("buffer");
  319. InputStream *ist = ifilter->ist;
  320. AVRational tb = ist->framerate.num ? av_inv_q(ist->framerate) :
  321. ist->st->time_base;
  322. AVRational sar;
  323. char args[255], name[255];
  324. int pad_idx = in->pad_idx;
  325. int ret;
  326. sar = ist->st->sample_aspect_ratio.num ?
  327. ist->st->sample_aspect_ratio :
  328. ist->st->codec->sample_aspect_ratio;
  329. snprintf(args, sizeof(args), "%d:%d:%d:%d:%d:%d:%d", ist->st->codec->width,
  330. ist->st->codec->height, ist->st->codec->pix_fmt,
  331. tb.num, tb.den, sar.num, sar.den);
  332. snprintf(name, sizeof(name), "graph %d input from stream %d:%d", fg->index,
  333. ist->file_index, ist->st->index);
  334. if ((ret = avfilter_graph_create_filter(&ifilter->filter, filter, name,
  335. args, NULL, fg->graph)) < 0)
  336. return ret;
  337. if (ist->framerate.num) {
  338. AVFilterContext *setpts;
  339. snprintf(name, sizeof(name), "force CFR for input from stream %d:%d",
  340. ist->file_index, ist->st->index);
  341. if ((ret = avfilter_graph_create_filter(&setpts,
  342. avfilter_get_by_name("setpts"),
  343. name, "N", NULL,
  344. fg->graph)) < 0)
  345. return ret;
  346. if ((ret = avfilter_link(setpts, 0, first_filter, pad_idx)) < 0)
  347. return ret;
  348. first_filter = setpts;
  349. pad_idx = 0;
  350. }
  351. if ((ret = avfilter_link(ifilter->filter, 0, first_filter, pad_idx)) < 0)
  352. return ret;
  353. return 0;
  354. }
  355. static int configure_input_audio_filter(FilterGraph *fg, InputFilter *ifilter,
  356. AVFilterInOut *in)
  357. {
  358. AVFilterContext *first_filter = in->filter_ctx;
  359. AVFilter *filter = avfilter_get_by_name("abuffer");
  360. InputStream *ist = ifilter->ist;
  361. int pad_idx = in->pad_idx;
  362. char args[255], name[255];
  363. int ret;
  364. snprintf(args, sizeof(args), "time_base=%d/%d:sample_rate=%d:sample_fmt=%s"
  365. ":channel_layout=0x%"PRIx64,
  366. 1, ist->st->codec->sample_rate,
  367. ist->st->codec->sample_rate,
  368. av_get_sample_fmt_name(ist->st->codec->sample_fmt),
  369. ist->st->codec->channel_layout);
  370. snprintf(name, sizeof(name), "graph %d input from stream %d:%d", fg->index,
  371. ist->file_index, ist->st->index);
  372. if ((ret = avfilter_graph_create_filter(&ifilter->filter, filter,
  373. name, args, NULL,
  374. fg->graph)) < 0)
  375. return ret;
  376. if (audio_sync_method > 0) {
  377. AVFilterContext *async;
  378. char args[256];
  379. int len = 0;
  380. av_log(NULL, AV_LOG_WARNING, "-async has been deprecated. Used the "
  381. "asyncts audio filter instead.\n");
  382. if (audio_sync_method > 1)
  383. len += snprintf(args + len, sizeof(args) - len, "compensate=1:"
  384. "max_comp=%d:", audio_sync_method);
  385. snprintf(args + len, sizeof(args) - len, "min_delta=%f",
  386. audio_drift_threshold);
  387. snprintf(name, sizeof(name), "graph %d audio sync for input stream %d:%d",
  388. fg->index, ist->file_index, ist->st->index);
  389. ret = avfilter_graph_create_filter(&async,
  390. avfilter_get_by_name("asyncts"),
  391. name, args, NULL, fg->graph);
  392. if (ret < 0)
  393. return ret;
  394. ret = avfilter_link(async, 0, first_filter, pad_idx);
  395. if (ret < 0)
  396. return ret;
  397. first_filter = async;
  398. pad_idx = 0;
  399. }
  400. if ((ret = avfilter_link(ifilter->filter, 0, first_filter, pad_idx)) < 0)
  401. return ret;
  402. return 0;
  403. }
  404. static int configure_input_filter(FilterGraph *fg, InputFilter *ifilter,
  405. AVFilterInOut *in)
  406. {
  407. av_freep(&ifilter->name);
  408. DESCRIBE_FILTER_LINK(ifilter, in, 1);
  409. switch (avfilter_pad_get_type(in->filter_ctx->input_pads, in->pad_idx)) {
  410. case AVMEDIA_TYPE_VIDEO: return configure_input_video_filter(fg, ifilter, in);
  411. case AVMEDIA_TYPE_AUDIO: return configure_input_audio_filter(fg, ifilter, in);
  412. default: av_assert0(0);
  413. }
  414. }
  415. int configure_filtergraph(FilterGraph *fg)
  416. {
  417. AVFilterInOut *inputs, *outputs, *cur;
  418. int ret, i, init = !fg->graph, simple = !fg->graph_desc;
  419. const char *graph_desc = simple ? fg->outputs[0]->ost->avfilter :
  420. fg->graph_desc;
  421. avfilter_graph_free(&fg->graph);
  422. if (!(fg->graph = avfilter_graph_alloc()))
  423. return AVERROR(ENOMEM);
  424. if (simple) {
  425. OutputStream *ost = fg->outputs[0]->ost;
  426. char args[255];
  427. snprintf(args, sizeof(args), "flags=0x%X", (unsigned)ost->sws_flags);
  428. fg->graph->scale_sws_opts = av_strdup(args);
  429. }
  430. if ((ret = avfilter_graph_parse2(fg->graph, graph_desc, &inputs, &outputs)) < 0)
  431. return ret;
  432. if (simple && (!inputs || inputs->next || !outputs || outputs->next)) {
  433. av_log(NULL, AV_LOG_ERROR, "Simple filtergraph '%s' does not have "
  434. "exactly one input and output.\n", graph_desc);
  435. return AVERROR(EINVAL);
  436. }
  437. for (cur = inputs; !simple && init && cur; cur = cur->next)
  438. init_input_filter(fg, cur);
  439. for (cur = inputs, i = 0; cur; cur = cur->next, i++)
  440. if ((ret = configure_input_filter(fg, fg->inputs[i], cur)) < 0)
  441. return ret;
  442. avfilter_inout_free(&inputs);
  443. if (!init || simple) {
  444. /* we already know the mappings between lavfi outputs and output streams,
  445. * so we can finish the setup */
  446. for (cur = outputs, i = 0; cur; cur = cur->next, i++)
  447. configure_output_filter(fg, fg->outputs[i], cur);
  448. avfilter_inout_free(&outputs);
  449. if ((ret = avfilter_graph_config(fg->graph, NULL)) < 0)
  450. return ret;
  451. } else {
  452. /* wait until output mappings are processed */
  453. for (cur = outputs; cur;) {
  454. fg->outputs = grow_array(fg->outputs, sizeof(*fg->outputs),
  455. &fg->nb_outputs, fg->nb_outputs + 1);
  456. if (!(fg->outputs[fg->nb_outputs - 1] = av_mallocz(sizeof(*fg->outputs[0]))))
  457. exit_program(1);
  458. fg->outputs[fg->nb_outputs - 1]->graph = fg;
  459. fg->outputs[fg->nb_outputs - 1]->out_tmp = cur;
  460. cur = cur->next;
  461. fg->outputs[fg->nb_outputs - 1]->out_tmp->next = NULL;
  462. }
  463. }
  464. return 0;
  465. }
  466. int ist_in_filtergraph(FilterGraph *fg, InputStream *ist)
  467. {
  468. int i;
  469. for (i = 0; i < fg->nb_inputs; i++)
  470. if (fg->inputs[i]->ist == ist)
  471. return 1;
  472. return 0;
  473. }