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.

299 lines
9.7KB

  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;
  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. AVBufferSinkParams *buffersink_params = av_buffersink_params_alloc();
  148. #if FF_API_OLD_VSINK_API
  149. ret = avfilter_graph_create_filter(&sink, buffersink,
  150. inout->name, NULL,
  151. pix_fmts, lavfi->graph);
  152. #else
  153. buffersink_params->pixel_fmts = pix_fmts;
  154. ret = avfilter_graph_create_filter(&sink, buffersink,
  155. inout->name, NULL,
  156. buffersink_params, lavfi->graph);
  157. #endif
  158. av_freep(&buffersink_params);
  159. if (ret < 0)
  160. goto end;
  161. lavfi->sinks[i] = sink;
  162. if ((ret = avfilter_link(inout->filter_ctx, inout->pad_idx, sink, 0)) < 0)
  163. FAIL(ret);
  164. }
  165. /* configure the graph */
  166. if ((ret = avfilter_graph_config(lavfi->graph, avctx)) < 0)
  167. FAIL(ret);
  168. /* fill each stream with the information in the corresponding sink */
  169. for (i = 0; i < avctx->nb_streams; i++) {
  170. AVFilterLink *link = lavfi->sinks[lavfi->stream_sink_map[i]]->inputs[0];
  171. AVStream *st = avctx->streams[i];
  172. st->codec->codec_type = link->type;
  173. av_set_pts_info(st, 64, link->time_base.num, link->time_base.den);
  174. if (link->type == AVMEDIA_TYPE_VIDEO) {
  175. st->codec->codec_id = CODEC_ID_RAWVIDEO;
  176. st->codec->pix_fmt = link->format;
  177. st->codec->time_base = link->time_base;
  178. st->codec->width = link->w;
  179. st->codec->height = link->h;
  180. st ->sample_aspect_ratio =
  181. st->codec->sample_aspect_ratio = link->sample_aspect_ratio;
  182. }
  183. }
  184. end:
  185. avfilter_inout_free(&input_links);
  186. avfilter_inout_free(&output_links);
  187. if (ret < 0)
  188. lavfi_read_close(avctx);
  189. return ret;
  190. }
  191. static int lavfi_read_packet(AVFormatContext *avctx, AVPacket *pkt)
  192. {
  193. LavfiContext *lavfi = avctx->priv_data;
  194. double min_pts = DBL_MAX;
  195. int min_pts_sink_idx = 0;
  196. AVFilterBufferRef *picref;
  197. AVPicture pict;
  198. int ret, i, size;
  199. /* iterate through all the graph sinks. Select the sink with the
  200. * minimum PTS */
  201. for (i = 0; i < avctx->nb_streams; i++) {
  202. AVRational tb = lavfi->sinks[i]->inputs[0]->time_base;
  203. double d;
  204. int ret = av_buffersink_get_buffer_ref(lavfi->sinks[i],
  205. &picref, AV_BUFFERSINK_FLAG_PEEK);
  206. if (ret < 0)
  207. return ret;
  208. d = av_rescale_q(picref->pts, tb, AV_TIME_BASE_Q);
  209. av_dlog(avctx, "sink_idx:%d time:%f\n", i, d);
  210. if (d < min_pts) {
  211. min_pts = d;
  212. min_pts_sink_idx = i;
  213. }
  214. }
  215. av_dlog(avctx, "min_pts_sink_idx:%i\n", min_pts_sink_idx);
  216. av_buffersink_get_buffer_ref(lavfi->sinks[min_pts_sink_idx], &picref, 0);
  217. size = avpicture_get_size(picref->format, picref->video->w, picref->video->h);
  218. if ((ret = av_new_packet(pkt, size)) < 0)
  219. return ret;
  220. memcpy(pict.data, picref->data, 4*sizeof(picref->data[0]));
  221. memcpy(pict.linesize, picref->linesize, 4*sizeof(picref->linesize[0]));
  222. avpicture_layout(&pict, picref->format, picref->video->w,
  223. picref->video->h, pkt->data, size);
  224. pkt->stream_index = lavfi->sink_stream_map[min_pts_sink_idx];
  225. pkt->pts = picref->pts;
  226. pkt->pos = picref->pos;
  227. pkt->size = size;
  228. avfilter_unref_buffer(picref);
  229. return size;
  230. }
  231. #define OFFSET(x) offsetof(LavfiContext, x)
  232. #define DEC AV_OPT_FLAG_DECODING_PARAM
  233. static const AVOption options[] = {
  234. { "graph", "Libavfilter graph", OFFSET(graph_str), FF_OPT_TYPE_STRING, {.str = NULL }, 0, 0, DEC },
  235. { NULL },
  236. };
  237. static const AVClass lavfi_class = {
  238. .class_name = "lavfi indev",
  239. .item_name = av_default_item_name,
  240. .option = options,
  241. .version = LIBAVUTIL_VERSION_INT,
  242. };
  243. AVInputFormat ff_lavfi_demuxer = {
  244. .name = "lavfi",
  245. .long_name = NULL_IF_CONFIG_SMALL("Libavfilter virtual input device"),
  246. .priv_data_size = sizeof(LavfiContext),
  247. .read_header = lavfi_read_header,
  248. .read_packet = lavfi_read_packet,
  249. .read_close = lavfi_read_close,
  250. .flags = AVFMT_NOFILE,
  251. .priv_class = &lavfi_class,
  252. };