vsrc_movie: convert to codecpar
[libav.git] / libavfilter / vsrc_movie.c
CommitLineData
9409c381
SS
1/*
2 * Copyright (c) 2010 Stefano Sabatini
3 * Copyright (c) 2008 Victor Paesa
4 *
2912e87a 5 * This file is part of Libav.
9409c381 6 *
2912e87a 7 * Libav is free software; you can redistribute it and/or
9409c381
SS
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 *
2912e87a 12 * Libav is distributed in the hope that it will be useful,
9409c381
SS
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
2912e87a 18 * License along with Libav; if not, write to the Free Software
9409c381
SS
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
9409c381 31#include <float.h>
8f8bc923 32#include <stdint.h>
093804a9
DB
33
34#include "libavutil/attributes.h"
9409c381
SS
35#include "libavutil/avstring.h"
36#include "libavutil/opt.h"
737eb597 37#include "libavutil/imgutils.h"
9409c381
SS
38#include "libavformat/avformat.h"
39#include "avfilter.h"
b74a1da4 40#include "formats.h"
9d0bfc50 41#include "internal.h"
803391f7 42#include "video.h"
9409c381 43
58400ac1 44typedef struct MovieContext {
9409c381
SS
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;
9409c381
SS
58} MovieContext;
59
60#define OFFSET(x) offsetof(MovieContext, x)
a42d6e6c 61#define FLAGS AV_OPT_FLAG_VIDEO_PARAM
9409c381
SS
62
63static const AVOption movie_options[]= {
a42d6e6c
AK
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 },
9409c381
SS
72};
73
74static const char *movie_get_name(void *ctx)
75{
76 return "movie";
77}
78
79static const AVClass movie_class = {
80 "MovieContext",
81 movie_get_name,
82 movie_options
83};
84
093804a9 85static av_cold int movie_init(AVFilterContext *ctx)
9409c381
SS
86{
87 MovieContext *movie = ctx->priv;
88 AVInputFormat *iformat = NULL;
1138eb55 89 AVStream *st;
9409c381
SS
90 AVCodec *codec;
91 int ret;
92 int64_t timestamp;
93
94 av_register_all();
95
96 // Try to find the movie format (container)
97 iformat = movie->format_name ? av_find_input_format(movie->format_name) : NULL;
98
99 movie->format_ctx = NULL;
603b8bc2 100 if ((ret = avformat_open_input(&movie->format_ctx, movie->file_name, iformat, NULL)) < 0) {
9409c381 101 av_log(ctx, AV_LOG_ERROR,
603b8bc2 102 "Failed to avformat_open_input '%s'\n", movie->file_name);
9409c381
SS
103 return ret;
104 }
a67c061e 105 if ((ret = avformat_find_stream_info(movie->format_ctx, NULL)) < 0)
9409c381
SS
106 av_log(ctx, AV_LOG_WARNING, "Failed to find stream info\n");
107
108 // if seeking requested, we execute it
109 if (movie->seek_point > 0) {
110 timestamp = movie->seek_point;
111 // add the stream start time, should it exist
112 if (movie->format_ctx->start_time != AV_NOPTS_VALUE) {
113 if (timestamp > INT64_MAX - movie->format_ctx->start_time) {
114 av_log(ctx, AV_LOG_ERROR,
115 "%s: seek value overflow with start_time:%"PRId64" seek_point:%"PRId64"\n",
116 movie->file_name, movie->format_ctx->start_time, movie->seek_point);
117 return AVERROR(EINVAL);
118 }
119 timestamp += movie->format_ctx->start_time;
120 }
121 if ((ret = av_seek_frame(movie->format_ctx, -1, timestamp, AVSEEK_FLAG_BACKWARD)) < 0) {
122 av_log(ctx, AV_LOG_ERROR, "%s: could not seek to position %"PRId64"\n",
123 movie->file_name, timestamp);
124 return ret;
125 }
126 }
127
128 /* select the video stream */
129 if ((ret = av_find_best_stream(movie->format_ctx, AVMEDIA_TYPE_VIDEO,
130 movie->stream_index, -1, NULL, 0)) < 0) {
131 av_log(ctx, AV_LOG_ERROR, "No video stream with index '%d' found\n",
132 movie->stream_index);
133 return ret;
134 }
135 movie->stream_index = ret;
1138eb55 136 st = movie->format_ctx->streams[movie->stream_index];
9409c381
SS
137
138 /*
139 * So now we've got a pointer to the so-called codec context for our video
140 * stream, but we still have to find the actual codec and open it.
141 */
1138eb55 142 codec = avcodec_find_decoder(st->codecpar->codec_id);
9409c381
SS
143 if (!codec) {
144 av_log(ctx, AV_LOG_ERROR, "Failed to find any codec\n");
145 return AVERROR(EINVAL);
146 }
147
1138eb55
AK
148 movie->codec_ctx = avcodec_alloc_context3(codec);
149 if (!movie->codec_ctx)
150 return AVERROR(ENOMEM);
151
152 ret = avcodec_parameters_to_context(movie->codec_ctx, st->codecpar);
153 if (ret < 0)
154 return ret;
155
7e350379
AK
156 movie->codec_ctx->refcounted_frames = 1;
157
0b950fe2 158 if ((ret = avcodec_open2(movie->codec_ctx, codec, NULL)) < 0) {
9409c381
SS
159 av_log(ctx, AV_LOG_ERROR, "Failed to open codec\n");
160 return ret;
161 }
162
9409c381
SS
163 movie->w = movie->codec_ctx->width;
164 movie->h = movie->codec_ctx->height;
165
1a49a169 166 av_log(ctx, AV_LOG_VERBOSE, "seek_point:%"PRIi64" format_name:%s file_name:%s stream_index:%d\n",
9409c381
SS
167 movie->seek_point, movie->format_name, movie->file_name,
168 movie->stream_index);
169
170 return 0;
171}
172
c43a7eca 173static av_cold int init(AVFilterContext *ctx)
9409c381
SS
174{
175 MovieContext *movie = ctx->priv;
9409c381
SS
176
177 movie->seek_point = movie->seek_point_d * 1000000 + 0.5;
178
179 return movie_init(ctx);
180}
181
182static av_cold void uninit(AVFilterContext *ctx)
183{
184 MovieContext *movie = ctx->priv;
185
1138eb55 186 avcodec_free_context(&movie->codec_ctx);
9409c381 187 if (movie->format_ctx)
cd3716b9 188 avformat_close_input(&movie->format_ctx);
7e350379 189 av_frame_free(&movie->frame);
9409c381
SS
190}
191
192static int query_formats(AVFilterContext *ctx)
193{
194 MovieContext *movie = ctx->priv;
716d413c 195 enum AVPixelFormat pix_fmts[] = { movie->codec_ctx->pix_fmt, AV_PIX_FMT_NONE };
9409c381 196
b74a1da4 197 ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
9409c381
SS
198 return 0;
199}
200
201static int config_output_props(AVFilterLink *outlink)
202{
203 MovieContext *movie = outlink->src->priv;
204
205 outlink->w = movie->w;
206 outlink->h = movie->h;
207 outlink->time_base = movie->format_ctx->streams[movie->stream_index]->time_base;
208
209 return 0;
210}
211
212static int movie_get_frame(AVFilterLink *outlink)
213{
214 MovieContext *movie = outlink->src->priv;
215 AVPacket pkt;
216 int ret, frame_decoded;
9409c381
SS
217
218 if (movie->is_done == 1)
219 return 0;
220
7e350379
AK
221 movie->frame = av_frame_alloc();
222 if (!movie->frame)
223 return AVERROR(ENOMEM);
224
9409c381
SS
225 while ((ret = av_read_frame(movie->format_ctx, &pkt)) >= 0) {
226 // Is this a packet from the video stream?
227 if (pkt.stream_index == movie->stream_index) {
9409c381
SS
228 avcodec_decode_video2(movie->codec_ctx, movie->frame, &frame_decoded, &pkt);
229
230 if (frame_decoded) {
7e350379
AK
231 if (movie->frame->pkt_pts != AV_NOPTS_VALUE)
232 movie->frame->pts = movie->frame->pkt_pts;
1a3eb042 233 av_log(outlink->src, AV_LOG_TRACE,
7e350379
AK
234 "movie_get_frame(): file:'%s' pts:%"PRId64" time:%f aspect:%d/%d\n",
235 movie->file_name, movie->frame->pts,
b977b287 236 (double)movie->frame->pts *
11cd727f 237 av_q2d(movie->format_ctx->streams[movie->stream_index]->time_base),
7e350379
AK
238 movie->frame->sample_aspect_ratio.num,
239 movie->frame->sample_aspect_ratio.den);
9409c381 240 // We got it. Free the packet since we are returning
ce70f28a 241 av_packet_unref(&pkt);
9409c381
SS
242
243 return 0;
244 }
245 }
246 // Free the packet that was allocated by av_read_frame
ce70f28a 247 av_packet_unref(&pkt);
9409c381
SS
248 }
249
250 // On multi-frame source we should stop the mixing process when
251 // the movie source does not have more frames
252 if (ret == AVERROR_EOF)
253 movie->is_done = 1;
254 return ret;
255}
256
257static int request_frame(AVFilterLink *outlink)
258{
9409c381
SS
259 MovieContext *movie = outlink->src->priv;
260 int ret;
261
262 if (movie->is_done)
263 return AVERROR_EOF;
264 if ((ret = movie_get_frame(outlink)) < 0)
265 return ret;
266
7e350379
AK
267 ret = ff_filter_frame(outlink, movie->frame);
268 movie->frame = NULL;
9409c381 269
3825b526 270 return ret;
9409c381
SS
271}
272
568c70e7
MR
273static const AVFilterPad avfilter_vsrc_movie_outputs[] = {
274 {
275 .name = "default",
276 .type = AVMEDIA_TYPE_VIDEO,
277 .request_frame = request_frame,
278 .config_props = config_output_props,
279 },
280 { NULL }
281};
282
cd43ca04 283AVFilter ff_vsrc_movie = {
9409c381
SS
284 .name = "movie",
285 .description = NULL_IF_CONFIG_SMALL("Read from a movie source."),
286 .priv_size = sizeof(MovieContext),
a42d6e6c 287 .priv_class = &movie_class,
9409c381
SS
288 .init = init,
289 .uninit = uninit,
290 .query_formats = query_formats,
291
1fce361d 292 .inputs = NULL,
568c70e7 293 .outputs = avfilter_vsrc_movie_outputs,
9409c381 294};