2 * Copyright (c) 2011 Stefano Sabatini
4 * This file is part of FFmpeg.
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.
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.
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
23 * libavfilter virtual input device
28 #include "float.h" /* DBL_MIN, DBL_MAX */
30 #include "libavutil/log.h"
31 #include "libavutil/mem.h"
32 #include "libavutil/opt.h"
33 #include "libavutil/parseutils.h"
34 #include "libavutil/pixdesc.h"
35 #include "libavfilter/avfilter.h"
36 #include "libavfilter/avfiltergraph.h"
37 #include "libavfilter/vsink_buffer.h"
41 AVClass *class; ///< class for private options
44 AVFilterContext **sinks;
49 static int *create_all_formats(int n)
51 int i, j, *fmts, count = 0;
53 for (i = 0; i < n; i++)
54 if (!(av_pix_fmt_descriptors[i].flags & PIX_FMT_HWACCEL))
57 if (!(fmts = av_malloc((count+1) * sizeof(int))))
59 for (j = 0, i = 0; i < n; i++) {
60 if (!(av_pix_fmt_descriptors[i].flags & PIX_FMT_HWACCEL))
67 av_cold static int lavfi_read_close(AVFormatContext *avctx)
69 LavfiContext *lavfi = avctx->priv_data;
71 av_freep(&lavfi->sink_stream_map);
72 av_freep(&lavfi->stream_sink_map);
73 avfilter_graph_free(&lavfi->graph);
78 av_cold static int lavfi_read_header(AVFormatContext *avctx,
79 AVFormatParameters *ap)
81 LavfiContext *lavfi = avctx->priv_data;
82 AVFilterInOut *input_links = NULL, *output_links = NULL, *inout;
84 int *pix_fmts = create_all_formats(PIX_FMT_NB);
87 #define FAIL(ERR) { ret = ERR; goto end; }
89 avfilter_register_all();
91 if (!(buffersink = avfilter_get_by_name("buffersink"))) {
92 av_log(avctx, AV_LOG_ERROR,
93 "Missing required buffersink filter, aborting.\n");
94 FAIL(AVERROR_FILTER_NOT_FOUND);
97 if (!lavfi->graph_str)
98 lavfi->graph_str = av_strdup(avctx->filename);
100 /* parse the graph, create a stream for each open output */
101 if (!(lavfi->graph = avfilter_graph_alloc()))
102 FAIL(AVERROR(ENOMEM));
104 if ((ret = avfilter_graph_parse(lavfi->graph, lavfi->graph_str,
105 &input_links, &output_links, avctx)) < 0)
109 av_log(avctx, AV_LOG_ERROR,
110 "Open inputs in the filtergraph are not acceptable\n");
111 FAIL(AVERROR(EINVAL));
114 /* count the outputs */
115 for (n = 0, inout = output_links; inout; n++, inout = inout->next);
117 if (!(lavfi->sink_stream_map = av_malloc(sizeof(int) * n)))
118 FAIL(AVERROR(ENOMEM));
119 if (!(lavfi->stream_sink_map = av_malloc(sizeof(int) * n)))
120 FAIL(AVERROR(ENOMEM));
122 for (i = 0; i < n; i++)
123 lavfi->stream_sink_map[i] = -1;
125 /* parse the output link names - they need to be of the form out0, out1, ...
126 * create a mapping between them and the streams */
127 for (i = 0, inout = output_links; inout; i++, inout = inout->next) {
129 if (!strcmp(inout->name, "out"))
131 else if (sscanf(inout->name, "out%d\n", &stream_idx) != 1) {
132 av_log(avctx, AV_LOG_ERROR,
133 "Invalid outpad name '%s'\n", inout->name);
134 FAIL(AVERROR(EINVAL));
137 if ((unsigned)stream_idx >= n) {
138 av_log(avctx, AV_LOG_ERROR,
139 "Invalid index was specified in output '%s', "
140 "must be a non-negative value < %d\n",
142 FAIL(AVERROR(EINVAL));
145 /* is a video output? */
146 if (inout->filter_ctx->output_pads[inout->pad_idx].type != AVMEDIA_TYPE_VIDEO) {
147 av_log(avctx, AV_LOG_ERROR,
148 "Output '%s' is not a video output, not yet supported", inout->name);
149 FAIL(AVERROR(EINVAL));
152 if (lavfi->stream_sink_map[stream_idx] != -1) {
153 av_log(avctx, AV_LOG_ERROR,
154 "An with stream index %d was already specified\n",
156 FAIL(AVERROR(EINVAL));
158 lavfi->sink_stream_map[i] = stream_idx;
159 lavfi->stream_sink_map[stream_idx] = i;
162 /* for each open output create a corresponding stream */
163 for (i = 0, inout = output_links; inout; i++, inout = inout->next) {
165 if (!(st = av_new_stream(avctx, i)))
166 FAIL(AVERROR(ENOMEM));
169 /* create a sink for each output and connect them to the graph */
170 lavfi->sinks = av_malloc(sizeof(AVFilterContext *) * avctx->nb_streams);
172 FAIL(AVERROR(ENOMEM));
174 for (i = 0, inout = output_links; inout; i++, inout = inout->next) {
175 AVFilterContext *sink;
176 if ((ret = avfilter_graph_create_filter(&sink, buffersink,
178 pix_fmts, lavfi->graph)) < 0)
180 lavfi->sinks[i] = sink;
181 if ((ret = avfilter_link(inout->filter_ctx, inout->pad_idx, sink, 0)) < 0)
185 /* configure the graph */
186 if ((ret = avfilter_graph_config(lavfi->graph, avctx)) < 0)
189 /* fill each stream with the information in the corresponding sink */
190 for (i = 0; i < avctx->nb_streams; i++) {
191 AVFilterLink *link = lavfi->sinks[lavfi->stream_sink_map[i]]->inputs[0];
192 AVStream *st = avctx->streams[i];
193 st->codec->codec_type = link->type;
194 av_set_pts_info(st, 64, link->time_base.num, link->time_base.den);
195 if (link->type == AVMEDIA_TYPE_VIDEO) {
196 st->codec->codec_id = CODEC_ID_RAWVIDEO;
197 st->codec->pix_fmt = link->format;
198 st->codec->time_base = link->time_base;
199 st->codec->width = link->w;
200 st->codec->height = link->h;
201 st ->sample_aspect_ratio =
202 st->codec->sample_aspect_ratio = link->sample_aspect_ratio;
207 avfilter_inout_free(&input_links);
208 avfilter_inout_free(&output_links);
210 lavfi_read_close(avctx);
214 static int lavfi_read_packet(AVFormatContext *avctx, AVPacket *pkt)
216 LavfiContext *lavfi = avctx->priv_data;
217 double min_pts = DBL_MAX;
218 int min_pts_sink_idx = 0;
219 AVFilterBufferRef *picref;
223 /* iterate through all the graph sinks. Select the sink with the
225 for (i = 0; i < avctx->nb_streams; i++) {
226 AVRational tb = lavfi->sinks[i]->inputs[0]->time_base;
228 int ret = av_vsink_buffer_get_video_buffer_ref(lavfi->sinks[i],
229 &picref, AV_VSINK_BUF_FLAG_PEEK);
232 d = av_rescale_q(picref->pts, tb, AV_TIME_BASE_Q);
235 min_pts_sink_idx = i;
239 av_vsink_buffer_get_video_buffer_ref(lavfi->sinks[min_pts_sink_idx],
242 size = avpicture_get_size(picref->format, picref->video->w, picref->video->h);
243 if ((ret = av_new_packet(pkt, size)) < 0)
246 memcpy(pict.data, picref->data, 4*sizeof(picref->data[0]));
247 memcpy(pict.linesize, picref->linesize, 4*sizeof(picref->linesize[0]));
249 avpicture_layout(&pict, picref->format, picref->video->w,
250 picref->video->h, pkt->data, size);
251 pkt->stream_index = lavfi->sink_stream_map[min_pts_sink_idx];
252 pkt->pts = picref->pts;
253 pkt->pos = picref->pos;
255 avfilter_unref_buffer(picref);
260 #define OFFSET(x) offsetof(LavfiContext, x)
262 #define DEC AV_OPT_FLAG_DECODING_PARAM
264 static const AVOption options[] = {
265 { "graph", "Libavfilter graph", OFFSET(graph_str), FF_OPT_TYPE_STRING, {.str = NULL }, 0, 0, DEC },
269 static const AVClass lavfi_class = {
270 .class_name = "lavfi indev",
271 .item_name = av_default_item_name,
273 .version = LIBAVUTIL_VERSION_INT,
276 AVInputFormat ff_lavfi_demuxer = {
278 .long_name = NULL_IF_CONFIG_SMALL("Libavfilter virtual input device"),
279 .priv_data_size = sizeof(LavfiContext),
280 .read_header = lavfi_read_header,
281 .read_packet = lavfi_read_packet,
282 .read_close = lavfi_read_close,
283 .flags = AVFMT_NOFILE,
284 .priv_class = &lavfi_class,