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.

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