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/bprint.h"
31 #include "libavutil/channel_layout.h"
32 #include "libavutil/file.h"
33 #include "libavutil/log.h"
34 #include "libavutil/mem.h"
35 #include "libavutil/opt.h"
36 #include "libavutil/parseutils.h"
37 #include "libavutil/pixdesc.h"
38 #include "libavfilter/avfilter.h"
39 #include "libavfilter/avfiltergraph.h"
40 #include "libavfilter/buffersink.h"
41 #include "libavformat/internal.h"
45 AVClass
*class; ///< class for private options
50 AVFilterContext
**sinks
;
54 AVFrame
*decoded_frame
;
57 static int *create_all_formats(int n
)
59 int i
, j
, *fmts
, count
= 0;
61 for (i
= 0; i
< n
; i
++) {
62 const AVPixFmtDescriptor
*desc
= av_pix_fmt_desc_get(i
);
63 if (!(desc
->flags
& AV_PIX_FMT_FLAG_HWACCEL
))
67 if (!(fmts
= av_malloc((count
+1) * sizeof(int))))
69 for (j
= 0, i
= 0; i
< n
; i
++) {
70 const AVPixFmtDescriptor
*desc
= av_pix_fmt_desc_get(i
);
71 if (!(desc
->flags
& AV_PIX_FMT_FLAG_HWACCEL
))
78 av_cold
static int lavfi_read_close(AVFormatContext
*avctx
)
80 LavfiContext
*lavfi
= avctx
->priv_data
;
82 av_freep(&lavfi
->sink_stream_map
);
83 av_freep(&lavfi
->sink_eof
);
84 av_freep(&lavfi
->stream_sink_map
);
85 av_freep(&lavfi
->sinks
);
86 avfilter_graph_free(&lavfi
->graph
);
87 av_frame_free(&lavfi
->decoded_frame
);
92 av_cold
static int lavfi_read_header(AVFormatContext
*avctx
)
94 LavfiContext
*lavfi
= avctx
->priv_data
;
95 AVFilterInOut
*input_links
= NULL
, *output_links
= NULL
, *inout
;
96 AVFilter
*buffersink
, *abuffersink
;
97 int *pix_fmts
= create_all_formats(AV_PIX_FMT_NB
);
98 enum AVMediaType type
;
101 #define FAIL(ERR) { ret = ERR; goto end; }
104 FAIL(AVERROR(ENOMEM
));
106 avfilter_register_all();
108 buffersink
= avfilter_get_by_name("buffersink");
109 abuffersink
= avfilter_get_by_name("abuffersink");
111 if (lavfi
->graph_filename
&& lavfi
->graph_str
) {
112 av_log(avctx
, AV_LOG_ERROR
,
113 "Only one of the graph or graph_file options must be specified\n");
114 FAIL(AVERROR(EINVAL
));
117 if (lavfi
->graph_filename
) {
118 AVBPrint graph_file_pb
;
119 AVIOContext
*avio
= NULL
;
120 ret
= avio_open(&avio
, lavfi
->graph_filename
, AVIO_FLAG_READ
);
123 av_bprint_init(&graph_file_pb
, 0, AV_BPRINT_SIZE_UNLIMITED
);
124 ret
= avio_read_to_bprint(avio
, &graph_file_pb
, INT_MAX
);
126 av_bprint_chars(&graph_file_pb
, '\0', 1);
127 if (!ret
&& !av_bprint_is_complete(&graph_file_pb
))
128 ret
= AVERROR(ENOMEM
);
130 av_bprint_finalize(&graph_file_pb
, NULL
);
133 if ((ret
= av_bprint_finalize(&graph_file_pb
, &lavfi
->graph_str
)))
137 if (!lavfi
->graph_str
)
138 lavfi
->graph_str
= av_strdup(avctx
->filename
);
140 /* parse the graph, create a stream for each open output */
141 if (!(lavfi
->graph
= avfilter_graph_alloc()))
142 FAIL(AVERROR(ENOMEM
));
144 if ((ret
= avfilter_graph_parse_ptr(lavfi
->graph
, lavfi
->graph_str
,
145 &input_links
, &output_links
, avctx
)) < 0)
149 av_log(avctx
, AV_LOG_ERROR
,
150 "Open inputs in the filtergraph are not acceptable\n");
151 FAIL(AVERROR(EINVAL
));
154 /* count the outputs */
155 for (n
= 0, inout
= output_links
; inout
; n
++, inout
= inout
->next
);
157 if (!(lavfi
->sink_stream_map
= av_malloc(sizeof(int) * n
)))
158 FAIL(AVERROR(ENOMEM
));
159 if (!(lavfi
->sink_eof
= av_mallocz(sizeof(int) * n
)))
160 FAIL(AVERROR(ENOMEM
));
161 if (!(lavfi
->stream_sink_map
= av_malloc(sizeof(int) * n
)))
162 FAIL(AVERROR(ENOMEM
));
164 for (i
= 0; i
< n
; i
++)
165 lavfi
->stream_sink_map
[i
] = -1;
167 /* parse the output link names - they need to be of the form out0, out1, ...
168 * create a mapping between them and the streams */
169 for (i
= 0, inout
= output_links
; inout
; i
++, inout
= inout
->next
) {
171 if (!strcmp(inout
->name
, "out"))
173 else if (sscanf(inout
->name
, "out%d\n", &stream_idx
) != 1) {
174 av_log(avctx
, AV_LOG_ERROR
,
175 "Invalid outpad name '%s'\n", inout
->name
);
176 FAIL(AVERROR(EINVAL
));
179 if ((unsigned)stream_idx
>= n
) {
180 av_log(avctx
, AV_LOG_ERROR
,
181 "Invalid index was specified in output '%s', "
182 "must be a non-negative value < %d\n",
184 FAIL(AVERROR(EINVAL
));
187 if (lavfi
->stream_sink_map
[stream_idx
] != -1) {
188 av_log(avctx
, AV_LOG_ERROR
,
189 "An output with stream index %d was already specified\n",
191 FAIL(AVERROR(EINVAL
));
193 lavfi
->sink_stream_map
[i
] = stream_idx
;
194 lavfi
->stream_sink_map
[stream_idx
] = i
;
197 /* for each open output create a corresponding stream */
198 for (i
= 0, inout
= output_links
; inout
; i
++, inout
= inout
->next
) {
200 if (!(st
= avformat_new_stream(avctx
, NULL
)))
201 FAIL(AVERROR(ENOMEM
));
205 /* create a sink for each output and connect them to the graph */
206 lavfi
->sinks
= av_malloc_array(avctx
->nb_streams
, sizeof(AVFilterContext
*));
208 FAIL(AVERROR(ENOMEM
));
210 for (i
= 0, inout
= output_links
; inout
; i
++, inout
= inout
->next
) {
211 AVFilterContext
*sink
;
213 type
= inout
->filter_ctx
->output_pads
[inout
->pad_idx
].type
;
215 if (type
== AVMEDIA_TYPE_VIDEO
&& ! buffersink
||
216 type
== AVMEDIA_TYPE_AUDIO
&& ! abuffersink
) {
217 av_log(avctx
, AV_LOG_ERROR
, "Missing required buffersink filter, aborting.\n");
218 FAIL(AVERROR_FILTER_NOT_FOUND
);
221 if (type
== AVMEDIA_TYPE_VIDEO
) {
222 ret
= avfilter_graph_create_filter(&sink
, buffersink
,
226 ret
= av_opt_set_int_list(sink
, "pix_fmts", pix_fmts
, AV_PIX_FMT_NONE
, AV_OPT_SEARCH_CHILDREN
);
229 } else if (type
== AVMEDIA_TYPE_AUDIO
) {
230 enum AVSampleFormat sample_fmts
[] = { AV_SAMPLE_FMT_U8
,
234 AV_SAMPLE_FMT_DBL
, -1 };
236 ret
= avfilter_graph_create_filter(&sink
, abuffersink
,
240 ret
= av_opt_set_int_list(sink
, "sample_fmts", sample_fmts
, AV_SAMPLE_FMT_NONE
, AV_OPT_SEARCH_CHILDREN
);
243 ret
= av_opt_set_int(sink
, "all_channel_counts", 1,
244 AV_OPT_SEARCH_CHILDREN
);
248 av_log(avctx
, AV_LOG_ERROR
,
249 "Output '%s' is not a video or audio output, not yet supported\n", inout
->name
);
250 FAIL(AVERROR(EINVAL
));
253 lavfi
->sinks
[i
] = sink
;
254 if ((ret
= avfilter_link(inout
->filter_ctx
, inout
->pad_idx
, sink
, 0)) < 0)
258 /* configure the graph */
259 if ((ret
= avfilter_graph_config(lavfi
->graph
, avctx
)) < 0)
262 if (lavfi
->dump_graph
) {
263 char *dump
= avfilter_graph_dump(lavfi
->graph
, lavfi
->dump_graph
);
269 /* fill each stream with the information in the corresponding sink */
270 for (i
= 0; i
< avctx
->nb_streams
; i
++) {
271 AVFilterLink
*link
= lavfi
->sinks
[lavfi
->stream_sink_map
[i
]]->inputs
[0];
272 AVStream
*st
= avctx
->streams
[i
];
273 st
->codec
->codec_type
= link
->type
;
274 avpriv_set_pts_info(st
, 64, link
->time_base
.num
, link
->time_base
.den
);
275 if (link
->type
== AVMEDIA_TYPE_VIDEO
) {
276 st
->codec
->codec_id
= AV_CODEC_ID_RAWVIDEO
;
277 st
->codec
->pix_fmt
= link
->format
;
278 st
->codec
->time_base
= link
->time_base
;
279 st
->codec
->width
= link
->w
;
280 st
->codec
->height
= link
->h
;
281 st
->sample_aspect_ratio
=
282 st
->codec
->sample_aspect_ratio
= link
->sample_aspect_ratio
;
283 avctx
->probesize
= FFMAX(avctx
->probesize
,
285 av_get_padded_bits_per_pixel(av_pix_fmt_desc_get(link
->format
)) *
287 } else if (link
->type
== AVMEDIA_TYPE_AUDIO
) {
288 st
->codec
->codec_id
= av_get_pcm_codec(link
->format
, -1);
289 st
->codec
->channels
= avfilter_link_get_channels(link
);
290 st
->codec
->sample_fmt
= link
->format
;
291 st
->codec
->sample_rate
= link
->sample_rate
;
292 st
->codec
->time_base
= link
->time_base
;
293 st
->codec
->channel_layout
= link
->channel_layout
;
294 if (st
->codec
->codec_id
== AV_CODEC_ID_NONE
)
295 av_log(avctx
, AV_LOG_ERROR
,
296 "Could not find PCM codec for sample format %s.\n",
297 av_get_sample_fmt_name(link
->format
));
301 if (!(lavfi
->decoded_frame
= av_frame_alloc()))
302 FAIL(AVERROR(ENOMEM
));
306 avfilter_inout_free(&input_links
);
307 avfilter_inout_free(&output_links
);
309 lavfi_read_close(avctx
);
313 static int lavfi_read_packet(AVFormatContext
*avctx
, AVPacket
*pkt
)
315 LavfiContext
*lavfi
= avctx
->priv_data
;
316 double min_pts
= DBL_MAX
;
317 int stream_idx
, min_pts_sink_idx
= 0;
318 AVFrame
*frame
= lavfi
->decoded_frame
;
320 AVDictionary
*frame_metadata
;
324 /* iterate through all the graph sinks. Select the sink with the
326 for (i
= 0; i
< avctx
->nb_streams
; i
++) {
327 AVRational tb
= lavfi
->sinks
[i
]->inputs
[0]->time_base
;
331 if (lavfi
->sink_eof
[i
])
334 ret
= av_buffersink_get_frame_flags(lavfi
->sinks
[i
], frame
,
335 AV_BUFFERSINK_FLAG_PEEK
);
336 if (ret
== AVERROR_EOF
) {
337 av_dlog(avctx
, "EOF sink_idx:%d\n", i
);
338 lavfi
->sink_eof
[i
] = 1;
342 d
= av_rescale_q(frame
->pts
, tb
, AV_TIME_BASE_Q
);
343 av_dlog(avctx
, "sink_idx:%d time:%f\n", i
, d
);
344 av_frame_unref(frame
);
348 min_pts_sink_idx
= i
;
351 if (min_pts
== DBL_MAX
)
354 av_dlog(avctx
, "min_pts_sink_idx:%i\n", min_pts_sink_idx
);
356 av_buffersink_get_frame_flags(lavfi
->sinks
[min_pts_sink_idx
], frame
, 0);
357 stream_idx
= lavfi
->sink_stream_map
[min_pts_sink_idx
];
359 if (frame
->width
/* FIXME best way of testing a video */) {
360 size
= avpicture_get_size(frame
->format
, frame
->width
, frame
->height
);
361 if ((ret
= av_new_packet(pkt
, size
)) < 0)
364 memcpy(pict
.data
, frame
->data
, 4*sizeof(frame
->data
[0]));
365 memcpy(pict
.linesize
, frame
->linesize
, 4*sizeof(frame
->linesize
[0]));
367 avpicture_layout(&pict
, frame
->format
, frame
->width
, frame
->height
,
369 } else if (av_frame_get_channels(frame
) /* FIXME test audio */) {
370 size
= frame
->nb_samples
* av_get_bytes_per_sample(frame
->format
) *
371 av_frame_get_channels(frame
);
372 if ((ret
= av_new_packet(pkt
, size
)) < 0)
374 memcpy(pkt
->data
, frame
->data
[0], size
);
377 frame_metadata
= av_frame_get_metadata(frame
);
378 if (frame_metadata
) {
380 AVDictionaryEntry
*e
= NULL
;
383 av_bprint_init(&meta_buf
, 0, AV_BPRINT_SIZE_UNLIMITED
);
384 while ((e
= av_dict_get(frame_metadata
, "", e
, AV_DICT_IGNORE_SUFFIX
))) {
385 av_bprintf(&meta_buf
, "%s", e
->key
);
386 av_bprint_chars(&meta_buf
, '\0', 1);
387 av_bprintf(&meta_buf
, "%s", e
->value
);
388 av_bprint_chars(&meta_buf
, '\0', 1);
390 if (!av_bprint_is_complete(&meta_buf
) ||
391 !(metadata
= av_packet_new_side_data(pkt
, AV_PKT_DATA_STRINGS_METADATA
,
393 av_bprint_finalize(&meta_buf
, NULL
);
394 return AVERROR(ENOMEM
);
396 memcpy(metadata
, meta_buf
.str
, meta_buf
.len
);
397 av_bprint_finalize(&meta_buf
, NULL
);
400 pkt
->stream_index
= stream_idx
;
401 pkt
->pts
= frame
->pts
;
402 pkt
->pos
= av_frame_get_pkt_pos(frame
);
404 av_frame_unref(frame
);
408 #define OFFSET(x) offsetof(LavfiContext, x)
410 #define DEC AV_OPT_FLAG_DECODING_PARAM
412 static const AVOption options
[] = {
413 { "graph", "set libavfilter graph", OFFSET(graph_str
), AV_OPT_TYPE_STRING
, {.str
= NULL
}, 0, 0, DEC
},
414 { "graph_file","set libavfilter graph filename", OFFSET(graph_filename
), AV_OPT_TYPE_STRING
, {.str
= NULL
}, 0, 0, DEC
},
415 { "dumpgraph", "dump graph to stderr", OFFSET(dump_graph
), AV_OPT_TYPE_STRING
, {.str
= NULL
}, 0, 0, DEC
},
419 static const AVClass lavfi_class
= {
420 .class_name
= "lavfi indev",
421 .item_name
= av_default_item_name
,
423 .version
= LIBAVUTIL_VERSION_INT
,
424 .category
= AV_CLASS_CATEGORY_DEVICE_INPUT
,
427 AVInputFormat ff_lavfi_demuxer
= {
429 .long_name
= NULL_IF_CONFIG_SMALL("Libavfilter virtual input device"),
430 .priv_data_size
= sizeof(LavfiContext
),
431 .read_header
= lavfi_read_header
,
432 .read_packet
= lavfi_read_packet
,
433 .read_close
= lavfi_read_close
,
434 .flags
= AVFMT_NOFILE
,
435 .priv_class
= &lavfi_class
,