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.

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