OSDN Git Service

lavfi: rename vsink_buffer.c to sink_buffer.c, and vsink_buffer.h to buffersink.h
[coroid/ffmpeg_saccubus.git] / 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/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/buffersink.h"
38 #include "avdevice.h"
39
40 typedef struct {
41     AVClass *class;          ///< class for private options
42     char          *graph_str;
43     AVFilterGraph *graph;
44     AVFilterContext **sinks;
45     int *sink_stream_map;
46     int *stream_sink_map;
47 } LavfiContext;
48
49 static int *create_all_formats(int n)
50 {
51     int i, j, *fmts, count = 0;
52
53     for (i = 0; i < n; i++)
54         if (!(av_pix_fmt_descriptors[i].flags & PIX_FMT_HWACCEL))
55             count++;
56
57     if (!(fmts = av_malloc((count+1) * sizeof(int))))
58         return NULL;
59     for (j = 0, i = 0; i < n; i++) {
60         if (!(av_pix_fmt_descriptors[i].flags & PIX_FMT_HWACCEL))
61             fmts[j++] = i;
62     }
63     fmts[j] = -1;
64     return fmts;
65 }
66
67 av_cold static int lavfi_read_close(AVFormatContext *avctx)
68 {
69     LavfiContext *lavfi = avctx->priv_data;
70
71     av_freep(&lavfi->sink_stream_map);
72     av_freep(&lavfi->stream_sink_map);
73     avfilter_graph_free(&lavfi->graph);
74
75     return 0;
76 }
77
78 av_cold static int lavfi_read_header(AVFormatContext *avctx,
79                                      AVFormatParameters *ap)
80 {
81     LavfiContext *lavfi = avctx->priv_data;
82     AVFilterInOut *input_links = NULL, *output_links = NULL, *inout;
83     AVFilter *buffersink;
84     int *pix_fmts = create_all_formats(PIX_FMT_NB);
85     int ret = 0, i, n;
86
87 #define FAIL(ERR) { ret = ERR; goto end; }
88
89     avfilter_register_all();
90
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);
95     }
96
97     if (!lavfi->graph_str)
98         lavfi->graph_str = av_strdup(avctx->filename);
99
100     /* parse the graph, create a stream for each open output */
101     if (!(lavfi->graph = avfilter_graph_alloc()))
102         FAIL(AVERROR(ENOMEM));
103
104     if ((ret = avfilter_graph_parse(lavfi->graph, lavfi->graph_str,
105                                     &input_links, &output_links, avctx)) < 0)
106         FAIL(ret);
107
108     if (input_links) {
109         av_log(avctx, AV_LOG_ERROR,
110                "Open inputs in the filtergraph are not acceptable\n");
111         FAIL(AVERROR(EINVAL));
112     }
113
114     /* count the outputs */
115     for (n = 0, inout = output_links; inout; n++, inout = inout->next);
116
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));
121
122     for (i = 0; i < n; i++)
123         lavfi->stream_sink_map[i] = -1;
124
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) {
128         int stream_idx;
129         if (!strcmp(inout->name, "out"))
130             stream_idx = 0;
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));
135         }
136
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",
141                    inout->name, n);
142             FAIL(AVERROR(EINVAL));
143         }
144
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));
150         }
151
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",
155                    stream_idx);
156             FAIL(AVERROR(EINVAL));
157         }
158         lavfi->sink_stream_map[i] = stream_idx;
159         lavfi->stream_sink_map[stream_idx] = i;
160     }
161
162     /* for each open output create a corresponding stream */
163     for (i = 0, inout = output_links; inout; i++, inout = inout->next) {
164         AVStream *st;
165         if (!(st = av_new_stream(avctx, i)))
166             FAIL(AVERROR(ENOMEM));
167     }
168
169     /* create a sink for each output and connect them to the graph */
170     lavfi->sinks = av_malloc(sizeof(AVFilterContext *) * avctx->nb_streams);
171     if (!lavfi->sinks)
172         FAIL(AVERROR(ENOMEM));
173
174     for (i = 0, inout = output_links; inout; i++, inout = inout->next) {
175         AVFilterContext *sink;
176         AVBufferSinkParams *buffersink_params = av_buffersink_params_alloc();
177 #if FF_API_OLD_VSINK_API
178         ret = avfilter_graph_create_filter(&sink, buffersink,
179                                            inout->name, NULL,
180                                            pix_fmts, lavfi->graph);
181 #else
182         buffersink_params->pixel_fmts = pix_fmts;
183         ret = avfilter_graph_create_filter(&sink, buffersink,
184                                            inout->name, NULL,
185                                            buffersink_params, lavfi->graph);
186 #endif
187         av_freep(&buffersink_params);
188         if (ret < 0)
189             goto end;
190
191         lavfi->sinks[i] = sink;
192         if ((ret = avfilter_link(inout->filter_ctx, inout->pad_idx, sink, 0)) < 0)
193             FAIL(ret);
194     }
195
196     /* configure the graph */
197     if ((ret = avfilter_graph_config(lavfi->graph, avctx)) < 0)
198         FAIL(ret);
199
200     /* fill each stream with the information in the corresponding sink */
201     for (i = 0; i < avctx->nb_streams; i++) {
202         AVFilterLink *link = lavfi->sinks[lavfi->stream_sink_map[i]]->inputs[0];
203         AVStream *st = avctx->streams[i];
204         st->codec->codec_type = link->type;
205         av_set_pts_info(st, 64, link->time_base.num, link->time_base.den);
206         if (link->type == AVMEDIA_TYPE_VIDEO) {
207             st->codec->codec_id   = CODEC_ID_RAWVIDEO;
208             st->codec->pix_fmt    = link->format;
209             st->codec->time_base  = link->time_base;
210             st->codec->width      = link->w;
211             st->codec->height     = link->h;
212             st       ->sample_aspect_ratio =
213             st->codec->sample_aspect_ratio = link->sample_aspect_ratio;
214         }
215     }
216
217 end:
218     avfilter_inout_free(&input_links);
219     avfilter_inout_free(&output_links);
220     if (ret < 0)
221         lavfi_read_close(avctx);
222     return ret;
223 }
224
225 static int lavfi_read_packet(AVFormatContext *avctx, AVPacket *pkt)
226 {
227     LavfiContext *lavfi = avctx->priv_data;
228     double min_pts = DBL_MAX;
229     int min_pts_sink_idx = 0;
230     AVFilterBufferRef *picref;
231     AVPicture pict;
232     int ret, i, size;
233
234     /* iterate through all the graph sinks. Select the sink with the
235      * minimum PTS */
236     for (i = 0; i < avctx->nb_streams; i++) {
237         AVRational tb = lavfi->sinks[i]->inputs[0]->time_base;
238         double d;
239         int ret = av_buffersink_get_buffer_ref(lavfi->sinks[i],
240                                                &picref, AV_BUFFERSINK_FLAG_PEEK);
241         if (ret < 0)
242             return ret;
243         d = av_rescale_q(picref->pts, tb, AV_TIME_BASE_Q);
244         av_dlog(avctx, "sink_idx:%d time:%f\n", i, d);
245
246         if (d < min_pts) {
247             min_pts = d;
248             min_pts_sink_idx = i;
249         }
250     }
251     av_dlog(avctx, "min_pts_sink_idx:%i\n", min_pts_sink_idx);
252
253     av_buffersink_get_buffer_ref(lavfi->sinks[min_pts_sink_idx], &picref, 0);
254
255     size = avpicture_get_size(picref->format, picref->video->w, picref->video->h);
256     if ((ret = av_new_packet(pkt, size)) < 0)
257         return ret;
258
259     memcpy(pict.data,     picref->data,     4*sizeof(picref->data[0]));
260     memcpy(pict.linesize, picref->linesize, 4*sizeof(picref->linesize[0]));
261
262     avpicture_layout(&pict, picref->format, picref->video->w,
263                      picref->video->h, pkt->data, size);
264     pkt->stream_index = lavfi->sink_stream_map[min_pts_sink_idx];
265     pkt->pts = picref->pts;
266     pkt->pos = picref->pos;
267     pkt->size = size;
268     avfilter_unref_buffer(picref);
269
270     return size;
271 }
272
273 #define OFFSET(x) offsetof(LavfiContext, x)
274
275 #define DEC AV_OPT_FLAG_DECODING_PARAM
276
277 static const AVOption options[] = {
278     { "graph", "Libavfilter graph", OFFSET(graph_str),  FF_OPT_TYPE_STRING, {.str = NULL }, 0,  0, DEC },
279     { NULL },
280 };
281
282 static const AVClass lavfi_class = {
283     .class_name = "lavfi indev",
284     .item_name  = av_default_item_name,
285     .option     = options,
286     .version    = LIBAVUTIL_VERSION_INT,
287 };
288
289 AVInputFormat ff_lavfi_demuxer = {
290     .name           = "lavfi",
291     .long_name      = NULL_IF_CONFIG_SMALL("Libavfilter virtual input device"),
292     .priv_data_size = sizeof(LavfiContext),
293     .read_header    = lavfi_read_header,
294     .read_packet    = lavfi_read_packet,
295     .read_close     = lavfi_read_close,
296     .flags          = AVFMT_NOFILE,
297     .priv_class     = &lavfi_class,
298 };