lavfi.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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/vsink_buffer.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;
  72. int *pix_fmts = create_all_formats(PIX_FMT_NB);
  73. int ret = 0, i, n;
  74. #define FAIL(ERR) { ret = ERR; goto end; }
  75. avfilter_register_all();
  76. if (!(buffersink = avfilter_get_by_name("buffersink"))) {
  77. av_log(avctx, AV_LOG_ERROR,
  78. "Missing required buffersink filter, aborting.\n");
  79. FAIL(AVERROR_FILTER_NOT_FOUND);
  80. }
  81. if (!lavfi->graph_str)
  82. lavfi->graph_str = av_strdup(avctx->filename);
  83. /* parse the graph, create a stream for each open output */
  84. if (!(lavfi->graph = avfilter_graph_alloc()))
  85. FAIL(AVERROR(ENOMEM));
  86. if ((ret = avfilter_graph_parse(lavfi->graph, lavfi->graph_str,
  87. &input_links, &output_links, avctx)) < 0)
  88. FAIL(ret);
  89. if (input_links) {
  90. av_log(avctx, AV_LOG_ERROR,
  91. "Open inputs in the filtergraph are not acceptable\n");
  92. FAIL(AVERROR(EINVAL));
  93. }
  94. /* count the outputs */
  95. for (n = 0, inout = output_links; inout; n++, inout = inout->next);
  96. if (!(lavfi->sink_stream_map = av_malloc(sizeof(int) * n)))
  97. FAIL(AVERROR(ENOMEM));
  98. if (!(lavfi->stream_sink_map = av_malloc(sizeof(int) * n)))
  99. FAIL(AVERROR(ENOMEM));
  100. for (i = 0; i < n; i++)
  101. lavfi->stream_sink_map[i] = -1;
  102. /* parse the output link names - they need to be of the form out0, out1, ...
  103. * create a mapping between them and the streams */
  104. for (i = 0, inout = output_links; inout; i++, inout = inout->next) {
  105. int stream_idx;
  106. if (!strcmp(inout->name, "out"))
  107. stream_idx = 0;
  108. else if (sscanf(inout->name, "out%d\n", &stream_idx) != 1) {
  109. av_log(avctx, AV_LOG_ERROR,
  110. "Invalid outpad name '%s'\n", inout->name);
  111. FAIL(AVERROR(EINVAL));
  112. }
  113. if ((unsigned)stream_idx >= n) {
  114. av_log(avctx, AV_LOG_ERROR,
  115. "Invalid index was specified in output '%s', "
  116. "must be a non-negative value < %d\n",
  117. inout->name, n);
  118. FAIL(AVERROR(EINVAL));
  119. }
  120. /* is a video output? */
  121. if (inout->filter_ctx->output_pads[inout->pad_idx].type != AVMEDIA_TYPE_VIDEO) {
  122. av_log(avctx, AV_LOG_ERROR,
  123. "Output '%s' is not a video output, not yet supported", inout->name);
  124. FAIL(AVERROR(EINVAL));
  125. }
  126. if (lavfi->stream_sink_map[stream_idx] != -1) {
  127. av_log(avctx, AV_LOG_ERROR,
  128. "An with stream index %d was already specified\n",
  129. stream_idx);
  130. FAIL(AVERROR(EINVAL));
  131. }
  132. lavfi->sink_stream_map[i] = stream_idx;
  133. lavfi->stream_sink_map[stream_idx] = i;
  134. }
  135. /* for each open output create a corresponding stream */
  136. for (i = 0, inout = output_links; inout; i++, inout = inout->next) {
  137. AVStream *st;
  138. if (!(st = av_new_stream(avctx, i)))
  139. FAIL(AVERROR(ENOMEM));
  140. }
  141. /* create a sink for each output and connect them to the graph */
  142. lavfi->sinks = av_malloc(sizeof(AVFilterContext *) * avctx->nb_streams);
  143. if (!lavfi->sinks)
  144. FAIL(AVERROR(ENOMEM));
  145. for (i = 0, inout = output_links; inout; i++, inout = inout->next) {
  146. AVFilterContext *sink;
  147. if ((ret = avfilter_graph_create_filter(&sink, buffersink,
  148. inout->name, NULL,
  149. pix_fmts, lavfi->graph)) < 0)
  150. FAIL(ret);
  151. lavfi->sinks[i] = sink;
  152. if ((ret = avfilter_link(inout->filter_ctx, inout->pad_idx, sink, 0)) < 0)
  153. FAIL(ret);
  154. }
  155. /* configure the graph */
  156. if ((ret = avfilter_graph_config(lavfi->graph, avctx)) < 0)
  157. FAIL(ret);
  158. /* fill each stream with the information in the corresponding sink */
  159. for (i = 0; i < avctx->nb_streams; i++) {
  160. AVFilterLink *link = lavfi->sinks[lavfi->stream_sink_map[i]]->inputs[0];
  161. AVStream *st = avctx->streams[i];
  162. st->codec->codec_type = link->type;
  163. av_set_pts_info(st, 64, link->time_base.num, link->time_base.den);
  164. if (link->type == AVMEDIA_TYPE_VIDEO) {
  165. st->codec->codec_id = CODEC_ID_RAWVIDEO;
  166. st->codec->pix_fmt = link->format;
  167. st->codec->time_base = link->time_base;
  168. st->codec->width = link->w;
  169. st->codec->height = link->h;
  170. }
  171. }
  172. end:
  173. avfilter_inout_free(&input_links);
  174. avfilter_inout_free(&output_links);
  175. if (ret < 0)
  176. lavfi_read_close(avctx);
  177. return ret;
  178. }
  179. static int lavfi_read_packet(AVFormatContext *avctx, AVPacket *pkt)
  180. {
  181. LavfiContext *lavfi = avctx->priv_data;
  182. double min_pts = DBL_MAX;
  183. int min_pts_sink_idx;
  184. AVFilterBufferRef *picref;
  185. AVPicture pict;
  186. int ret, i, size;
  187. /* iterate through all the graph sinks. Select the sink with the
  188. * minimum PTS */
  189. for (i = 0; i < avctx->nb_streams; i++) {
  190. AVRational tb = lavfi->sinks[i]->inputs[0]->time_base;
  191. double d;
  192. int ret = av_vsink_buffer_get_video_buffer_ref(lavfi->sinks[i],
  193. &picref, AV_VSINK_BUF_FLAG_PEEK);
  194. if (ret < 0)
  195. return ret;
  196. d = av_rescale_q(picref->pts, tb, AV_TIME_BASE_Q);
  197. if (d < min_pts) {
  198. min_pts = d;
  199. min_pts_sink_idx = i;
  200. }
  201. }
  202. av_vsink_buffer_get_video_buffer_ref(lavfi->sinks[min_pts_sink_idx],
  203. &picref, 0);
  204. size = avpicture_get_size(picref->format, picref->video->w, picref->video->h);
  205. if ((ret = av_new_packet(pkt, size)) < 0)
  206. return ret;
  207. memcpy(pict.data, picref->data, 4*sizeof(picref->data[0]));
  208. memcpy(pict.linesize, picref->linesize, 4*sizeof(picref->linesize[0]));
  209. avpicture_layout(&pict, picref->format, picref->video->w,
  210. picref->video->h, pkt->data, size);
  211. pkt->stream_index = lavfi->sink_stream_map[min_pts_sink_idx];
  212. pkt->pts = picref->pts;
  213. pkt->size = size;
  214. avfilter_unref_buffer(picref);
  215. return size;
  216. }
  217. #define OFFSET(x) offsetof(LavfiContext, x)
  218. #define DEC AV_OPT_FLAG_DECODING_PARAM
  219. static const AVOption options[] = {
  220. { "graph", "Libavfilter graph", OFFSET(graph_str), FF_OPT_TYPE_STRING, {.str = NULL }, 0, 0, DEC },
  221. { NULL },
  222. };
  223. static const AVClass lavfi_class = {
  224. .class_name = "lavfi indev",
  225. .item_name = av_default_item_name,
  226. .option = options,
  227. .version = LIBAVUTIL_VERSION_INT,
  228. };
  229. AVInputFormat ff_lavfi_demuxer = {
  230. .name = "lavfi",
  231. .long_name = NULL_IF_CONFIG_SMALL("Libavfilter virtual input device"),
  232. .priv_data_size = sizeof(LavfiContext),
  233. .read_header = lavfi_read_header,
  234. .read_packet = lavfi_read_packet,
  235. .read_close = lavfi_read_close,
  236. .flags = AVFMT_NOFILE,
  237. .priv_class = &lavfi_class,
  238. };