]> git.sesse.net Git - ffmpeg/blob - libavfilter/vsrc_movie.c
lavfi: do not export the filters from shared objects
[ffmpeg] / libavfilter / vsrc_movie.c
1 /*
2  * Copyright (c) 2010 Stefano Sabatini
3  * Copyright (c) 2008 Victor Paesa
4  *
5  * This file is part of Libav.
6  *
7  * Libav is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * Libav is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 /**
23  * @file
24  * movie video source
25  *
26  * @todo use direct rendering (no allocation of a new frame)
27  * @todo support a PTS correction mechanism
28  * @todo support more than one output stream
29  */
30
31 #include <float.h>
32
33 #include "libavutil/attributes.h"
34 #include "libavutil/avstring.h"
35 #include "libavutil/opt.h"
36 #include "libavutil/imgutils.h"
37 #include "libavformat/avformat.h"
38 #include "avfilter.h"
39 #include "formats.h"
40 #include "internal.h"
41 #include "video.h"
42
43 typedef struct {
44     const AVClass *class;
45     int64_t seek_point;   ///< seekpoint in microseconds
46     double seek_point_d;
47     char *format_name;
48     char *file_name;
49     int stream_index;
50
51     AVFormatContext *format_ctx;
52     AVCodecContext *codec_ctx;
53     int is_done;
54     AVFrame *frame;   ///< video frame to store the decoded images in
55
56     int w, h;
57 } MovieContext;
58
59 #define OFFSET(x) offsetof(MovieContext, x)
60 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM
61
62 static const AVOption movie_options[]= {
63     { "filename",     NULL,                      OFFSET(file_name),    AV_OPT_TYPE_STRING,                                    .flags = FLAGS },
64     { "format_name",  "set format name",         OFFSET(format_name),  AV_OPT_TYPE_STRING,                                    .flags = FLAGS },
65     { "f",            "set format name",         OFFSET(format_name),  AV_OPT_TYPE_STRING,                                    .flags = FLAGS },
66     { "stream_index", "set stream index",        OFFSET(stream_index), AV_OPT_TYPE_INT,    { .i64 = -1 }, -1, INT_MAX,                 FLAGS  },
67     { "si",           "set stream index",        OFFSET(stream_index), AV_OPT_TYPE_INT,    { .i64 = -1 }, -1, INT_MAX,                 FLAGS  },
68     { "seek_point",   "set seekpoint (seconds)", OFFSET(seek_point_d), AV_OPT_TYPE_DOUBLE, { .dbl =  0 },  0, (INT64_MAX-1) / 1000000, FLAGS },
69     { "sp",           "set seekpoint (seconds)", OFFSET(seek_point_d), AV_OPT_TYPE_DOUBLE, { .dbl =  0 },  0, (INT64_MAX-1) / 1000000, FLAGS },
70     { NULL },
71 };
72
73 static const char *movie_get_name(void *ctx)
74 {
75     return "movie";
76 }
77
78 static const AVClass movie_class = {
79     "MovieContext",
80     movie_get_name,
81     movie_options
82 };
83
84 static av_cold int movie_init(AVFilterContext *ctx)
85 {
86     MovieContext *movie = ctx->priv;
87     AVInputFormat *iformat = NULL;
88     AVCodec *codec;
89     int ret;
90     int64_t timestamp;
91
92     av_register_all();
93
94     // Try to find the movie format (container)
95     iformat = movie->format_name ? av_find_input_format(movie->format_name) : NULL;
96
97     movie->format_ctx = NULL;
98     if ((ret = avformat_open_input(&movie->format_ctx, movie->file_name, iformat, NULL)) < 0) {
99         av_log(ctx, AV_LOG_ERROR,
100                "Failed to avformat_open_input '%s'\n", movie->file_name);
101         return ret;
102     }
103     if ((ret = avformat_find_stream_info(movie->format_ctx, NULL)) < 0)
104         av_log(ctx, AV_LOG_WARNING, "Failed to find stream info\n");
105
106     // if seeking requested, we execute it
107     if (movie->seek_point > 0) {
108         timestamp = movie->seek_point;
109         // add the stream start time, should it exist
110         if (movie->format_ctx->start_time != AV_NOPTS_VALUE) {
111             if (timestamp > INT64_MAX - movie->format_ctx->start_time) {
112                 av_log(ctx, AV_LOG_ERROR,
113                        "%s: seek value overflow with start_time:%"PRId64" seek_point:%"PRId64"\n",
114                        movie->file_name, movie->format_ctx->start_time, movie->seek_point);
115                 return AVERROR(EINVAL);
116             }
117             timestamp += movie->format_ctx->start_time;
118         }
119         if ((ret = av_seek_frame(movie->format_ctx, -1, timestamp, AVSEEK_FLAG_BACKWARD)) < 0) {
120             av_log(ctx, AV_LOG_ERROR, "%s: could not seek to position %"PRId64"\n",
121                    movie->file_name, timestamp);
122             return ret;
123         }
124     }
125
126     /* select the video stream */
127     if ((ret = av_find_best_stream(movie->format_ctx, AVMEDIA_TYPE_VIDEO,
128                                    movie->stream_index, -1, NULL, 0)) < 0) {
129         av_log(ctx, AV_LOG_ERROR, "No video stream with index '%d' found\n",
130                movie->stream_index);
131         return ret;
132     }
133     movie->stream_index = ret;
134     movie->codec_ctx = movie->format_ctx->streams[movie->stream_index]->codec;
135
136     /*
137      * So now we've got a pointer to the so-called codec context for our video
138      * stream, but we still have to find the actual codec and open it.
139      */
140     codec = avcodec_find_decoder(movie->codec_ctx->codec_id);
141     if (!codec) {
142         av_log(ctx, AV_LOG_ERROR, "Failed to find any codec\n");
143         return AVERROR(EINVAL);
144     }
145
146     movie->codec_ctx->refcounted_frames = 1;
147
148     if ((ret = avcodec_open2(movie->codec_ctx, codec, NULL)) < 0) {
149         av_log(ctx, AV_LOG_ERROR, "Failed to open codec\n");
150         return ret;
151     }
152
153     movie->w = movie->codec_ctx->width;
154     movie->h = movie->codec_ctx->height;
155
156     av_log(ctx, AV_LOG_VERBOSE, "seek_point:%"PRIi64" format_name:%s file_name:%s stream_index:%d\n",
157            movie->seek_point, movie->format_name, movie->file_name,
158            movie->stream_index);
159
160     return 0;
161 }
162
163 static av_cold int init(AVFilterContext *ctx)
164 {
165     MovieContext *movie = ctx->priv;
166
167     movie->seek_point = movie->seek_point_d * 1000000 + 0.5;
168
169     return movie_init(ctx);
170 }
171
172 static av_cold void uninit(AVFilterContext *ctx)
173 {
174     MovieContext *movie = ctx->priv;
175
176     if (movie->codec_ctx)
177         avcodec_close(movie->codec_ctx);
178     if (movie->format_ctx)
179         avformat_close_input(&movie->format_ctx);
180     av_frame_free(&movie->frame);
181 }
182
183 static int query_formats(AVFilterContext *ctx)
184 {
185     MovieContext *movie = ctx->priv;
186     enum AVPixelFormat pix_fmts[] = { movie->codec_ctx->pix_fmt, AV_PIX_FMT_NONE };
187
188     ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
189     return 0;
190 }
191
192 static int config_output_props(AVFilterLink *outlink)
193 {
194     MovieContext *movie = outlink->src->priv;
195
196     outlink->w = movie->w;
197     outlink->h = movie->h;
198     outlink->time_base = movie->format_ctx->streams[movie->stream_index]->time_base;
199
200     return 0;
201 }
202
203 static int movie_get_frame(AVFilterLink *outlink)
204 {
205     MovieContext *movie = outlink->src->priv;
206     AVPacket pkt;
207     int ret, frame_decoded;
208     AVStream av_unused *st = movie->format_ctx->streams[movie->stream_index];
209
210     if (movie->is_done == 1)
211         return 0;
212
213     movie->frame = av_frame_alloc();
214     if (!movie->frame)
215         return AVERROR(ENOMEM);
216
217     while ((ret = av_read_frame(movie->format_ctx, &pkt)) >= 0) {
218         // Is this a packet from the video stream?
219         if (pkt.stream_index == movie->stream_index) {
220             avcodec_decode_video2(movie->codec_ctx, movie->frame, &frame_decoded, &pkt);
221
222             if (frame_decoded) {
223                 if (movie->frame->pkt_pts != AV_NOPTS_VALUE)
224                     movie->frame->pts = movie->frame->pkt_pts;
225                 av_dlog(outlink->src,
226                         "movie_get_frame(): file:'%s' pts:%"PRId64" time:%f aspect:%d/%d\n",
227                         movie->file_name, movie->frame->pts,
228                         (double)movie->frame->pts * av_q2d(st->time_base),
229                         movie->frame->sample_aspect_ratio.num,
230                         movie->frame->sample_aspect_ratio.den);
231                 // We got it. Free the packet since we are returning
232                 av_free_packet(&pkt);
233
234                 return 0;
235             }
236         }
237         // Free the packet that was allocated by av_read_frame
238         av_free_packet(&pkt);
239     }
240
241     // On multi-frame source we should stop the mixing process when
242     // the movie source does not have more frames
243     if (ret == AVERROR_EOF)
244         movie->is_done = 1;
245     return ret;
246 }
247
248 static int request_frame(AVFilterLink *outlink)
249 {
250     MovieContext *movie = outlink->src->priv;
251     int ret;
252
253     if (movie->is_done)
254         return AVERROR_EOF;
255     if ((ret = movie_get_frame(outlink)) < 0)
256         return ret;
257
258     ret = ff_filter_frame(outlink, movie->frame);
259     movie->frame = NULL;
260
261     return ret;
262 }
263
264 static const AVFilterPad avfilter_vsrc_movie_outputs[] = {
265     {
266         .name          = "default",
267         .type          = AVMEDIA_TYPE_VIDEO,
268         .request_frame = request_frame,
269         .config_props  = config_output_props,
270     },
271     { NULL }
272 };
273
274 AVFilter ff_vsrc_movie = {
275     .name          = "movie",
276     .description   = NULL_IF_CONFIG_SMALL("Read from a movie source."),
277     .priv_size     = sizeof(MovieContext),
278     .priv_class    = &movie_class,
279     .init          = init,
280     .uninit        = uninit,
281     .query_formats = query_formats,
282
283     .inputs    = NULL,
284     .outputs   = avfilter_vsrc_movie_outputs,
285 };