2 * Copyright (c) 2010 Stefano Sabatini
3 * Copyright (c) 2008 Victor Paesa
5 * This file is part of Libav.
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.
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.
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
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
34 #include "libavutil/avstring.h"
35 #include "libavutil/opt.h"
36 #include "libavutil/imgutils.h"
37 #include "libavformat/avformat.h"
42 int64_t seek_point; ///< seekpoint in microseconds
48 AVFormatContext *format_ctx;
49 AVCodecContext *codec_ctx;
51 AVFrame *frame; ///< video frame to store the decoded images in
54 AVFilterBufferRef *picref;
57 #define OFFSET(x) offsetof(MovieContext, x)
59 static const AVOption movie_options[]= {
60 {"format_name", "set format name", OFFSET(format_name), FF_OPT_TYPE_STRING, {.str = 0}, CHAR_MIN, CHAR_MAX },
61 {"f", "set format name", OFFSET(format_name), FF_OPT_TYPE_STRING, {.str = 0}, CHAR_MIN, CHAR_MAX },
62 {"stream_index", "set stream index", OFFSET(stream_index), FF_OPT_TYPE_INT, {.dbl = -1}, -1, INT_MAX },
63 {"si", "set stream index", OFFSET(stream_index), FF_OPT_TYPE_INT, {.dbl = -1}, -1, INT_MAX },
64 {"seek_point", "set seekpoint (seconds)", OFFSET(seek_point_d), FF_OPT_TYPE_DOUBLE, {.dbl = 0}, 0, (INT64_MAX-1) / 1000000 },
65 {"sp", "set seekpoint (seconds)", OFFSET(seek_point_d), FF_OPT_TYPE_DOUBLE, {.dbl = 0}, 0, (INT64_MAX-1) / 1000000 },
69 static const char *movie_get_name(void *ctx)
74 static const AVClass movie_class = {
80 static int movie_init(AVFilterContext *ctx)
82 MovieContext *movie = ctx->priv;
83 AVInputFormat *iformat = NULL;
90 // Try to find the movie format (container)
91 iformat = movie->format_name ? av_find_input_format(movie->format_name) : NULL;
93 movie->format_ctx = NULL;
94 if ((ret = avformat_open_input(&movie->format_ctx, movie->file_name, iformat, NULL)) < 0) {
95 av_log(ctx, AV_LOG_ERROR,
96 "Failed to avformat_open_input '%s'\n", movie->file_name);
99 if ((ret = avformat_find_stream_info(movie->format_ctx, NULL)) < 0)
100 av_log(ctx, AV_LOG_WARNING, "Failed to find stream info\n");
102 // if seeking requested, we execute it
103 if (movie->seek_point > 0) {
104 timestamp = movie->seek_point;
105 // add the stream start time, should it exist
106 if (movie->format_ctx->start_time != AV_NOPTS_VALUE) {
107 if (timestamp > INT64_MAX - movie->format_ctx->start_time) {
108 av_log(ctx, AV_LOG_ERROR,
109 "%s: seek value overflow with start_time:%"PRId64" seek_point:%"PRId64"\n",
110 movie->file_name, movie->format_ctx->start_time, movie->seek_point);
111 return AVERROR(EINVAL);
113 timestamp += movie->format_ctx->start_time;
115 if ((ret = av_seek_frame(movie->format_ctx, -1, timestamp, AVSEEK_FLAG_BACKWARD)) < 0) {
116 av_log(ctx, AV_LOG_ERROR, "%s: could not seek to position %"PRId64"\n",
117 movie->file_name, timestamp);
122 /* select the video stream */
123 if ((ret = av_find_best_stream(movie->format_ctx, AVMEDIA_TYPE_VIDEO,
124 movie->stream_index, -1, NULL, 0)) < 0) {
125 av_log(ctx, AV_LOG_ERROR, "No video stream with index '%d' found\n",
126 movie->stream_index);
129 movie->stream_index = ret;
130 movie->codec_ctx = movie->format_ctx->streams[movie->stream_index]->codec;
133 * So now we've got a pointer to the so-called codec context for our video
134 * stream, but we still have to find the actual codec and open it.
136 codec = avcodec_find_decoder(movie->codec_ctx->codec_id);
138 av_log(ctx, AV_LOG_ERROR, "Failed to find any codec\n");
139 return AVERROR(EINVAL);
142 if ((ret = avcodec_open2(movie->codec_ctx, codec, NULL)) < 0) {
143 av_log(ctx, AV_LOG_ERROR, "Failed to open codec\n");
147 if (!(movie->frame = avcodec_alloc_frame()) ) {
148 av_log(ctx, AV_LOG_ERROR, "Failed to alloc frame\n");
149 return AVERROR(ENOMEM);
152 movie->w = movie->codec_ctx->width;
153 movie->h = movie->codec_ctx->height;
155 av_log(ctx, AV_LOG_INFO, "seek_point:%"PRIi64" format_name:%s file_name:%s stream_index:%d\n",
156 movie->seek_point, movie->format_name, movie->file_name,
157 movie->stream_index);
162 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
164 MovieContext *movie = ctx->priv;
166 movie->class = &movie_class;
167 av_opt_set_defaults2(movie, 0, 0);
170 movie->file_name = av_get_token(&args, ":");
171 if (!movie->file_name || !*movie->file_name) {
172 av_log(ctx, AV_LOG_ERROR, "No filename provided!\n");
173 return AVERROR(EINVAL);
176 if (*args++ == ':' && (ret = av_set_options_string(movie, args, "=", ":")) < 0) {
177 av_log(ctx, AV_LOG_ERROR, "Error parsing options string: '%s'\n", args);
181 movie->seek_point = movie->seek_point_d * 1000000 + 0.5;
183 return movie_init(ctx);
186 static av_cold void uninit(AVFilterContext *ctx)
188 MovieContext *movie = ctx->priv;
190 av_free(movie->file_name);
191 av_free(movie->format_name);
192 if (movie->codec_ctx)
193 avcodec_close(movie->codec_ctx);
194 if (movie->format_ctx)
195 av_close_input_file(movie->format_ctx);
196 avfilter_unref_buffer(movie->picref);
197 av_freep(&movie->frame);
200 static int query_formats(AVFilterContext *ctx)
202 MovieContext *movie = ctx->priv;
203 enum PixelFormat pix_fmts[] = { movie->codec_ctx->pix_fmt, PIX_FMT_NONE };
205 avfilter_set_common_formats(ctx, avfilter_make_format_list(pix_fmts));
209 static int config_output_props(AVFilterLink *outlink)
211 MovieContext *movie = outlink->src->priv;
213 outlink->w = movie->w;
214 outlink->h = movie->h;
215 outlink->time_base = movie->format_ctx->streams[movie->stream_index]->time_base;
220 static int movie_get_frame(AVFilterLink *outlink)
222 MovieContext *movie = outlink->src->priv;
224 int ret, frame_decoded;
225 AVStream *st = movie->format_ctx->streams[movie->stream_index];
227 if (movie->is_done == 1)
230 while ((ret = av_read_frame(movie->format_ctx, &pkt)) >= 0) {
231 // Is this a packet from the video stream?
232 if (pkt.stream_index == movie->stream_index) {
233 movie->codec_ctx->reordered_opaque = pkt.pos;
234 avcodec_decode_video2(movie->codec_ctx, movie->frame, &frame_decoded, &pkt);
237 /* FIXME: avoid the memcpy */
238 movie->picref = avfilter_get_video_buffer(outlink, AV_PERM_WRITE | AV_PERM_PRESERVE |
239 AV_PERM_REUSE2, outlink->w, outlink->h);
240 av_image_copy(movie->picref->data, movie->picref->linesize,
241 movie->frame->data, movie->frame->linesize,
242 movie->picref->format, outlink->w, outlink->h);
244 /* FIXME: use a PTS correction mechanism as that in
245 * ffplay.c when some API will be available for that */
246 /* use pkt_dts if pkt_pts is not available */
247 movie->picref->pts = movie->frame->pkt_pts == AV_NOPTS_VALUE ?
248 movie->frame->pkt_dts : movie->frame->pkt_pts;
250 movie->picref->pos = movie->frame->reordered_opaque;
251 movie->picref->video->pixel_aspect = st->sample_aspect_ratio.num ?
252 st->sample_aspect_ratio : movie->codec_ctx->sample_aspect_ratio;
253 movie->picref->video->interlaced = movie->frame->interlaced_frame;
254 movie->picref->video->top_field_first = movie->frame->top_field_first;
255 movie->picref->video->key_frame = movie->frame->key_frame;
256 movie->picref->video->pict_type = movie->frame->pict_type;
257 av_dlog(outlink->src,
258 "movie_get_frame(): file:'%s' pts:%"PRId64" time:%lf pos:%"PRId64" aspect:%d/%d\n",
259 movie->file_name, movie->picref->pts,
260 (double)movie->picref->pts * av_q2d(st->time_base),
262 movie->picref->video->pixel_aspect.num, movie->picref->video->pixel_aspect.den);
263 // We got it. Free the packet since we are returning
264 av_free_packet(&pkt);
269 // Free the packet that was allocated by av_read_frame
270 av_free_packet(&pkt);
273 // On multi-frame source we should stop the mixing process when
274 // the movie source does not have more frames
275 if (ret == AVERROR_EOF)
280 static int request_frame(AVFilterLink *outlink)
282 AVFilterBufferRef *outpicref;
283 MovieContext *movie = outlink->src->priv;
288 if ((ret = movie_get_frame(outlink)) < 0)
291 outpicref = avfilter_ref_buffer(movie->picref, ~0);
292 avfilter_start_frame(outlink, outpicref);
293 avfilter_draw_slice(outlink, 0, outlink->h, 1);
294 avfilter_end_frame(outlink);
295 avfilter_unref_buffer(movie->picref);
296 movie->picref = NULL;
301 AVFilter avfilter_vsrc_movie = {
303 .description = NULL_IF_CONFIG_SMALL("Read from a movie source."),
304 .priv_size = sizeof(MovieContext),
307 .query_formats = query_formats,
309 .inputs = (AVFilterPad[]) {{ .name = NULL }},
310 .outputs = (AVFilterPad[]) {{ .name = "default",
311 .type = AVMEDIA_TYPE_VIDEO,
312 .request_frame = request_frame,
313 .config_props = config_output_props, },