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.

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