lavfi.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. /*
  2. * Copyright (c) 2011 Stefano Sabatini
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg 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. * FFmpeg 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 FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /**
  21. * @file
  22. * libavfilter virtual input device
  23. */
  24. /* #define DEBUG */
  25. #include "float.h" /* DBL_MIN, DBL_MAX */
  26. #include "libavutil/log.h"
  27. #include "libavutil/mem.h"
  28. #include "libavutil/opt.h"
  29. #include "libavutil/parseutils.h"
  30. #include "libavutil/pixdesc.h"
  31. #include "libavfilter/avfilter.h"
  32. #include "libavfilter/avfiltergraph.h"
  33. #include "libavfilter/buffersink.h"
  34. #include "avdevice.h"
  35. typedef struct {
  36. AVClass *class; ///< class for private options
  37. char *graph_str;
  38. AVFilterGraph *graph;
  39. AVFilterContext **sinks;
  40. int *sink_stream_map;
  41. int *stream_sink_map;
  42. } LavfiContext;
  43. static int *create_all_formats(int n)
  44. {
  45. int i, j, *fmts, count = 0;
  46. for (i = 0; i < n; i++)
  47. if (!(av_pix_fmt_descriptors[i].flags & PIX_FMT_HWACCEL))
  48. count++;
  49. if (!(fmts = av_malloc((count+1) * sizeof(int))))
  50. return NULL;
  51. for (j = 0, i = 0; i < n; i++) {
  52. if (!(av_pix_fmt_descriptors[i].flags & PIX_FMT_HWACCEL))
  53. fmts[j++] = i;
  54. }
  55. fmts[j] = -1;
  56. return fmts;
  57. }
  58. av_cold static int lavfi_read_close(AVFormatContext *avctx)
  59. {
  60. LavfiContext *lavfi = avctx->priv_data;
  61. av_freep(&lavfi->sink_stream_map);
  62. av_freep(&lavfi->stream_sink_map);
  63. avfilter_graph_free(&lavfi->graph);
  64. return 0;
  65. }
  66. av_cold static int lavfi_read_header(AVFormatContext *avctx,
  67. AVFormatParameters *ap)
  68. {
  69. LavfiContext *lavfi = avctx->priv_data;
  70. AVFilterInOut *input_links = NULL, *output_links = NULL, *inout;
  71. AVFilter *buffersink, *abuffersink;
  72. int *pix_fmts = create_all_formats(PIX_FMT_NB);
  73. enum AVMediaType type;
  74. int ret = 0, i, n;
  75. #define FAIL(ERR) { ret = ERR; goto end; }
  76. avfilter_register_all();
  77. buffersink = avfilter_get_by_name("buffersink");
  78. abuffersink = avfilter_get_by_name("abuffersink");
  79. if (!lavfi->graph_str)
  80. lavfi->graph_str = av_strdup(avctx->filename);
  81. /* parse the graph, create a stream for each open output */
  82. if (!(lavfi->graph = avfilter_graph_alloc()))
  83. FAIL(AVERROR(ENOMEM));
  84. if ((ret = avfilter_graph_parse(lavfi->graph, lavfi->graph_str,
  85. &input_links, &output_links, avctx)) < 0)
  86. FAIL(ret);
  87. if (input_links) {
  88. av_log(avctx, AV_LOG_ERROR,
  89. "Open inputs in the filtergraph are not acceptable\n");
  90. FAIL(AVERROR(EINVAL));
  91. }
  92. /* count the outputs */
  93. for (n = 0, inout = output_links; inout; n++, inout = inout->next);
  94. if (!(lavfi->sink_stream_map = av_malloc(sizeof(int) * n)))
  95. FAIL(AVERROR(ENOMEM));
  96. if (!(lavfi->stream_sink_map = av_malloc(sizeof(int) * n)))
  97. FAIL(AVERROR(ENOMEM));
  98. for (i = 0; i < n; i++)
  99. lavfi->stream_sink_map[i] = -1;
  100. /* parse the output link names - they need to be of the form out0, out1, ...
  101. * create a mapping between them and the streams */
  102. for (i = 0, inout = output_links; inout; i++, inout = inout->next) {
  103. int stream_idx;
  104. if (!strcmp(inout->name, "out"))
  105. stream_idx = 0;
  106. else if (sscanf(inout->name, "out%d\n", &stream_idx) != 1) {
  107. av_log(avctx, AV_LOG_ERROR,
  108. "Invalid outpad name '%s'\n", inout->name);
  109. FAIL(AVERROR(EINVAL));
  110. }
  111. if ((unsigned)stream_idx >= n) {
  112. av_log(avctx, AV_LOG_ERROR,
  113. "Invalid index was specified in output '%s', "
  114. "must be a non-negative value < %d\n",
  115. inout->name, n);
  116. FAIL(AVERROR(EINVAL));
  117. }
  118. /* is a video output? */
  119. type = inout->filter_ctx->output_pads[inout->pad_idx].type;
  120. if (type != AVMEDIA_TYPE_VIDEO && type != AVMEDIA_TYPE_AUDIO) {
  121. av_log(avctx, AV_LOG_ERROR,
  122. "Output '%s' is not a video or audio output, not yet supported\n", inout->name);
  123. FAIL(AVERROR(EINVAL));
  124. }
  125. if (lavfi->stream_sink_map[stream_idx] != -1) {
  126. av_log(avctx, AV_LOG_ERROR,
  127. "An with stream index %d was already specified\n",
  128. stream_idx);
  129. FAIL(AVERROR(EINVAL));
  130. }
  131. lavfi->sink_stream_map[i] = stream_idx;
  132. lavfi->stream_sink_map[stream_idx] = i;
  133. }
  134. /* for each open output create a corresponding stream */
  135. for (i = 0, inout = output_links; inout; i++, inout = inout->next) {
  136. AVStream *st;
  137. if (!(st = av_new_stream(avctx, i)))
  138. FAIL(AVERROR(ENOMEM));
  139. }
  140. /* create a sink for each output and connect them to the graph */
  141. lavfi->sinks = av_malloc(sizeof(AVFilterContext *) * avctx->nb_streams);
  142. if (!lavfi->sinks)
  143. FAIL(AVERROR(ENOMEM));
  144. for (i = 0, inout = output_links; inout; i++, inout = inout->next) {
  145. AVFilterContext *sink;
  146. type = inout->filter_ctx->output_pads[inout->pad_idx].type;
  147. if (type == AVMEDIA_TYPE_VIDEO && ! buffersink ||
  148. type == AVMEDIA_TYPE_AUDIO && ! abuffersink) {
  149. av_log(avctx, AV_LOG_ERROR, "Missing required buffersink filter, aborting.\n");
  150. FAIL(AVERROR_FILTER_NOT_FOUND);
  151. }
  152. if (type == AVMEDIA_TYPE_VIDEO) {
  153. AVBufferSinkParams *buffersink_params = av_buffersink_params_alloc();
  154. buffersink_params->pixel_fmts = pix_fmts;
  155. #if FF_API_OLD_VSINK_API
  156. ret = avfilter_graph_create_filter(&sink, buffersink,
  157. inout->name, NULL,
  158. pix_fmts, lavfi->graph);
  159. #else
  160. buffersink_params->pixel_fmts = pix_fmts;
  161. ret = avfilter_graph_create_filter(&sink, buffersink,
  162. inout->name, NULL,
  163. buffersink_params, lavfi->graph);
  164. #endif
  165. av_freep(&buffersink_params);
  166. if (ret < 0)
  167. goto end;
  168. } else if (type == AVMEDIA_TYPE_AUDIO) {
  169. enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_S16, -1 };
  170. const int packing_fmts[] = { AVFILTER_PACKED, -1 };
  171. const int64_t *chlayouts = avfilter_all_channel_layouts;
  172. AVABufferSinkParams *abuffersink_params = av_abuffersink_params_alloc();
  173. abuffersink_params->sample_fmts = sample_fmts;
  174. abuffersink_params->packing_fmts = packing_fmts;
  175. abuffersink_params->channel_layouts = chlayouts;
  176. ret = avfilter_graph_create_filter(&sink, abuffersink,
  177. inout->name, NULL,
  178. abuffersink_params, lavfi->graph);
  179. av_free(abuffersink_params);
  180. if (ret < 0)
  181. goto end;
  182. }
  183. lavfi->sinks[i] = sink;
  184. if ((ret = avfilter_link(inout->filter_ctx, inout->pad_idx, sink, 0)) < 0)
  185. FAIL(ret);
  186. }
  187. /* configure the graph */
  188. if ((ret = avfilter_graph_config(lavfi->graph, avctx)) < 0)
  189. FAIL(ret);
  190. /* fill each stream with the information in the corresponding sink */
  191. for (i = 0; i < avctx->nb_streams; i++) {
  192. AVFilterLink *link = lavfi->sinks[lavfi->stream_sink_map[i]]->inputs[0];
  193. AVStream *st = avctx->streams[i];
  194. st->codec->codec_type = link->type;
  195. av_set_pts_info(st, 64, link->time_base.num, link->time_base.den);
  196. if (link->type == AVMEDIA_TYPE_VIDEO) {
  197. st->codec->codec_id = CODEC_ID_RAWVIDEO;
  198. st->codec->pix_fmt = link->format;
  199. st->codec->time_base = link->time_base;
  200. st->codec->width = link->w;
  201. st->codec->height = link->h;
  202. st ->sample_aspect_ratio =
  203. st->codec->sample_aspect_ratio = link->sample_aspect_ratio;
  204. } else if (link->type == AVMEDIA_TYPE_AUDIO) {
  205. st->codec->codec_id = CODEC_ID_PCM_S16LE;
  206. st->codec->channels = av_get_channel_layout_nb_channels(link->channel_layout);
  207. st->codec->sample_fmt = link->format;
  208. st->codec->sample_rate = link->sample_rate;
  209. st->codec->time_base = link->time_base;
  210. st->codec->channel_layout = link->channel_layout;
  211. }
  212. }
  213. end:
  214. avfilter_inout_free(&input_links);
  215. avfilter_inout_free(&output_links);
  216. if (ret < 0)
  217. lavfi_read_close(avctx);
  218. return ret;
  219. }
  220. static int lavfi_read_packet(AVFormatContext *avctx, AVPacket *pkt)
  221. {
  222. LavfiContext *lavfi = avctx->priv_data;
  223. double min_pts = DBL_MAX;
  224. int stream_idx, min_pts_sink_idx = 0;
  225. AVFilterBufferRef *ref;
  226. AVPicture pict;
  227. int ret, i, size;
  228. /* iterate through all the graph sinks. Select the sink with the
  229. * minimum PTS */
  230. for (i = 0; i < avctx->nb_streams; i++) {
  231. AVRational tb = lavfi->sinks[i]->inputs[0]->time_base;
  232. double d;
  233. int ret = av_buffersink_get_buffer_ref(lavfi->sinks[i],
  234. &ref, AV_BUFFERSINK_FLAG_PEEK);
  235. if (ret < 0)
  236. return ret;
  237. d = av_rescale_q(ref->pts, tb, AV_TIME_BASE_Q);
  238. av_dlog(avctx, "sink_idx:%d time:%f\n", i, d);
  239. if (d < min_pts) {
  240. min_pts = d;
  241. min_pts_sink_idx = i;
  242. }
  243. }
  244. av_dlog(avctx, "min_pts_sink_idx:%i\n", min_pts_sink_idx);
  245. av_buffersink_get_buffer_ref(lavfi->sinks[min_pts_sink_idx], &ref, 0);
  246. stream_idx = lavfi->sink_stream_map[min_pts_sink_idx];
  247. if (ref->video) {
  248. size = avpicture_get_size(ref->format, ref->video->w, ref->video->h);
  249. if ((ret = av_new_packet(pkt, size)) < 0)
  250. return ret;
  251. memcpy(pict.data, ref->data, 4*sizeof(ref->data[0]));
  252. memcpy(pict.linesize, ref->linesize, 4*sizeof(ref->linesize[0]));
  253. avpicture_layout(&pict, ref->format, ref->video->w,
  254. ref->video->h, pkt->data, size);
  255. } else if (ref->audio) {
  256. size = ref->audio->nb_samples *
  257. av_get_bytes_per_sample(ref->format) *
  258. av_get_channel_layout_nb_channels(ref->audio->channel_layout);
  259. if ((ret = av_new_packet(pkt, size)) < 0)
  260. return ret;
  261. memcpy(pkt->data, ref->data[0], size);
  262. }
  263. pkt->stream_index = stream_idx;
  264. pkt->pts = ref->pts;
  265. pkt->pos = ref->pos;
  266. pkt->size = size;
  267. avfilter_unref_buffer(ref);
  268. return size;
  269. }
  270. #define OFFSET(x) offsetof(LavfiContext, x)
  271. #define DEC AV_OPT_FLAG_DECODING_PARAM
  272. static const AVOption options[] = {
  273. { "graph", "Libavfilter graph", OFFSET(graph_str), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, DEC },
  274. { NULL },
  275. };
  276. static const AVClass lavfi_class = {
  277. .class_name = "lavfi indev",
  278. .item_name = av_default_item_name,
  279. .option = options,
  280. .version = LIBAVUTIL_VERSION_INT,
  281. };
  282. AVInputFormat ff_lavfi_demuxer = {
  283. .name = "lavfi",
  284. .long_name = NULL_IF_CONFIG_SMALL("Libavfilter virtual input device"),
  285. .priv_data_size = sizeof(LavfiContext),
  286. .read_header = lavfi_read_header,
  287. .read_packet = lavfi_read_packet,
  288. .read_close = lavfi_read_close,
  289. .flags = AVFMT_NOFILE,
  290. .priv_class = &lavfi_class,
  291. };