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"
45 int64_t seek_point; ///< seekpoint in microseconds
51 AVFormatContext *format_ctx;
52 AVCodecContext *codec_ctx;
54 AVFrame *frame; ///< video frame to store the decoded images in
57 AVFilterBufferRef *picref;
60 #define OFFSET(x) offsetof(MovieContext, x)
62 static const AVOption movie_options[]= {
63 {"format_name", "set format name", OFFSET(format_name), AV_OPT_TYPE_STRING, {.str = 0}, CHAR_MIN, CHAR_MAX },
64 {"f", "set format name", OFFSET(format_name), AV_OPT_TYPE_STRING, {.str = 0}, CHAR_MIN, CHAR_MAX },
65 {"stream_index", "set stream index", OFFSET(stream_index), AV_OPT_TYPE_INT, {.dbl = -1}, -1, INT_MAX },
66 {"si", "set stream index", OFFSET(stream_index), AV_OPT_TYPE_INT, {.dbl = -1}, -1, INT_MAX },
67 {"seek_point", "set seekpoint (seconds)", OFFSET(seek_point_d), AV_OPT_TYPE_DOUBLE, {.dbl = 0}, 0, (INT64_MAX-1) / 1000000 },
68 {"sp", "set seekpoint (seconds)", OFFSET(seek_point_d), AV_OPT_TYPE_DOUBLE, {.dbl = 0}, 0, (INT64_MAX-1) / 1000000 },
72 static const char *movie_get_name(void *ctx)
77 static const AVClass movie_class = {
83 static int movie_init(AVFilterContext *ctx)
85 MovieContext *movie = ctx->priv;
86 AVInputFormat *iformat = NULL;
93 // Try to find the movie format (container)
94 iformat = movie->format_name ? av_find_input_format(movie->format_name) : NULL;
96 movie->format_ctx = NULL;
97 if ((ret = avformat_open_input(&movie->format_ctx, movie->file_name, iformat, NULL)) < 0) {
98 av_log(ctx, AV_LOG_ERROR,
99 "Failed to avformat_open_input '%s'\n", movie->file_name);
102 if ((ret = avformat_find_stream_info(movie->format_ctx, NULL)) < 0)
103 av_log(ctx, AV_LOG_WARNING, "Failed to find stream info\n");
105 // if seeking requested, we execute it
106 if (movie->seek_point > 0) {
107 timestamp = movie->seek_point;
108 // add the stream start time, should it exist
109 if (movie->format_ctx->start_time != AV_NOPTS_VALUE) {
110 if (timestamp > INT64_MAX - movie->format_ctx->start_time) {
111 av_log(ctx, AV_LOG_ERROR,
112 "%s: seek value overflow with start_time:%"PRId64" seek_point:%"PRId64"\n",
113 movie->file_name, movie->format_ctx->start_time, movie->seek_point);
114 return AVERROR(EINVAL);
116 timestamp += movie->format_ctx->start_time;
118 if ((ret = av_seek_frame(movie->format_ctx, -1, timestamp, AVSEEK_FLAG_BACKWARD)) < 0) {
119 av_log(ctx, AV_LOG_ERROR, "%s: could not seek to position %"PRId64"\n",
120 movie->file_name, timestamp);
125 /* select the video stream */
126 if ((ret = av_find_best_stream(movie->format_ctx, AVMEDIA_TYPE_VIDEO,
127 movie->stream_index, -1, NULL, 0)) < 0) {
128 av_log(ctx, AV_LOG_ERROR, "No video stream with index '%d' found\n",
129 movie->stream_index);
132 movie->stream_index = ret;
133 movie->codec_ctx = movie->format_ctx->streams[movie->stream_index]->codec;
136 * So now we've got a pointer to the so-called codec context for our video
137 * stream, but we still have to find the actual codec and open it.
139 codec = avcodec_find_decoder(movie->codec_ctx->codec_id);
141 av_log(ctx, AV_LOG_ERROR, "Failed to find any codec\n");
142 return AVERROR(EINVAL);
145 if ((ret = avcodec_open2(movie->codec_ctx, codec, NULL)) < 0) {
146 av_log(ctx, AV_LOG_ERROR, "Failed to open codec\n");
150 if (!(movie->frame = avcodec_alloc_frame()) ) {
151 av_log(ctx, AV_LOG_ERROR, "Failed to alloc frame\n");
152 return AVERROR(ENOMEM);
155 movie->w = movie->codec_ctx->width;
156 movie->h = movie->codec_ctx->height;
158 av_log(ctx, AV_LOG_VERBOSE, "seek_point:%"PRIi64" format_name:%s file_name:%s stream_index:%d\n",
159 movie->seek_point, movie->format_name, movie->file_name,
160 movie->stream_index);
165 static av_cold int init(AVFilterContext *ctx, const char *args)
167 MovieContext *movie = ctx->priv;
169 movie->class = &movie_class;
170 av_opt_set_defaults(movie);
173 movie->file_name = av_get_token(&args, ":");
174 if (!movie->file_name || !*movie->file_name) {
175 av_log(ctx, AV_LOG_ERROR, "No filename provided!\n");
176 return AVERROR(EINVAL);
179 if (*args++ == ':' && (ret = av_set_options_string(movie, args, "=", ":")) < 0) {
180 av_log(ctx, AV_LOG_ERROR, "Error parsing options string: '%s'\n", args);
184 movie->seek_point = movie->seek_point_d * 1000000 + 0.5;
186 return movie_init(ctx);
189 static av_cold void uninit(AVFilterContext *ctx)
191 MovieContext *movie = ctx->priv;
193 av_free(movie->file_name);
194 av_free(movie->format_name);
195 if (movie->codec_ctx)
196 avcodec_close(movie->codec_ctx);
197 if (movie->format_ctx)
198 avformat_close_input(&movie->format_ctx);
199 avfilter_unref_buffer(movie->picref);
200 av_freep(&movie->frame);
203 static int query_formats(AVFilterContext *ctx)
205 MovieContext *movie = ctx->priv;
206 enum PixelFormat pix_fmts[] = { movie->codec_ctx->pix_fmt, PIX_FMT_NONE };
208 ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
212 static int config_output_props(AVFilterLink *outlink)
214 MovieContext *movie = outlink->src->priv;
216 outlink->w = movie->w;
217 outlink->h = movie->h;
218 outlink->time_base = movie->format_ctx->streams[movie->stream_index]->time_base;
223 static int movie_get_frame(AVFilterLink *outlink)
225 MovieContext *movie = outlink->src->priv;
227 int ret, frame_decoded;
228 AVStream *st = movie->format_ctx->streams[movie->stream_index];
230 if (movie->is_done == 1)
233 while ((ret = av_read_frame(movie->format_ctx, &pkt)) >= 0) {
234 // Is this a packet from the video stream?
235 if (pkt.stream_index == movie->stream_index) {
236 movie->codec_ctx->reordered_opaque = pkt.pos;
237 avcodec_decode_video2(movie->codec_ctx, movie->frame, &frame_decoded, &pkt);
240 /* FIXME: avoid the memcpy */
241 movie->picref = ff_get_video_buffer(outlink, AV_PERM_WRITE | AV_PERM_PRESERVE |
242 AV_PERM_REUSE2, outlink->w, outlink->h);
243 av_image_copy(movie->picref->data, movie->picref->linesize,
244 movie->frame->data, movie->frame->linesize,
245 movie->picref->format, outlink->w, outlink->h);
246 avfilter_copy_frame_props(movie->picref, movie->frame);
248 /* FIXME: use a PTS correction mechanism as that in
249 * ffplay.c when some API will be available for that */
250 /* use pkt_dts if pkt_pts is not available */
251 movie->picref->pts = movie->frame->pkt_pts == AV_NOPTS_VALUE ?
252 movie->frame->pkt_dts : movie->frame->pkt_pts;
254 movie->picref->pos = movie->frame->reordered_opaque;
255 if (!movie->frame->sample_aspect_ratio.num)
256 movie->picref->video->pixel_aspect = st->sample_aspect_ratio;
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 ff_start_frame(outlink, outpicref);
293 ff_draw_slice(outlink, 0, outlink->h, 1);
294 ff_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, },