vsrc_movie: Adjust a silly typo from b977b287f61fea48ecd6251d54a26334213b7ec6
[platform/upstream/libav.git] / 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 #include <stdint.h>
33
34 #include "libavutil/attributes.h"
35 #include "libavutil/avstring.h"
36 #include "libavutil/opt.h"
37 #include "libavutil/imgutils.h"
38 #include "libavformat/avformat.h"
39 #include "avfilter.h"
40 #include "formats.h"
41 #include "internal.h"
42 #include "video.h"
43
44 typedef struct MovieContext {
45     const AVClass *class;
46     int64_t seek_point;   ///< seekpoint in microseconds
47     double seek_point_d;
48     char *format_name;
49     char *file_name;
50     int stream_index;
51
52     AVFormatContext *format_ctx;
53     AVCodecContext *codec_ctx;
54     int is_done;
55     AVFrame *frame;   ///< video frame to store the decoded images in
56
57     int w, h;
58 } MovieContext;
59
60 #define OFFSET(x) offsetof(MovieContext, x)
61 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM
62
63 static const AVOption movie_options[]= {
64     { "filename",     NULL,                      OFFSET(file_name),    AV_OPT_TYPE_STRING,                                    .flags = FLAGS },
65     { "format_name",  "set format name",         OFFSET(format_name),  AV_OPT_TYPE_STRING,                                    .flags = FLAGS },
66     { "f",            "set format name",         OFFSET(format_name),  AV_OPT_TYPE_STRING,                                    .flags = FLAGS },
67     { "stream_index", "set stream index",        OFFSET(stream_index), AV_OPT_TYPE_INT,    { .i64 = -1 }, -1, INT_MAX,                 FLAGS  },
68     { "si",           "set stream index",        OFFSET(stream_index), AV_OPT_TYPE_INT,    { .i64 = -1 }, -1, INT_MAX,                 FLAGS  },
69     { "seek_point",   "set seekpoint (seconds)", OFFSET(seek_point_d), AV_OPT_TYPE_DOUBLE, { .dbl =  0 },  0, (INT64_MAX-1) / 1000000, FLAGS },
70     { "sp",           "set seekpoint (seconds)", OFFSET(seek_point_d), AV_OPT_TYPE_DOUBLE, { .dbl =  0 },  0, (INT64_MAX-1) / 1000000, FLAGS },
71     { NULL },
72 };
73
74 static const char *movie_get_name(void *ctx)
75 {
76     return "movie";
77 }
78
79 static const AVClass movie_class = {
80     "MovieContext",
81     movie_get_name,
82     movie_options
83 };
84
85 static av_cold int movie_init(AVFilterContext *ctx)
86 {
87     MovieContext *movie = ctx->priv;
88     AVInputFormat *iformat = NULL;
89     AVCodec *codec;
90     int ret;
91     int64_t timestamp;
92
93     av_register_all();
94
95     // Try to find the movie format (container)
96     iformat = movie->format_name ? av_find_input_format(movie->format_name) : NULL;
97
98     movie->format_ctx = NULL;
99     if ((ret = avformat_open_input(&movie->format_ctx, movie->file_name, iformat, NULL)) < 0) {
100         av_log(ctx, AV_LOG_ERROR,
101                "Failed to avformat_open_input '%s'\n", movie->file_name);
102         return ret;
103     }
104     if ((ret = avformat_find_stream_info(movie->format_ctx, NULL)) < 0)
105         av_log(ctx, AV_LOG_WARNING, "Failed to find stream info\n");
106
107     // if seeking requested, we execute it
108     if (movie->seek_point > 0) {
109         timestamp = movie->seek_point;
110         // add the stream start time, should it exist
111         if (movie->format_ctx->start_time != AV_NOPTS_VALUE) {
112             if (timestamp > INT64_MAX - movie->format_ctx->start_time) {
113                 av_log(ctx, AV_LOG_ERROR,
114                        "%s: seek value overflow with start_time:%"PRId64" seek_point:%"PRId64"\n",
115                        movie->file_name, movie->format_ctx->start_time, movie->seek_point);
116                 return AVERROR(EINVAL);
117             }
118             timestamp += movie->format_ctx->start_time;
119         }
120         if ((ret = av_seek_frame(movie->format_ctx, -1, timestamp, AVSEEK_FLAG_BACKWARD)) < 0) {
121             av_log(ctx, AV_LOG_ERROR, "%s: could not seek to position %"PRId64"\n",
122                    movie->file_name, timestamp);
123             return ret;
124         }
125     }
126
127     /* select the video stream */
128     if ((ret = av_find_best_stream(movie->format_ctx, AVMEDIA_TYPE_VIDEO,
129                                    movie->stream_index, -1, NULL, 0)) < 0) {
130         av_log(ctx, AV_LOG_ERROR, "No video stream with index '%d' found\n",
131                movie->stream_index);
132         return ret;
133     }
134     movie->stream_index = ret;
135     movie->codec_ctx = movie->format_ctx->streams[movie->stream_index]->codec;
136
137     /*
138      * So now we've got a pointer to the so-called codec context for our video
139      * stream, but we still have to find the actual codec and open it.
140      */
141     codec = avcodec_find_decoder(movie->codec_ctx->codec_id);
142     if (!codec) {
143         av_log(ctx, AV_LOG_ERROR, "Failed to find any codec\n");
144         return AVERROR(EINVAL);
145     }
146
147     movie->codec_ctx->refcounted_frames = 1;
148
149     if ((ret = avcodec_open2(movie->codec_ctx, codec, NULL)) < 0) {
150         av_log(ctx, AV_LOG_ERROR, "Failed to open codec\n");
151         return ret;
152     }
153
154     movie->w = movie->codec_ctx->width;
155     movie->h = movie->codec_ctx->height;
156
157     av_log(ctx, AV_LOG_VERBOSE, "seek_point:%"PRIi64" format_name:%s file_name:%s stream_index:%d\n",
158            movie->seek_point, movie->format_name, movie->file_name,
159            movie->stream_index);
160
161     return 0;
162 }
163
164 static av_cold int init(AVFilterContext *ctx)
165 {
166     MovieContext *movie = ctx->priv;
167
168     movie->seek_point = movie->seek_point_d * 1000000 + 0.5;
169
170     return movie_init(ctx);
171 }
172
173 static av_cold void uninit(AVFilterContext *ctx)
174 {
175     MovieContext *movie = ctx->priv;
176
177     if (movie->codec_ctx)
178         avcodec_close(movie->codec_ctx);
179     if (movie->format_ctx)
180         avformat_close_input(&movie->format_ctx);
181     av_frame_free(&movie->frame);
182 }
183
184 static int query_formats(AVFilterContext *ctx)
185 {
186     MovieContext *movie = ctx->priv;
187     enum AVPixelFormat pix_fmts[] = { movie->codec_ctx->pix_fmt, AV_PIX_FMT_NONE };
188
189     ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
190     return 0;
191 }
192
193 static int config_output_props(AVFilterLink *outlink)
194 {
195     MovieContext *movie = outlink->src->priv;
196
197     outlink->w = movie->w;
198     outlink->h = movie->h;
199     outlink->time_base = movie->format_ctx->streams[movie->stream_index]->time_base;
200
201     return 0;
202 }
203
204 static int movie_get_frame(AVFilterLink *outlink)
205 {
206     MovieContext *movie = outlink->src->priv;
207     AVPacket pkt;
208     int ret, frame_decoded;
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 *
229                         av_q2d(movie->format_ctx->streams[movie->stream_index]->time_base),
230                         movie->frame->sample_aspect_ratio.num,
231                         movie->frame->sample_aspect_ratio.den);
232                 // We got it. Free the packet since we are returning
233                 av_free_packet(&pkt);
234
235                 return 0;
236             }
237         }
238         // Free the packet that was allocated by av_read_frame
239         av_free_packet(&pkt);
240     }
241
242     // On multi-frame source we should stop the mixing process when
243     // the movie source does not have more frames
244     if (ret == AVERROR_EOF)
245         movie->is_done = 1;
246     return ret;
247 }
248
249 static int request_frame(AVFilterLink *outlink)
250 {
251     MovieContext *movie = outlink->src->priv;
252     int ret;
253
254     if (movie->is_done)
255         return AVERROR_EOF;
256     if ((ret = movie_get_frame(outlink)) < 0)
257         return ret;
258
259     ret = ff_filter_frame(outlink, movie->frame);
260     movie->frame = NULL;
261
262     return ret;
263 }
264
265 static const AVFilterPad avfilter_vsrc_movie_outputs[] = {
266     {
267         .name          = "default",
268         .type          = AVMEDIA_TYPE_VIDEO,
269         .request_frame = request_frame,
270         .config_props  = config_output_props,
271     },
272     { NULL }
273 };
274
275 AVFilter ff_vsrc_movie = {
276     .name          = "movie",
277     .description   = NULL_IF_CONFIG_SMALL("Read from a movie source."),
278     .priv_size     = sizeof(MovieContext),
279     .priv_class    = &movie_class,
280     .init          = init,
281     .uninit        = uninit,
282     .query_formats = query_formats,
283
284     .inputs    = NULL,
285     .outputs   = avfilter_vsrc_movie_outputs,
286 };