ffmpeg / libavfilter / vsrc_movie.c @ 1ba57272
History | View | Annotate | Download (11.1 KB)
1 |
/*
|
---|---|
2 |
* Copyright (c) 2010 Stefano Sabatini
|
3 |
* Copyright (c) 2008 Victor Paesa
|
4 |
*
|
5 |
* This file is part of FFmpeg.
|
6 |
*
|
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.
|
11 |
*
|
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.
|
16 |
*
|
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
|
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 |
|
31 |
/* #define DEBUG */
|
32 |
|
33 |
#include <float.h> |
34 |
#include "libavutil/avstring.h" |
35 |
#include "libavutil/opt.h" |
36 |
#include "libavutil/imgutils.h" |
37 |
#include "libavformat/avformat.h" |
38 |
#include "avfilter.h" |
39 |
|
40 |
typedef struct { |
41 |
const AVClass *class;
|
42 |
int64_t seek_point; ///< seekpoint in microseconds
|
43 |
double seek_point_d;
|
44 |
char *format_name;
|
45 |
char *file_name;
|
46 |
int stream_index;
|
47 |
|
48 |
AVFormatContext *format_ctx; |
49 |
AVCodecContext *codec_ctx; |
50 |
int is_done;
|
51 |
AVFrame *frame; ///< video frame to store the decoded images in
|
52 |
|
53 |
int w, h;
|
54 |
AVFilterBufferRef *picref; |
55 |
} MovieContext; |
56 |
|
57 |
#define OFFSET(x) offsetof(MovieContext, x)
|
58 |
|
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 }, |
66 |
{NULL},
|
67 |
}; |
68 |
|
69 |
static const char *movie_get_name(void *ctx) |
70 |
{ |
71 |
return "movie"; |
72 |
} |
73 |
|
74 |
static const AVClass movie_class = { |
75 |
"MovieContext",
|
76 |
movie_get_name, |
77 |
movie_options |
78 |
}; |
79 |
|
80 |
static int movie_init(AVFilterContext *ctx) |
81 |
{ |
82 |
MovieContext *movie = ctx->priv; |
83 |
AVInputFormat *iformat = NULL;
|
84 |
AVCodec *codec; |
85 |
int ret;
|
86 |
int64_t timestamp; |
87 |
|
88 |
av_register_all(); |
89 |
|
90 |
// Try to find the movie format (container)
|
91 |
iformat = movie->format_name ? av_find_input_format(movie->format_name) : NULL;
|
92 |
|
93 |
movie->format_ctx = NULL;
|
94 |
if ((ret = av_open_input_file(&movie->format_ctx, movie->file_name, iformat, 0, NULL)) < 0) { |
95 |
av_log(ctx, AV_LOG_ERROR, |
96 |
"Failed to av_open_input_file '%s'\n", movie->file_name);
|
97 |
return ret;
|
98 |
} |
99 |
if ((ret = av_find_stream_info(movie->format_ctx)) < 0) |
100 |
av_log(ctx, AV_LOG_WARNING, "Failed to find stream info\n");
|
101 |
|
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);
|
112 |
} |
113 |
timestamp += movie->format_ctx->start_time; |
114 |
} |
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); |
118 |
return ret;
|
119 |
} |
120 |
} |
121 |
|
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); |
127 |
return ret;
|
128 |
} |
129 |
movie->stream_index = ret; |
130 |
movie->codec_ctx = movie->format_ctx->streams[movie->stream_index]->codec; |
131 |
|
132 |
/*
|
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.
|
135 |
*/
|
136 |
codec = avcodec_find_decoder(movie->codec_ctx->codec_id); |
137 |
if (!codec) {
|
138 |
av_log(ctx, AV_LOG_ERROR, "Failed to find any codec\n");
|
139 |
return AVERROR(EINVAL);
|
140 |
} |
141 |
|
142 |
if ((ret = avcodec_open(movie->codec_ctx, codec)) < 0) { |
143 |
av_log(ctx, AV_LOG_ERROR, "Failed to open codec\n");
|
144 |
return ret;
|
145 |
} |
146 |
|
147 |
if (!(movie->frame = avcodec_alloc_frame()) ) {
|
148 |
av_log(ctx, AV_LOG_ERROR, "Failed to alloc frame\n");
|
149 |
return AVERROR(ENOMEM);
|
150 |
} |
151 |
|
152 |
movie->w = movie->codec_ctx->width; |
153 |
movie->h = movie->codec_ctx->height; |
154 |
|
155 |
av_log(ctx, AV_LOG_INFO, "seek_point:%lld format_name:%s file_name:%s stream_index:%d\n",
|
156 |
movie->seek_point, movie->format_name, movie->file_name, |
157 |
movie->stream_index); |
158 |
|
159 |
return 0; |
160 |
} |
161 |
|
162 |
static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque) |
163 |
{ |
164 |
MovieContext *movie = ctx->priv; |
165 |
int ret;
|
166 |
movie->class = &movie_class; |
167 |
av_opt_set_defaults2(movie, 0, 0); |
168 |
|
169 |
if (args)
|
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);
|
174 |
} |
175 |
|
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);
|
178 |
return ret;
|
179 |
} |
180 |
|
181 |
movie->seek_point = movie->seek_point_d * 1000000 + 0.5; |
182 |
|
183 |
return movie_init(ctx);
|
184 |
} |
185 |
|
186 |
static av_cold void uninit(AVFilterContext *ctx) |
187 |
{ |
188 |
MovieContext *movie = ctx->priv; |
189 |
|
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); |
198 |
} |
199 |
|
200 |
static int query_formats(AVFilterContext *ctx) |
201 |
{ |
202 |
MovieContext *movie = ctx->priv; |
203 |
enum PixelFormat pix_fmts[] = { movie->codec_ctx->pix_fmt, PIX_FMT_NONE };
|
204 |
|
205 |
avfilter_set_common_formats(ctx, avfilter_make_format_list(pix_fmts)); |
206 |
return 0; |
207 |
} |
208 |
|
209 |
static int config_output_props(AVFilterLink *outlink) |
210 |
{ |
211 |
MovieContext *movie = outlink->src->priv; |
212 |
|
213 |
outlink->w = movie->w; |
214 |
outlink->h = movie->h; |
215 |
outlink->time_base = movie->format_ctx->streams[movie->stream_index]->time_base; |
216 |
|
217 |
return 0; |
218 |
} |
219 |
|
220 |
static int movie_get_frame(AVFilterLink *outlink) |
221 |
{ |
222 |
MovieContext *movie = outlink->src->priv; |
223 |
AVPacket pkt; |
224 |
int ret, frame_decoded;
|
225 |
AVStream *st = movie->format_ctx->streams[movie->stream_index]; |
226 |
|
227 |
if (movie->is_done == 1) |
228 |
return 0; |
229 |
|
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 |
avcodec_decode_video2(movie->codec_ctx, movie->frame, &frame_decoded, &pkt); |
234 |
|
235 |
if (frame_decoded) {
|
236 |
/* FIXME: avoid the memcpy */
|
237 |
movie->picref = avfilter_get_video_buffer(outlink, AV_PERM_WRITE | AV_PERM_PRESERVE | |
238 |
AV_PERM_REUSE2, outlink->w, outlink->h); |
239 |
av_image_copy(movie->picref->data, movie->picref->linesize, |
240 |
movie->frame->data, movie->frame->linesize, |
241 |
movie->picref->format, outlink->w, outlink->h); |
242 |
|
243 |
/* FIXME: use a PTS correction mechanism as that in
|
244 |
* ffplay.c when some API will be available for that */
|
245 |
/* use pkt_dts if pkt_pts is not available */
|
246 |
movie->picref->pts = movie->frame->pkt_pts == AV_NOPTS_VALUE ? |
247 |
movie->frame->pkt_dts : movie->frame->pkt_pts; |
248 |
|
249 |
movie->picref->pos = movie->frame->pkt_pos; |
250 |
movie->picref->video->pixel_aspect = st->sample_aspect_ratio.num ? |
251 |
st->sample_aspect_ratio : movie->codec_ctx->sample_aspect_ratio; |
252 |
movie->picref->video->interlaced = movie->frame->interlaced_frame; |
253 |
movie->picref->video->top_field_first = movie->frame->top_field_first; |
254 |
movie->picref->video->key_frame = movie->frame->key_frame; |
255 |
movie->picref->video->pict_type = movie->frame->pict_type; |
256 |
av_dlog(outlink->src, |
257 |
"movie_get_frame(): file:'%s' pts:%"PRId64" time:%lf pos:%"PRId64" aspect:%d/%d\n", |
258 |
movie->file_name, movie->picref->pts, |
259 |
(double)movie->picref->pts * av_q2d(st->time_base),
|
260 |
movie->picref->pos, |
261 |
movie->picref->video->pixel_aspect.num, movie->picref->video->pixel_aspect.den); |
262 |
// We got it. Free the packet since we are returning
|
263 |
av_free_packet(&pkt); |
264 |
|
265 |
return 0; |
266 |
} |
267 |
} |
268 |
// Free the packet that was allocated by av_read_frame
|
269 |
av_free_packet(&pkt); |
270 |
} |
271 |
|
272 |
// On multi-frame source we should stop the mixing process when
|
273 |
// the movie source does not have more frames
|
274 |
if (ret == AVERROR_EOF)
|
275 |
movie->is_done = 1;
|
276 |
return ret;
|
277 |
} |
278 |
|
279 |
static int request_frame(AVFilterLink *outlink) |
280 |
{ |
281 |
AVFilterBufferRef *outpicref; |
282 |
MovieContext *movie = outlink->src->priv; |
283 |
int ret;
|
284 |
|
285 |
if (movie->is_done)
|
286 |
return AVERROR_EOF;
|
287 |
if ((ret = movie_get_frame(outlink)) < 0) |
288 |
return ret;
|
289 |
|
290 |
outpicref = avfilter_ref_buffer(movie->picref, ~0);
|
291 |
avfilter_start_frame(outlink, outpicref); |
292 |
avfilter_draw_slice(outlink, 0, outlink->h, 1); |
293 |
avfilter_end_frame(outlink); |
294 |
avfilter_unref_buffer(movie->picref); |
295 |
movie->picref = NULL;
|
296 |
|
297 |
return 0; |
298 |
} |
299 |
|
300 |
AVFilter avfilter_vsrc_movie = { |
301 |
.name = "movie",
|
302 |
.description = NULL_IF_CONFIG_SMALL("Read from a movie source."),
|
303 |
.priv_size = sizeof(MovieContext),
|
304 |
.init = init, |
305 |
.uninit = uninit, |
306 |
.query_formats = query_formats, |
307 |
|
308 |
.inputs = (AVFilterPad[]) {{ .name = NULL }},
|
309 |
.outputs = (AVFilterPad[]) {{ .name = "default",
|
310 |
.type = AVMEDIA_TYPE_VIDEO, |
311 |
.request_frame = request_frame, |
312 |
.config_props = config_output_props, }, |
313 |
{ .name = NULL}},
|
314 |
}; |