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.

286 lines
9.3KB

  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. st ->sample_aspect_ratio =
  171. st->codec->sample_aspect_ratio = link->sample_aspect_ratio;
  172. }
  173. }
  174. end:
  175. avfilter_inout_free(&input_links);
  176. avfilter_inout_free(&output_links);
  177. if (ret < 0)
  178. lavfi_read_close(avctx);
  179. return ret;
  180. }
  181. static int lavfi_read_packet(AVFormatContext *avctx, AVPacket *pkt)
  182. {
  183. LavfiContext *lavfi = avctx->priv_data;
  184. double min_pts = DBL_MAX;
  185. int min_pts_sink_idx = 0;
  186. AVFilterBufferRef *picref;
  187. AVPicture pict;
  188. int ret, i, size;
  189. /* iterate through all the graph sinks. Select the sink with the
  190. * minimum PTS */
  191. for (i = 0; i < avctx->nb_streams; i++) {
  192. AVRational tb = lavfi->sinks[i]->inputs[0]->time_base;
  193. double d;
  194. int ret = av_vsink_buffer_get_video_buffer_ref(lavfi->sinks[i],
  195. &picref, AV_VSINK_BUF_FLAG_PEEK);
  196. if (ret < 0)
  197. return ret;
  198. d = av_rescale_q(picref->pts, tb, AV_TIME_BASE_Q);
  199. if (d < min_pts) {
  200. min_pts = d;
  201. min_pts_sink_idx = i;
  202. }
  203. }
  204. av_vsink_buffer_get_video_buffer_ref(lavfi->sinks[min_pts_sink_idx],
  205. &picref, 0);
  206. size = avpicture_get_size(picref->format, picref->video->w, picref->video->h);
  207. if ((ret = av_new_packet(pkt, size)) < 0)
  208. return ret;
  209. memcpy(pict.data, picref->data, 4*sizeof(picref->data[0]));
  210. memcpy(pict.linesize, picref->linesize, 4*sizeof(picref->linesize[0]));
  211. avpicture_layout(&pict, picref->format, picref->video->w,
  212. picref->video->h, pkt->data, size);
  213. pkt->stream_index = lavfi->sink_stream_map[min_pts_sink_idx];
  214. pkt->pts = picref->pts;
  215. pkt->pos = picref->pos;
  216. pkt->size = size;
  217. avfilter_unref_buffer(picref);
  218. return size;
  219. }
  220. #define OFFSET(x) offsetof(LavfiContext, x)
  221. #define DEC AV_OPT_FLAG_DECODING_PARAM
  222. static const AVOption options[] = {
  223. { "graph", "Libavfilter graph", OFFSET(graph_str), FF_OPT_TYPE_STRING, {.str = NULL }, 0, 0, DEC },
  224. { NULL },
  225. };
  226. static const AVClass lavfi_class = {
  227. .class_name = "lavfi indev",
  228. .item_name = av_default_item_name,
  229. .option = options,
  230. .version = LIBAVUTIL_VERSION_INT,
  231. };
  232. AVInputFormat ff_lavfi_demuxer = {
  233. .name = "lavfi",
  234. .long_name = NULL_IF_CONFIG_SMALL("Libavfilter virtual input device"),
  235. .priv_data_size = sizeof(LavfiContext),
  236. .read_header = lavfi_read_header,
  237. .read_packet = lavfi_read_packet,
  238. .read_close = lavfi_read_close,
  239. .flags = AVFMT_NOFILE,
  240. .priv_class = &lavfi_class,
  241. };