2 * Copyright (c) 2010 Stefano Sabatini
3 * Copyright (c) 2008 Victor Paesa
5 * This file is part of FFmpeg.
7 * FFmpeg 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 * FFmpeg 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 FFmpeg; 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 /* common A/V fields */
44 int64_t seek_point; ///< seekpoint in microseconds
50 AVFormatContext *format_ctx;
51 AVCodecContext *codec_ctx;
53 AVFrame *frame; ///< video frame to store the decoded images in
55 /* video-only fields */
57 AVFilterBufferRef *picref;
59 /* audio-only fields */
60 int bps; ///< bytes per sample
62 AVFilterBufferRef *samplesref;
65 #define OFFSET(x) offsetof(MovieContext, x)
67 static const AVOption movie_options[]= {
68 {"format_name", "set format name", OFFSET(format_name), AV_OPT_TYPE_STRING, {.str = 0}, CHAR_MIN, CHAR_MAX },
69 {"f", "set format name", OFFSET(format_name), AV_OPT_TYPE_STRING, {.str = 0}, CHAR_MIN, CHAR_MAX },
70 {"stream_index", "set stream index", OFFSET(stream_index), AV_OPT_TYPE_INT, {.dbl = -1}, -1, INT_MAX },
71 {"si", "set stream index", OFFSET(stream_index), AV_OPT_TYPE_INT, {.dbl = -1}, -1, INT_MAX },
72 {"seek_point", "set seekpoint (seconds)", OFFSET(seek_point_d), AV_OPT_TYPE_DOUBLE, {.dbl = 0}, 0, (INT64_MAX-1) / 1000000 },
73 {"sp", "set seekpoint (seconds)", OFFSET(seek_point_d), AV_OPT_TYPE_DOUBLE, {.dbl = 0}, 0, (INT64_MAX-1) / 1000000 },
77 static const char *movie_get_name(void *ctx)
82 static const AVClass movie_class = {
88 static av_cold int movie_common_init(AVFilterContext *ctx, const char *args, void *opaque,
89 enum AVMediaType type)
91 MovieContext *movie = ctx->priv;
92 AVInputFormat *iformat = NULL;
97 movie->class = &movie_class;
98 av_opt_set_defaults(movie);
101 movie->file_name = av_get_token(&args, ":");
102 if (!movie->file_name || !*movie->file_name) {
103 av_log(ctx, AV_LOG_ERROR, "No filename provided!\n");
104 return AVERROR(EINVAL);
107 if (*args++ == ':' && (ret = av_set_options_string(movie, args, "=", ":")) < 0) {
108 av_log(ctx, AV_LOG_ERROR, "Error parsing options string: '%s'\n", args);
112 movie->seek_point = movie->seek_point_d * 1000000 + 0.5;
116 // Try to find the movie format (container)
117 iformat = movie->format_name ? av_find_input_format(movie->format_name) : NULL;
119 movie->format_ctx = NULL;
120 if ((ret = avformat_open_input(&movie->format_ctx, movie->file_name, iformat, NULL)) < 0) {
121 av_log(ctx, AV_LOG_ERROR,
122 "Failed to avformat_open_input '%s'\n", movie->file_name);
125 if ((ret = avformat_find_stream_info(movie->format_ctx, NULL)) < 0)
126 av_log(ctx, AV_LOG_WARNING, "Failed to find stream info\n");
128 // if seeking requested, we execute it
129 if (movie->seek_point > 0) {
130 timestamp = movie->seek_point;
131 // add the stream start time, should it exist
132 if (movie->format_ctx->start_time != AV_NOPTS_VALUE) {
133 if (timestamp > INT64_MAX - movie->format_ctx->start_time) {
134 av_log(ctx, AV_LOG_ERROR,
135 "%s: seek value overflow with start_time:%"PRId64" seek_point:%"PRId64"\n",
136 movie->file_name, movie->format_ctx->start_time, movie->seek_point);
137 return AVERROR(EINVAL);
139 timestamp += movie->format_ctx->start_time;
141 if ((ret = av_seek_frame(movie->format_ctx, -1, timestamp, AVSEEK_FLAG_BACKWARD)) < 0) {
142 av_log(ctx, AV_LOG_ERROR, "%s: could not seek to position %"PRId64"\n",
143 movie->file_name, timestamp);
148 /* select the media stream */
149 if ((ret = av_find_best_stream(movie->format_ctx, type,
150 movie->stream_index, -1, NULL, 0)) < 0) {
151 av_log(ctx, AV_LOG_ERROR, "No %s stream with index '%d' found\n",
152 av_get_media_type_string(type), movie->stream_index);
155 movie->stream_index = ret;
156 movie->codec_ctx = movie->format_ctx->streams[movie->stream_index]->codec;
159 * So now we've got a pointer to the so-called codec context for our video
160 * stream, but we still have to find the actual codec and open it.
162 codec = avcodec_find_decoder(movie->codec_ctx->codec_id);
164 av_log(ctx, AV_LOG_ERROR, "Failed to find any codec\n");
165 return AVERROR(EINVAL);
168 if ((ret = avcodec_open2(movie->codec_ctx, codec, NULL)) < 0) {
169 av_log(ctx, AV_LOG_ERROR, "Failed to open codec\n");
173 av_log(ctx, AV_LOG_INFO, "seek_point:%"PRIi64" format_name:%s file_name:%s stream_index:%d\n",
174 movie->seek_point, movie->format_name, movie->file_name,
175 movie->stream_index);
177 if (!(movie->frame = avcodec_alloc_frame()) ) {
178 av_log(ctx, AV_LOG_ERROR, "Failed to alloc frame\n");
179 return AVERROR(ENOMEM);
185 static av_cold void movie_common_uninit(AVFilterContext *ctx)
187 MovieContext *movie = ctx->priv;
189 av_free(movie->file_name);
190 av_free(movie->format_name);
191 if (movie->codec_ctx)
192 avcodec_close(movie->codec_ctx);
193 if (movie->format_ctx)
194 avformat_close_input(&movie->format_ctx);
196 avfilter_unref_buffer(movie->picref);
197 av_freep(&movie->frame);
199 avfilter_unref_buffer(movie->samplesref);
202 #if CONFIG_MOVIE_FILTER
204 static av_cold int movie_init(AVFilterContext *ctx, const char *args, void *opaque)
206 MovieContext *movie = ctx->priv;
209 if ((ret = movie_common_init(ctx, args, opaque, AVMEDIA_TYPE_VIDEO)) < 0)
212 movie->w = movie->codec_ctx->width;
213 movie->h = movie->codec_ctx->height;
218 static int movie_query_formats(AVFilterContext *ctx)
220 MovieContext *movie = ctx->priv;
221 enum PixelFormat pix_fmts[] = { movie->codec_ctx->pix_fmt, PIX_FMT_NONE };
223 avfilter_set_common_pixel_formats(ctx, avfilter_make_format_list(pix_fmts));
227 static int movie_config_output_props(AVFilterLink *outlink)
229 MovieContext *movie = outlink->src->priv;
231 outlink->w = movie->w;
232 outlink->h = movie->h;
233 outlink->time_base = movie->format_ctx->streams[movie->stream_index]->time_base;
238 static int movie_get_frame(AVFilterLink *outlink)
240 MovieContext *movie = outlink->src->priv;
242 int ret, frame_decoded;
243 AVStream *st = movie->format_ctx->streams[movie->stream_index];
245 if (movie->is_done == 1)
248 while ((ret = av_read_frame(movie->format_ctx, &pkt)) >= 0) {
249 // Is this a packet from the video stream?
250 if (pkt.stream_index == movie->stream_index) {
251 avcodec_decode_video2(movie->codec_ctx, movie->frame, &frame_decoded, &pkt);
254 /* FIXME: avoid the memcpy */
255 movie->picref = avfilter_get_video_buffer(outlink, AV_PERM_WRITE | AV_PERM_PRESERVE |
256 AV_PERM_REUSE2, outlink->w, outlink->h);
257 av_image_copy(movie->picref->data, movie->picref->linesize,
258 (void*)movie->frame->data, movie->frame->linesize,
259 movie->picref->format, outlink->w, outlink->h);
260 avfilter_copy_frame_props(movie->picref, movie->frame);
262 /* FIXME: use a PTS correction mechanism as that in
263 * ffplay.c when some API will be available for that */
264 /* use pkt_dts if pkt_pts is not available */
265 movie->picref->pts = movie->frame->pkt_pts == AV_NOPTS_VALUE ?
266 movie->frame->pkt_dts : movie->frame->pkt_pts;
267 if (!movie->frame->sample_aspect_ratio.num)
268 movie->picref->video->sample_aspect_ratio = st->sample_aspect_ratio;
269 av_dlog(outlink->src,
270 "movie_get_frame(): file:'%s' pts:%"PRId64" time:%lf pos:%"PRId64" aspect:%d/%d\n",
271 movie->file_name, movie->picref->pts,
272 (double)movie->picref->pts * av_q2d(st->time_base),
274 movie->picref->video->sample_aspect_ratio.num,
275 movie->picref->video->sample_aspect_ratio.den);
276 // We got it. Free the packet since we are returning
277 av_free_packet(&pkt);
282 // Free the packet that was allocated by av_read_frame
283 av_free_packet(&pkt);
286 // On multi-frame source we should stop the mixing process when
287 // the movie source does not have more frames
288 if (ret == AVERROR_EOF)
293 static int movie_request_frame(AVFilterLink *outlink)
295 AVFilterBufferRef *outpicref;
296 MovieContext *movie = outlink->src->priv;
301 if ((ret = movie_get_frame(outlink)) < 0)
304 outpicref = avfilter_ref_buffer(movie->picref, ~0);
305 avfilter_start_frame(outlink, outpicref);
306 avfilter_draw_slice(outlink, 0, outlink->h, 1);
307 avfilter_end_frame(outlink);
308 avfilter_unref_buffer(movie->picref);
309 movie->picref = NULL;
314 AVFilter avfilter_vsrc_movie = {
316 .description = NULL_IF_CONFIG_SMALL("Read from a movie source."),
317 .priv_size = sizeof(MovieContext),
319 .uninit = movie_common_uninit,
320 .query_formats = movie_query_formats,
322 .inputs = (const AVFilterPad[]) {{ .name = NULL }},
323 .outputs = (const AVFilterPad[]) {{ .name = "default",
324 .type = AVMEDIA_TYPE_VIDEO,
325 .request_frame = movie_request_frame,
326 .config_props = movie_config_output_props, },
330 #endif /* CONFIG_MOVIE_FILTER */
332 #if CONFIG_AMOVIE_FILTER
334 static av_cold int amovie_init(AVFilterContext *ctx, const char *args, void *opaque)
336 MovieContext *movie = ctx->priv;
339 if ((ret = movie_common_init(ctx, args, opaque, AVMEDIA_TYPE_AUDIO)) < 0)
342 movie->bps = av_get_bytes_per_sample(movie->codec_ctx->sample_fmt);
346 static int amovie_query_formats(AVFilterContext *ctx)
348 MovieContext *movie = ctx->priv;
349 AVCodecContext *c = movie->codec_ctx;
351 enum AVSampleFormat sample_fmts[] = { c->sample_fmt, -1 };
352 int packing_fmts[] = { AVFILTER_PACKED, -1 };
353 int64_t chlayouts[] = { c->channel_layout ? c->channel_layout :
354 av_get_default_channel_layout(c->channels), -1 };
356 avfilter_set_common_sample_formats (ctx, avfilter_make_format_list(sample_fmts));
357 avfilter_set_common_packing_formats(ctx, avfilter_make_format_list(packing_fmts));
358 avfilter_set_common_channel_layouts(ctx, avfilter_make_format64_list(chlayouts));
363 static int amovie_config_output_props(AVFilterLink *outlink)
365 MovieContext *movie = outlink->src->priv;
366 AVCodecContext *c = movie->codec_ctx;
368 outlink->sample_rate = c->sample_rate;
369 outlink->time_base = movie->format_ctx->streams[movie->stream_index]->time_base;
374 static int amovie_get_samples(AVFilterLink *outlink)
376 MovieContext *movie = outlink->src->priv;
378 int ret, got_frame = 0;
380 if (!movie->pkt.size && movie->is_done == 1)
383 /* check for another frame, in case the previous one was completely consumed */
384 if (!movie->pkt.size) {
385 while ((ret = av_read_frame(movie->format_ctx, &pkt)) >= 0) {
386 // Is this a packet from the selected stream?
387 if (pkt.stream_index != movie->stream_index) {
388 av_free_packet(&pkt);
391 movie->pkt0 = movie->pkt = pkt;
396 if (ret == AVERROR_EOF) {
402 /* decode and update the movie pkt */
403 avcodec_get_frame_defaults(movie->frame);
404 ret = avcodec_decode_audio4(movie->codec_ctx, movie->frame, &got_frame, &movie->pkt);
409 movie->pkt.data += ret;
410 movie->pkt.size -= ret;
412 /* wrap the decoded data in a samplesref */
414 int nb_samples = movie->frame->nb_samples;
416 av_samples_get_buffer_size(NULL, movie->codec_ctx->channels,
417 nb_samples, movie->codec_ctx->sample_fmt, 1);
421 avfilter_get_audio_buffer(outlink, AV_PERM_WRITE, nb_samples);
422 memcpy(movie->samplesref->data[0], movie->frame->data[0], data_size);
423 movie->samplesref->pts = movie->pkt.pts;
424 movie->samplesref->pos = movie->pkt.pos;
425 movie->samplesref->audio->sample_rate = movie->codec_ctx->sample_rate;
428 // We got it. Free the packet since we are returning
429 if (movie->pkt.size <= 0)
430 av_free_packet(&movie->pkt0);
435 static int amovie_request_frame(AVFilterLink *outlink)
437 MovieContext *movie = outlink->src->priv;
443 if ((ret = amovie_get_samples(outlink)) < 0)
445 } while (!movie->samplesref);
447 avfilter_filter_samples(outlink, avfilter_ref_buffer(movie->samplesref, ~0));
448 avfilter_unref_buffer(movie->samplesref);
449 movie->samplesref = NULL;
454 AVFilter avfilter_asrc_amovie = {
456 .description = NULL_IF_CONFIG_SMALL("Read audio from a movie source."),
457 .priv_size = sizeof(MovieContext),
459 .uninit = movie_common_uninit,
460 .query_formats = amovie_query_formats,
462 .inputs = (const AVFilterPad[]) {{ .name = NULL }},
463 .outputs = (const AVFilterPad[]) {{ .name = "default",
464 .type = AVMEDIA_TYPE_AUDIO,
465 .request_frame = amovie_request_frame,
466 .config_props = amovie_config_output_props, },
470 #endif /* CONFIG_AMOVIE_FILTER */