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.

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