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 */
62 int bps; ///< bytes per sample
64 AVFilterBufferRef *samplesref;
67 #define OFFSET(x) offsetof(MovieContext, x)
69 static const AVOption movie_options[]= {
70 {"format_name", "set format name", OFFSET(format_name), FF_OPT_TYPE_STRING, {.str = 0}, CHAR_MIN, CHAR_MAX },
71 {"f", "set format name", OFFSET(format_name), FF_OPT_TYPE_STRING, {.str = 0}, CHAR_MIN, CHAR_MAX },
72 {"stream_index", "set stream index", OFFSET(stream_index), FF_OPT_TYPE_INT, {.dbl = -1}, -1, INT_MAX },
73 {"si", "set stream index", OFFSET(stream_index), FF_OPT_TYPE_INT, {.dbl = -1}, -1, INT_MAX },
74 {"seek_point", "set seekpoint (seconds)", OFFSET(seek_point_d), FF_OPT_TYPE_DOUBLE, {.dbl = 0}, 0, (INT64_MAX-1) / 1000000 },
75 {"sp", "set seekpoint (seconds)", OFFSET(seek_point_d), FF_OPT_TYPE_DOUBLE, {.dbl = 0}, 0, (INT64_MAX-1) / 1000000 },
79 static const char *movie_get_name(void *ctx)
84 static const AVClass movie_class = {
90 static av_cold int movie_common_init(AVFilterContext *ctx, const char *args, void *opaque,
91 enum AVMediaType type)
93 MovieContext *movie = ctx->priv;
94 AVInputFormat *iformat = NULL;
99 movie->class = &movie_class;
100 av_opt_set_defaults2(movie, 0, 0);
103 movie->file_name = av_get_token(&args, ":");
104 if (!movie->file_name || !*movie->file_name) {
105 av_log(ctx, AV_LOG_ERROR, "No filename provided!\n");
106 return AVERROR(EINVAL);
109 if (*args++ == ':' && (ret = av_set_options_string(movie, args, "=", ":")) < 0) {
110 av_log(ctx, AV_LOG_ERROR, "Error parsing options string: '%s'\n", args);
114 movie->seek_point = movie->seek_point_d * 1000000 + 0.5;
118 // Try to find the movie format (container)
119 iformat = movie->format_name ? av_find_input_format(movie->format_name) : NULL;
121 movie->format_ctx = NULL;
122 if ((ret = avformat_open_input(&movie->format_ctx, movie->file_name, iformat, NULL)) < 0) {
123 av_log(ctx, AV_LOG_ERROR,
124 "Failed to avformat_open_input '%s'\n", movie->file_name);
127 if ((ret = avformat_find_stream_info(movie->format_ctx, NULL)) < 0)
128 av_log(ctx, AV_LOG_WARNING, "Failed to find stream info\n");
130 // if seeking requested, we execute it
131 if (movie->seek_point > 0) {
132 timestamp = movie->seek_point;
133 // add the stream start time, should it exist
134 if (movie->format_ctx->start_time != AV_NOPTS_VALUE) {
135 if (timestamp > INT64_MAX - movie->format_ctx->start_time) {
136 av_log(ctx, AV_LOG_ERROR,
137 "%s: seek value overflow with start_time:%"PRId64" seek_point:%"PRId64"\n",
138 movie->file_name, movie->format_ctx->start_time, movie->seek_point);
139 return AVERROR(EINVAL);
141 timestamp += movie->format_ctx->start_time;
143 if ((ret = av_seek_frame(movie->format_ctx, -1, timestamp, AVSEEK_FLAG_BACKWARD)) < 0) {
144 av_log(ctx, AV_LOG_ERROR, "%s: could not seek to position %"PRId64"\n",
145 movie->file_name, timestamp);
150 /* select the media stream */
151 if ((ret = av_find_best_stream(movie->format_ctx, type,
152 movie->stream_index, -1, NULL, 0)) < 0) {
153 av_log(ctx, AV_LOG_ERROR, "No %s stream with index '%d' found\n",
154 av_get_media_type_string(type), movie->stream_index);
157 movie->stream_index = ret;
158 movie->codec_ctx = movie->format_ctx->streams[movie->stream_index]->codec;
161 * So now we've got a pointer to the so-called codec context for our video
162 * stream, but we still have to find the actual codec and open it.
164 codec = avcodec_find_decoder(movie->codec_ctx->codec_id);
166 av_log(ctx, AV_LOG_ERROR, "Failed to find any codec\n");
167 return AVERROR(EINVAL);
170 if ((ret = avcodec_open2(movie->codec_ctx, codec, NULL)) < 0) {
171 av_log(ctx, AV_LOG_ERROR, "Failed to open codec\n");
175 av_log(ctx, AV_LOG_INFO, "seek_point:%"PRIi64" format_name:%s file_name:%s stream_index:%d\n",
176 movie->seek_point, movie->format_name, movie->file_name,
177 movie->stream_index);
182 static av_cold void movie_common_uninit(AVFilterContext *ctx)
184 MovieContext *movie = ctx->priv;
186 av_free(movie->file_name);
187 av_free(movie->format_name);
188 if (movie->codec_ctx)
189 avcodec_close(movie->codec_ctx);
190 if (movie->format_ctx)
191 av_close_input_file(movie->format_ctx);
193 avfilter_unref_buffer(movie->picref);
194 av_freep(&movie->frame);
196 avfilter_unref_buffer(movie->samplesref);
197 av_freep(&movie->samples_buf);
200 #if CONFIG_MOVIE_FILTER
202 static av_cold int movie_init(AVFilterContext *ctx, const char *args, void *opaque)
204 MovieContext *movie = ctx->priv;
207 if ((ret = movie_common_init(ctx, args, opaque, AVMEDIA_TYPE_VIDEO)) < 0)
210 if (!(movie->frame = avcodec_alloc_frame()) ) {
211 av_log(ctx, AV_LOG_ERROR, "Failed to alloc frame\n");
212 return AVERROR(ENOMEM);
215 movie->w = movie->codec_ctx->width;
216 movie->h = movie->codec_ctx->height;
221 static int movie_query_formats(AVFilterContext *ctx)
223 MovieContext *movie = ctx->priv;
224 enum PixelFormat pix_fmts[] = { movie->codec_ctx->pix_fmt, PIX_FMT_NONE };
226 avfilter_set_common_pixel_formats(ctx, avfilter_make_format_list(pix_fmts));
230 static int movie_config_output_props(AVFilterLink *outlink)
232 MovieContext *movie = outlink->src->priv;
234 outlink->w = movie->w;
235 outlink->h = movie->h;
236 outlink->time_base = movie->format_ctx->streams[movie->stream_index]->time_base;
241 static int movie_get_frame(AVFilterLink *outlink)
243 MovieContext *movie = outlink->src->priv;
245 int ret, frame_decoded;
246 AVStream *st = movie->format_ctx->streams[movie->stream_index];
248 if (movie->is_done == 1)
251 while ((ret = av_read_frame(movie->format_ctx, &pkt)) >= 0) {
252 // Is this a packet from the video stream?
253 if (pkt.stream_index == movie->stream_index) {
254 avcodec_decode_video2(movie->codec_ctx, movie->frame, &frame_decoded, &pkt);
257 /* FIXME: avoid the memcpy */
258 movie->picref = avfilter_get_video_buffer(outlink, AV_PERM_WRITE | AV_PERM_PRESERVE |
259 AV_PERM_REUSE2, outlink->w, outlink->h);
260 av_image_copy(movie->picref->data, movie->picref->linesize,
261 movie->frame->data, movie->frame->linesize,
262 movie->picref->format, outlink->w, outlink->h);
263 avfilter_copy_frame_props(movie->picref, movie->frame);
265 /* FIXME: use a PTS correction mechanism as that in
266 * ffplay.c when some API will be available for that */
267 /* use pkt_dts if pkt_pts is not available */
268 movie->picref->pts = movie->frame->pkt_pts == AV_NOPTS_VALUE ?
269 movie->frame->pkt_dts : movie->frame->pkt_pts;
270 if (!movie->frame->sample_aspect_ratio.num)
271 movie->picref->video->sample_aspect_ratio = st->sample_aspect_ratio;
272 av_dlog(outlink->src,
273 "movie_get_frame(): file:'%s' pts:%"PRId64" time:%lf pos:%"PRId64" aspect:%d/%d\n",
274 movie->file_name, movie->picref->pts,
275 (double)movie->picref->pts * av_q2d(st->time_base),
277 movie->picref->video->sample_aspect_ratio.num,
278 movie->picref->video->sample_aspect_ratio.den);
279 // We got it. Free the packet since we are returning
280 av_free_packet(&pkt);
285 // Free the packet that was allocated by av_read_frame
286 av_free_packet(&pkt);
289 // On multi-frame source we should stop the mixing process when
290 // the movie source does not have more frames
291 if (ret == AVERROR_EOF)
296 static int movie_request_frame(AVFilterLink *outlink)
298 AVFilterBufferRef *outpicref;
299 MovieContext *movie = outlink->src->priv;
304 if ((ret = movie_get_frame(outlink)) < 0)
307 outpicref = avfilter_ref_buffer(movie->picref, ~0);
308 avfilter_start_frame(outlink, outpicref);
309 avfilter_draw_slice(outlink, 0, outlink->h, 1);
310 avfilter_end_frame(outlink);
311 avfilter_unref_buffer(movie->picref);
312 movie->picref = NULL;
317 AVFilter avfilter_vsrc_movie = {
319 .description = NULL_IF_CONFIG_SMALL("Read from a movie source."),
320 .priv_size = sizeof(MovieContext),
322 .uninit = movie_common_uninit,
323 .query_formats = movie_query_formats,
325 .inputs = (AVFilterPad[]) {{ .name = NULL }},
326 .outputs = (AVFilterPad[]) {{ .name = "default",
327 .type = AVMEDIA_TYPE_VIDEO,
328 .request_frame = movie_request_frame,
329 .config_props = movie_config_output_props, },
333 #endif /* CONFIG_MOVIE_FILTER */
335 #if CONFIG_AMOVIE_FILTER
337 static av_cold int amovie_init(AVFilterContext *ctx, const char *args, void *opaque)
339 MovieContext *movie = ctx->priv;
342 if ((ret = movie_common_init(ctx, args, opaque, AVMEDIA_TYPE_AUDIO)) < 0)
345 movie->bps = av_get_bytes_per_sample(movie->codec_ctx->sample_fmt);
349 static int amovie_query_formats(AVFilterContext *ctx)
351 MovieContext *movie = ctx->priv;
352 AVCodecContext *c = movie->codec_ctx;
354 enum AVSampleFormat sample_fmts[] = { c->sample_fmt, -1 };
355 int packing_fmts[] = { AVFILTER_PACKED, -1 };
356 int64_t chlayouts[] = { c->channel_layout, -1 };
358 avfilter_set_common_sample_formats (ctx, avfilter_make_format_list(sample_fmts));
359 avfilter_set_common_packing_formats(ctx, avfilter_make_format_list(packing_fmts));
360 avfilter_set_common_channel_layouts(ctx, avfilter_make_format64_list(chlayouts));
365 static int amovie_config_output_props(AVFilterLink *outlink)
367 MovieContext *movie = outlink->src->priv;
368 AVCodecContext *c = movie->codec_ctx;
370 outlink->sample_rate = c->sample_rate;
371 outlink->time_base = movie->format_ctx->streams[movie->stream_index]->time_base;
376 static int amovie_get_samples(AVFilterLink *outlink)
378 MovieContext *movie = outlink->src->priv;
380 int ret, samples_size, decoded_data_size;
382 if (!movie->pkt.size && movie->is_done == 1)
385 /* check for another frame, in case the previous one was completely consumed */
386 if (!movie->pkt.size) {
387 while ((ret = av_read_frame(movie->format_ctx, &pkt)) >= 0) {
388 // Is this a packet from the selected stream?
389 if (pkt.stream_index != movie->stream_index) {
390 av_free_packet(&pkt);
393 movie->pkt0 = movie->pkt = pkt;
398 if (ret == AVERROR_EOF) {
404 /* reallocate the buffer for the decoded samples, if necessary */
406 FFMAX(movie->pkt.size*sizeof(movie->bps), AVCODEC_MAX_AUDIO_FRAME_SIZE);
407 if (samples_size > movie->samples_buf_size) {
408 movie->samples_buf = av_fast_realloc(movie->samples_buf,
409 &movie->samples_buf_size, samples_size);
410 if (!movie->samples_buf)
411 return AVERROR(ENOMEM);
413 decoded_data_size = movie->samples_buf_size;
415 /* decode and update the movie pkt */
416 ret = avcodec_decode_audio3(movie->codec_ctx, movie->samples_buf,
417 &decoded_data_size, &movie->pkt);
420 movie->pkt.data += ret;
421 movie->pkt.size -= ret;
423 /* wrap the decoded data in a samplesref */
424 if (decoded_data_size > 0) {
425 int nb_samples = decoded_data_size / movie->bps / movie->codec_ctx->channels;
427 avfilter_get_audio_buffer(outlink, AV_PERM_WRITE,
428 movie->codec_ctx->sample_fmt, nb_samples,
429 movie->codec_ctx->channel_layout, 0);
430 memcpy(movie->samplesref->data[0], movie->samples_buf, decoded_data_size);
431 movie->samplesref->pts = movie->pkt.pts;
432 movie->samplesref->pos = movie->pkt.pos;
433 movie->samplesref->audio->sample_rate = movie->codec_ctx->sample_rate;
436 // We got it. Free the packet since we are returning
437 if (movie->pkt.size <= 0)
438 av_free_packet(&movie->pkt0);
443 static int amovie_request_frame(AVFilterLink *outlink)
445 MovieContext *movie = outlink->src->priv;
450 if ((ret = amovie_get_samples(outlink)) < 0)
453 avfilter_filter_samples(outlink, avfilter_ref_buffer(movie->samplesref, ~0));
454 avfilter_unref_buffer(movie->samplesref);
455 movie->samplesref = NULL;
460 AVFilter avfilter_asrc_amovie = {
462 .description = NULL_IF_CONFIG_SMALL("Read audio from a movie source."),
463 .priv_size = sizeof(MovieContext),
465 .uninit = movie_common_uninit,
466 .query_formats = amovie_query_formats,
468 .inputs = (AVFilterPad[]) {{ .name = NULL }},
469 .outputs = (AVFilterPad[]) {{ .name = "default",
470 .type = AVMEDIA_TYPE_AUDIO,
471 .request_frame = amovie_request_frame,
472 .config_props = amovie_config_output_props, },
476 #endif /* CONFIG_AMOVIE_FILTER */