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.

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