]> git.sesse.net Git - ffmpeg/blob - libavdevice/lavfi.c
Merge commit 'd7d6efe42b0d2057e67999b96b9a391f533d2333'
[ffmpeg] / libavdevice / lavfi.c
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 /**
22  * @file
23  * libavfilter virtual input device
24  */
25
26 /* #define DEBUG */
27
28 #include <float.h>              /* DBL_MIN, DBL_MAX */
29
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"
42 #include "avdevice.h"
43
44 typedef struct {
45     AVClass *class;          ///< class for private options
46     char          *graph_str;
47     char          *graph_filename;
48     char          *dump_graph;
49     AVFilterGraph *graph;
50     AVFilterContext **sinks;
51     int *sink_stream_map;
52     int *sink_eof;
53     int *stream_sink_map;
54 } LavfiContext;
55
56 static int *create_all_formats(int n)
57 {
58     int i, j, *fmts, count = 0;
59
60     for (i = 0; i < n; i++) {
61         const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(i);
62         if (!(desc->flags & PIX_FMT_HWACCEL))
63             count++;
64     }
65
66     if (!(fmts = av_malloc((count+1) * sizeof(int))))
67         return NULL;
68     for (j = 0, i = 0; i < n; i++) {
69         const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(i);
70         if (!(desc->flags & PIX_FMT_HWACCEL))
71             fmts[j++] = i;
72     }
73     fmts[j] = -1;
74     return fmts;
75 }
76
77 av_cold static int lavfi_read_close(AVFormatContext *avctx)
78 {
79     LavfiContext *lavfi = avctx->priv_data;
80
81     av_freep(&lavfi->sink_stream_map);
82     av_freep(&lavfi->sink_eof);
83     av_freep(&lavfi->stream_sink_map);
84     av_freep(&lavfi->sinks);
85     avfilter_graph_free(&lavfi->graph);
86
87     return 0;
88 }
89
90 av_cold static int lavfi_read_header(AVFormatContext *avctx)
91 {
92     LavfiContext *lavfi = avctx->priv_data;
93     AVFilterInOut *input_links = NULL, *output_links = NULL, *inout;
94     AVFilter *buffersink, *abuffersink;
95     int *pix_fmts = create_all_formats(AV_PIX_FMT_NB);
96     enum AVMediaType type;
97     int ret = 0, i, n;
98
99 #define FAIL(ERR) { ret = ERR; goto end; }
100
101     if (!pix_fmts)
102         FAIL(AVERROR(ENOMEM));
103
104     avfilter_register_all();
105
106     buffersink = avfilter_get_by_name("ffbuffersink");
107     abuffersink = avfilter_get_by_name("ffabuffersink");
108
109     if (lavfi->graph_filename && lavfi->graph_str) {
110         av_log(avctx, AV_LOG_ERROR,
111                "Only one of the graph or graph_file options must be specified\n");
112         FAIL(AVERROR(EINVAL));
113     }
114
115     if (lavfi->graph_filename) {
116         uint8_t *file_buf, *graph_buf;
117         size_t file_bufsize;
118         ret = av_file_map(lavfi->graph_filename,
119                           &file_buf, &file_bufsize, 0, avctx);
120         if (ret < 0)
121             goto end;
122
123         /* create a 0-terminated string based on the read file */
124         graph_buf = av_malloc(file_bufsize + 1);
125         if (!graph_buf) {
126             av_file_unmap(file_buf, file_bufsize);
127             FAIL(AVERROR(ENOMEM));
128         }
129         memcpy(graph_buf, file_buf, file_bufsize);
130         graph_buf[file_bufsize] = 0;
131         av_file_unmap(file_buf, file_bufsize);
132         lavfi->graph_str = graph_buf;
133     }
134
135     if (!lavfi->graph_str)
136         lavfi->graph_str = av_strdup(avctx->filename);
137
138     /* parse the graph, create a stream for each open output */
139     if (!(lavfi->graph = avfilter_graph_alloc()))
140         FAIL(AVERROR(ENOMEM));
141
142     if ((ret = avfilter_graph_parse(lavfi->graph, lavfi->graph_str,
143                                     &input_links, &output_links, avctx)) < 0)
144         FAIL(ret);
145
146     if (input_links) {
147         av_log(avctx, AV_LOG_ERROR,
148                "Open inputs in the filtergraph are not acceptable\n");
149         FAIL(AVERROR(EINVAL));
150     }
151
152     /* count the outputs */
153     for (n = 0, inout = output_links; inout; n++, inout = inout->next);
154
155     if (!(lavfi->sink_stream_map = av_malloc(sizeof(int) * n)))
156         FAIL(AVERROR(ENOMEM));
157     if (!(lavfi->sink_eof = av_mallocz(sizeof(int) * n)))
158         FAIL(AVERROR(ENOMEM));
159     if (!(lavfi->stream_sink_map = av_malloc(sizeof(int) * n)))
160         FAIL(AVERROR(ENOMEM));
161
162     for (i = 0; i < n; i++)
163         lavfi->stream_sink_map[i] = -1;
164
165     /* parse the output link names - they need to be of the form out0, out1, ...
166      * create a mapping between them and the streams */
167     for (i = 0, inout = output_links; inout; i++, inout = inout->next) {
168         int stream_idx;
169         if (!strcmp(inout->name, "out"))
170             stream_idx = 0;
171         else if (sscanf(inout->name, "out%d\n", &stream_idx) != 1) {
172             av_log(avctx,  AV_LOG_ERROR,
173                    "Invalid outpad name '%s'\n", inout->name);
174             FAIL(AVERROR(EINVAL));
175         }
176
177         if ((unsigned)stream_idx >= n) {
178             av_log(avctx, AV_LOG_ERROR,
179                    "Invalid index was specified in output '%s', "
180                    "must be a non-negative value < %d\n",
181                    inout->name, n);
182             FAIL(AVERROR(EINVAL));
183         }
184
185         /* is an audio or video output? */
186         type = inout->filter_ctx->output_pads[inout->pad_idx].type;
187         if (type != AVMEDIA_TYPE_VIDEO && type != AVMEDIA_TYPE_AUDIO) {
188             av_log(avctx,  AV_LOG_ERROR,
189                    "Output '%s' is not a video or audio output, not yet supported\n", inout->name);
190             FAIL(AVERROR(EINVAL));
191         }
192
193         if (lavfi->stream_sink_map[stream_idx] != -1) {
194             av_log(avctx,  AV_LOG_ERROR,
195                    "An output with stream index %d was already specified\n",
196                    stream_idx);
197             FAIL(AVERROR(EINVAL));
198         }
199         lavfi->sink_stream_map[i] = stream_idx;
200         lavfi->stream_sink_map[stream_idx] = i;
201     }
202
203     /* for each open output create a corresponding stream */
204     for (i = 0, inout = output_links; inout; i++, inout = inout->next) {
205         AVStream *st;
206         if (!(st = avformat_new_stream(avctx, NULL)))
207             FAIL(AVERROR(ENOMEM));
208         st->id = i;
209     }
210
211     /* create a sink for each output and connect them to the graph */
212     lavfi->sinks = av_malloc(sizeof(AVFilterContext *) * avctx->nb_streams);
213     if (!lavfi->sinks)
214         FAIL(AVERROR(ENOMEM));
215
216     for (i = 0, inout = output_links; inout; i++, inout = inout->next) {
217         AVFilterContext *sink;
218
219         type = inout->filter_ctx->output_pads[inout->pad_idx].type;
220
221         if (type == AVMEDIA_TYPE_VIDEO && ! buffersink ||
222             type == AVMEDIA_TYPE_AUDIO && ! abuffersink) {
223                 av_log(avctx, AV_LOG_ERROR, "Missing required buffersink filter, aborting.\n");
224                 FAIL(AVERROR_FILTER_NOT_FOUND);
225         }
226
227         if (type == AVMEDIA_TYPE_VIDEO) {
228             AVBufferSinkParams *buffersink_params = av_buffersink_params_alloc();
229
230             buffersink_params->pixel_fmts = pix_fmts;
231             ret = avfilter_graph_create_filter(&sink, buffersink,
232                                                inout->name, NULL,
233                                                buffersink_params, lavfi->graph);
234             av_freep(&buffersink_params);
235
236             if (ret < 0)
237                 goto end;
238         } else if (type == AVMEDIA_TYPE_AUDIO) {
239             enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_U8,
240                                                   AV_SAMPLE_FMT_S16,
241                                                   AV_SAMPLE_FMT_S32,
242                                                   AV_SAMPLE_FMT_FLT,
243                                                   AV_SAMPLE_FMT_DBL, -1 };
244             AVABufferSinkParams *abuffersink_params = av_abuffersink_params_alloc();
245             abuffersink_params->sample_fmts = sample_fmts;
246
247             ret = avfilter_graph_create_filter(&sink, abuffersink,
248                                                inout->name, NULL,
249                                                abuffersink_params, lavfi->graph);
250             av_free(abuffersink_params);
251             if (ret < 0)
252                 goto end;
253         }
254
255         lavfi->sinks[i] = sink;
256         if ((ret = avfilter_link(inout->filter_ctx, inout->pad_idx, sink, 0)) < 0)
257             FAIL(ret);
258     }
259
260     /* configure the graph */
261     if ((ret = avfilter_graph_config(lavfi->graph, avctx)) < 0)
262         FAIL(ret);
263
264     if (lavfi->dump_graph) {
265         char *dump = avfilter_graph_dump(lavfi->graph, lavfi->dump_graph);
266         fputs(dump, stderr);
267         fflush(stderr);
268         av_free(dump);
269     }
270
271     /* fill each stream with the information in the corresponding sink */
272     for (i = 0; i < avctx->nb_streams; i++) {
273         AVFilterLink *link = lavfi->sinks[lavfi->stream_sink_map[i]]->inputs[0];
274         AVStream *st = avctx->streams[i];
275         st->codec->codec_type = link->type;
276         avpriv_set_pts_info(st, 64, link->time_base.num, link->time_base.den);
277         if (link->type == AVMEDIA_TYPE_VIDEO) {
278             st->codec->codec_id   = AV_CODEC_ID_RAWVIDEO;
279             st->codec->pix_fmt    = link->format;
280             st->codec->time_base  = link->time_base;
281             st->codec->width      = link->w;
282             st->codec->height     = link->h;
283             st       ->sample_aspect_ratio =
284             st->codec->sample_aspect_ratio = link->sample_aspect_ratio;
285         } else if (link->type == AVMEDIA_TYPE_AUDIO) {
286             st->codec->codec_id    = av_get_pcm_codec(link->format, -1);
287             st->codec->channels    = av_get_channel_layout_nb_channels(link->channel_layout);
288             st->codec->sample_fmt  = link->format;
289             st->codec->sample_rate = link->sample_rate;
290             st->codec->time_base   = link->time_base;
291             st->codec->channel_layout = link->channel_layout;
292             if (st->codec->codec_id == AV_CODEC_ID_NONE)
293                 av_log(avctx, AV_LOG_ERROR,
294                        "Could not find PCM codec for sample format %s.\n",
295                        av_get_sample_fmt_name(link->format));
296         }
297     }
298
299 end:
300     av_free(pix_fmts);
301     avfilter_inout_free(&input_links);
302     avfilter_inout_free(&output_links);
303     if (ret < 0)
304         lavfi_read_close(avctx);
305     return ret;
306 }
307
308 static int lavfi_read_packet(AVFormatContext *avctx, AVPacket *pkt)
309 {
310     LavfiContext *lavfi = avctx->priv_data;
311     double min_pts = DBL_MAX;
312     int stream_idx, min_pts_sink_idx = 0;
313     AVFilterBufferRef *ref;
314     AVPicture pict;
315     int ret, i;
316     int size = 0;
317
318     /* iterate through all the graph sinks. Select the sink with the
319      * minimum PTS */
320     for (i = 0; i < avctx->nb_streams; i++) {
321         AVRational tb = lavfi->sinks[i]->inputs[0]->time_base;
322         double d;
323         int ret;
324
325         if (lavfi->sink_eof[i])
326             continue;
327
328         ret = av_buffersink_get_buffer_ref(lavfi->sinks[i],
329                                        &ref, AV_BUFFERSINK_FLAG_PEEK);
330         if (ret == AVERROR_EOF) {
331             av_dlog(avctx, "EOF sink_idx:%d\n", i);
332             lavfi->sink_eof[i] = 1;
333             continue;
334         } else if (ret < 0)
335             return ret;
336         d = av_rescale_q(ref->pts, tb, AV_TIME_BASE_Q);
337         av_dlog(avctx, "sink_idx:%d time:%f\n", i, d);
338
339         if (d < min_pts) {
340             min_pts = d;
341             min_pts_sink_idx = i;
342         }
343     }
344     if (min_pts == DBL_MAX)
345         return AVERROR_EOF;
346
347     av_dlog(avctx, "min_pts_sink_idx:%i\n", min_pts_sink_idx);
348
349     av_buffersink_get_buffer_ref(lavfi->sinks[min_pts_sink_idx], &ref, 0);
350     stream_idx = lavfi->sink_stream_map[min_pts_sink_idx];
351
352     if (ref->video) {
353         size = avpicture_get_size(ref->format, ref->video->w, ref->video->h);
354         if ((ret = av_new_packet(pkt, size)) < 0)
355             return ret;
356
357         memcpy(pict.data,     ref->data,     4*sizeof(ref->data[0]));
358         memcpy(pict.linesize, ref->linesize, 4*sizeof(ref->linesize[0]));
359
360         avpicture_layout(&pict, ref->format, ref->video->w,
361                          ref->video->h, pkt->data, size);
362     } else if (ref->audio) {
363         size = ref->audio->nb_samples *
364             av_get_bytes_per_sample(ref->format) *
365             av_get_channel_layout_nb_channels(ref->audio->channel_layout);
366         if ((ret = av_new_packet(pkt, size)) < 0)
367             return ret;
368         memcpy(pkt->data, ref->data[0], size);
369     }
370
371     if (ref->metadata) {
372         uint8_t *metadata;
373         AVDictionaryEntry *e = NULL;
374         AVBPrint meta_buf;
375
376         av_bprint_init(&meta_buf, 0, AV_BPRINT_SIZE_UNLIMITED);
377         while ((e = av_dict_get(ref->metadata, "", e, AV_DICT_IGNORE_SUFFIX))) {
378             av_bprintf(&meta_buf, "%s", e->key);
379             av_bprint_chars(&meta_buf, '\0', 1);
380             av_bprintf(&meta_buf, "%s", e->value);
381             av_bprint_chars(&meta_buf, '\0', 1);
382         }
383         if (!av_bprint_is_complete(&meta_buf) ||
384             !(metadata = av_packet_new_side_data(pkt, AV_PKT_DATA_STRINGS_METADATA,
385                                                  meta_buf.len))) {
386             av_bprint_finalize(&meta_buf, NULL);
387             return AVERROR(ENOMEM);
388         }
389         memcpy(metadata, meta_buf.str, meta_buf.len);
390         av_bprint_finalize(&meta_buf, NULL);
391     }
392
393     pkt->stream_index = stream_idx;
394     pkt->pts = ref->pts;
395     pkt->pos = ref->pos;
396     pkt->size = size;
397     avfilter_unref_buffer(ref);
398     return size;
399 }
400
401 #define OFFSET(x) offsetof(LavfiContext, x)
402
403 #define DEC AV_OPT_FLAG_DECODING_PARAM
404
405 static const AVOption options[] = {
406     { "graph",     "set libavfilter graph", OFFSET(graph_str),  AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
407     { "graph_file","set libavfilter graph filename", OFFSET(graph_filename), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC},
408     { "dumpgraph", "dump graph to stderr",  OFFSET(dump_graph), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
409     { NULL },
410 };
411
412 static const AVClass lavfi_class = {
413     .class_name = "lavfi indev",
414     .item_name  = av_default_item_name,
415     .option     = options,
416     .version    = LIBAVUTIL_VERSION_INT,
417 };
418
419 AVInputFormat ff_lavfi_demuxer = {
420     .name           = "lavfi",
421     .long_name      = NULL_IF_CONFIG_SMALL("Libavfilter virtual input device"),
422     .priv_data_size = sizeof(LavfiContext),
423     .read_header    = lavfi_read_header,
424     .read_packet    = lavfi_read_packet,
425     .read_close     = lavfi_read_close,
426     .flags          = AVFMT_NOFILE,
427     .priv_class     = &lavfi_class,
428 };