vsrc_movie.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010 Stefano Sabatini
3  * Copyright (c) 2008 Victor Paesa
4  *
5  * This file is part of Libav.
6  *
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.
11  *
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.
16  *
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
20  */
21 
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 #include "formats.h"
40 #include "internal.h"
41 #include "video.h"
42 
43 typedef struct {
44  const AVClass *class;
45  int64_t seek_point;
46  double seek_point_d;
47  char *format_name;
48  char *file_name;
50 
53  int is_done;
55 
56  int w, h;
58 } MovieContext;
59 
60 #define OFFSET(x) offsetof(MovieContext, x)
61 
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, {.i64 = -1}, -1, INT_MAX },
66 {"si", "set stream index", OFFSET(stream_index), AV_OPT_TYPE_INT, {.i64 = -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 },
69 {NULL},
70 };
71 
72 static const char *movie_get_name(void *ctx)
73 {
74  return "movie";
75 }
76 
77 static const AVClass movie_class = {
78  "MovieContext",
80  movie_options
81 };
82 
83 static int movie_init(AVFilterContext *ctx)
84 {
85  MovieContext *movie = ctx->priv;
87  AVCodec *codec;
88  int ret;
89  int64_t timestamp;
90 
92 
93  // Try to find the movie format (container)
94  iformat = movie->format_name ? av_find_input_format(movie->format_name) : NULL;
95 
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);
100  return ret;
101  }
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");
104 
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);
115  }
116  timestamp += movie->format_ctx->start_time;
117  }
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);
121  return ret;
122  }
123  }
124 
125  /* select the video stream */
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);
130  return ret;
131  }
132  movie->stream_index = ret;
133  movie->codec_ctx = movie->format_ctx->streams[movie->stream_index]->codec;
134 
135  /*
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.
138  */
139  codec = avcodec_find_decoder(movie->codec_ctx->codec_id);
140  if (!codec) {
141  av_log(ctx, AV_LOG_ERROR, "Failed to find any codec\n");
142  return AVERROR(EINVAL);
143  }
144 
145  if ((ret = avcodec_open2(movie->codec_ctx, codec, NULL)) < 0) {
146  av_log(ctx, AV_LOG_ERROR, "Failed to open codec\n");
147  return ret;
148  }
149 
150  if (!(movie->frame = avcodec_alloc_frame()) ) {
151  av_log(ctx, AV_LOG_ERROR, "Failed to alloc frame\n");
152  return AVERROR(ENOMEM);
153  }
154 
155  movie->w = movie->codec_ctx->width;
156  movie->h = movie->codec_ctx->height;
157 
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);
161 
162  return 0;
163 }
164 
165 static av_cold int init(AVFilterContext *ctx, const char *args)
166 {
167  MovieContext *movie = ctx->priv;
168  int ret;
169  movie->class = &movie_class;
170  av_opt_set_defaults(movie);
171 
172  if (args)
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);
177  }
178 
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);
181  return ret;
182  }
183 
184  movie->seek_point = movie->seek_point_d * 1000000 + 0.5;
185 
186  return movie_init(ctx);
187 }
188 
189 static av_cold void uninit(AVFilterContext *ctx)
190 {
191  MovieContext *movie = ctx->priv;
192 
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)
200  avcodec_free_frame(&movie->frame);
201 }
202 
204 {
205  MovieContext *movie = ctx->priv;
206  enum AVPixelFormat pix_fmts[] = { movie->codec_ctx->pix_fmt, AV_PIX_FMT_NONE };
207 
209  return 0;
210 }
211 
212 static int config_output_props(AVFilterLink *outlink)
213 {
214  MovieContext *movie = outlink->src->priv;
215 
216  outlink->w = movie->w;
217  outlink->h = movie->h;
218  outlink->time_base = movie->format_ctx->streams[movie->stream_index]->time_base;
219 
220  return 0;
221 }
222 
223 static int movie_get_frame(AVFilterLink *outlink)
224 {
225  MovieContext *movie = outlink->src->priv;
226  AVPacket pkt;
227  int ret, frame_decoded;
228  AVStream *st = movie->format_ctx->streams[movie->stream_index];
229 
230  if (movie->is_done == 1)
231  return 0;
232 
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);
238 
239  if (frame_decoded) {
240  /* FIXME: avoid the memcpy */
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);
247 
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;
253 
254  movie->picref->pos = movie->frame->reordered_opaque;
255  if (!movie->frame->sample_aspect_ratio.num)
257  av_dlog(outlink->src,
258  "movie_get_frame(): file:'%s' pts:%"PRId64" time:%f pos:%"PRId64" aspect:%d/%d\n",
259  movie->file_name, movie->picref->pts,
260  (double)movie->picref->pts * av_q2d(st->time_base),
261  movie->picref->pos,
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);
265 
266  return 0;
267  }
268  }
269  // Free the packet that was allocated by av_read_frame
270  av_free_packet(&pkt);
271  }
272 
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)
276  movie->is_done = 1;
277  return ret;
278 }
279 
280 static int request_frame(AVFilterLink *outlink)
281 {
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  ret = ff_filter_frame(outlink, movie->picref);
291  movie->picref = NULL;
292 
293  return ret;
294 }
295 
297  {
298  .name = "default",
299  .type = AVMEDIA_TYPE_VIDEO,
300  .request_frame = request_frame,
301  .config_props = config_output_props,
302  },
303  { NULL }
304 };
305 
307  .name = "movie",
308  .description = NULL_IF_CONFIG_SMALL("Read from a movie source."),
309  .priv_size = sizeof(MovieContext),
310  .init = init,
311  .uninit = uninit,
313 
314  .inputs = NULL,
315  .outputs = avfilter_vsrc_movie_outputs,
316 };
void av_free_packet(AVPacket *pkt)
Free a packet.
Definition: avpacket.c:153
This structure describes decoded (raw) audio or video data.
Definition: avcodec.h:989
AVOption.
Definition: opt.h:233
AVFilterBufferRefVideoProps * video
video buffer specific properties
Definition: avfilter.h:159
int linesize[8]
number of bytes per line
Definition: avfilter.h:157
misc image utilities
static const AVFilterPad outputs[]
Definition: af_ashowinfo.c:122
int64_t pos
byte position in stream, -1 if unknown
Definition: avcodec.h:940
int avformat_open_input(AVFormatContext **ps, const char *filename, AVInputFormat *fmt, AVDictionary **options)
Open an input stream and read the header.
Definition: utils.c:476
void av_opt_set_defaults(void *s)
Set the values of all AVOption fields to their default values.
Definition: opt.c:505
int ff_filter_frame(AVFilterLink *link, AVFilterBufferRef *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:459
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown)
Definition: avformat.h:697
int num
numerator
Definition: rational.h:44
int av_set_options_string(void *ctx, const char *opts, const char *key_val_sep, const char *pairs_sep)
Parse the key/value pairs list in opts.
Definition: opt.c:587
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1533
av_dlog(ac->avr,"%d samples - audio_convert: %s to %s (%s)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt), use_generic?ac->func_descr_generic:ac->func_descr)
static int config_output_props(AVFilterLink *outlink)
Definition: vsrc_movie.c:212
AVCodec.
Definition: avcodec.h:2960
char * file_name
Definition: vsrc_movie.c:48
void avfilter_unref_buffer(AVFilterBufferRef *ref)
Remove a reference to a buffer.
Definition: buffer.c:75
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:165
Format I/O context.
Definition: avformat.h:828
double seek_point_d
Definition: vsrc_movie.c:46
const char * name
Pad name.
Definition: internal.h:39
AVOptions.
static int request_frame(AVFilterLink *outlink)
Definition: vsrc_movie.c:280
AVStream ** streams
Definition: avformat.h:876
static double av_q2d(AVRational a)
Convert rational to double.
Definition: rational.h:69
AVFrame * frame
video frame to store the decoded images in
Definition: vsrc_movie.c:54
void ff_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
A helper for query_formats() which sets all links to the same list of formats.
Definition: formats.c:375
AVRational pixel_aspect
pixel aspect ratio
Definition: avfilter.h:124
int64_t pts
presentation timestamp.
Definition: avfilter.h:167
A filter pad used for either input or output.
Definition: internal.h:33
int av_find_best_stream(AVFormatContext *ic, enum AVMediaType type, int wanted_stream_nb, int related_stream, AVCodec **decoder_ret, int flags)
Find the "best" stream in the file.
Definition: utils.c:2589
int avcodec_close(AVCodecContext *avctx)
Close a given AVCodecContext and free all the data associated with it (but not the AVCodecContext its...
Definition: utils.c:1437
AVFilterBufferRef * ff_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition: video.c:145
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:139
static const AVClass movie_class
Definition: vsrc_movie.c:77
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:88
void * priv
private data for use by the filter
Definition: avfilter.h:439
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:146
static int movie_init(AVFilterContext *ctx)
Definition: vsrc_movie.c:83
void av_image_copy(uint8_t *dst_data[4], int dst_linesizes[4], const uint8_t *src_data[4], const int src_linesizes[4], enum AVPixelFormat pix_fmt, int width, int height)
Copy image in src_data to dst_data.
Definition: imgutils.c:244
char * av_get_token(const char **buf, const char *term)
Unescape the given string until a non escaped terminating char, and return the token corresponding to...
Definition: avstring.c:106
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:641
AVFrame * avcodec_alloc_frame(void)
Allocate an AVFrame and set its fields to default values.
Definition: utils.c:618
AVInputFormat * av_find_input_format(const char *short_name)
Find AVInputFormat based on the short name of the input format.
Definition: utils.c:207
int width
picture width / height.
Definition: avcodec.h:1508
static const AVFilterPad avfilter_vsrc_movie_outputs[]
Definition: vsrc_movie.c:296
static av_cold void uninit(AVFilterContext *ctx)
Definition: vsrc_movie.c:189
char * format_name
Definition: vsrc_movie.c:47
int64_t reordered_opaque
opaque 64bit number (generally a PTS) that will be reordered and output in AVFrame.reordered_opaque
Definition: avcodec.h:2615
int avfilter_copy_frame_props(AVFilterBufferRef *dst, const AVFrame *src)
Copy the frame properties of src to dst, without copying the actual image data.
Definition: buffer.c:94
static int query_formats(AVFilterContext *ctx)
Definition: vsrc_movie.c:203
Stream structure.
Definition: avformat.h:622
A reference to an AVFilterBuffer.
Definition: avfilter.h:139
NULL
Definition: eval.c:52
enum AVCodecID codec_id
Definition: avcodec.h:1350
int linesize[AV_NUM_DATA_POINTERS]
Size, in bytes, of the data for each picture/channel plane.
Definition: avcodec.h:1008
main external API structure.
Definition: avcodec.h:1339
AVCodec * avcodec_find_decoder(enum AVCodecID id)
Find a registered decoder with a matching codec ID.
Definition: utils.c:1515
AVRational sample_aspect_ratio
sample aspect ratio for the video frame, 0/1 if unknown/unspecified
Definition: avcodec.h:1080
#define OFFSET(x)
Definition: vsrc_movie.c:60
AVCodecContext * codec_ctx
Definition: vsrc_movie.c:52
int64_t reordered_opaque
reordered opaque 64bit (generally an integer or a double precision float PTS but can be anything)...
Definition: avcodec.h:1273
Describe the class of an AVClass context structure.
Definition: log.h:33
Filter definition.
Definition: avfilter.h:371
static const AVFilterPad inputs[]
Definition: af_ashowinfo.c:110
int64_t seek_point
seekpoint in microseconds
Definition: vsrc_movie.c:45
static av_cold int init(AVFilterContext *ctx, const char *args)
Definition: vsrc_movie.c:165
const char * name
filter name
Definition: avfilter.h:372
int avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
Initialize the AVCodecContext to use the given AVCodec.
Definition: utils.c:645
int av_read_frame(AVFormatContext *s, AVPacket *pkt)
Return the next frame of a stream.
Definition: utils.c:1206
int64_t pkt_pts
pts copied from the AVPacket that was decoded to produce this frame
Definition: avcodec.h:1095
#define AV_PERM_PRESERVE
nobody else can overwrite the buffer
Definition: avfilter.h:99
int64_t start_time
Decoding: position of the first frame of the component, in AV_TIME_BASE fractional seconds...
Definition: avformat.h:885
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: avcodec.h:997
int stream_index
Definition: vsrc_movie.c:49
int av_seek_frame(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
Seek to the keyframe at timestamp.
Definition: utils.c:1730
AVFilter avfilter_vsrc_movie
Definition: vsrc_movie.c:306
static const char * movie_get_name(void *ctx)
Definition: vsrc_movie.c:72
int64_t pkt_dts
dts copied from the AVPacket that triggered returning this frame
Definition: avcodec.h:1102
Main libavformat public API header.
static AVInputFormat * iformat
Definition: avprobe.c:52
int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options)
Read packets of a media file to get stream information.
Definition: utils.c:2236
int den
denominator
Definition: rational.h:45
void avformat_close_input(AVFormatContext **s)
Close an opened input AVFormatContext.
Definition: utils.c:2713
#define AV_PERM_REUSE2
can output the buffer multiple times, modified each time
Definition: avfilter.h:101
#define AV_PERM_WRITE
can write to the buffer
Definition: avfilter.h:98
int64_t pos
byte position in stream, -1 if unknown
Definition: avfilter.h:168
static const AVOption movie_options[]
Definition: vsrc_movie.c:62
uint8_t * data[8]
picture/audio data for each plane
Definition: avfilter.h:141
int format
media format
Definition: avfilter.h:170
AVFormatContext * format_ctx
Definition: vsrc_movie.c:51
int avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture, int *got_picture_ptr, AVPacket *avpkt)
Decode the video frame of size avpkt->size from avpkt->data into picture.
Definition: utils.c:1268
void avcodec_free_frame(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: utils.c:630
An instance of a filter.
Definition: avfilter.h:418
const AVClass * class
Definition: vsrc_movie.c:44
AVFilterBufferRef * picref
Definition: vsrc_movie.c:57
int stream_index
Definition: avcodec.h:917
internal API functions
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avformat.h:669
AVPixelFormat
Pixel format.
Definition: pixfmt.h:63
This structure stores compressed data.
Definition: avcodec.h:898
void av_register_all(void)
Initialize libavformat and register all the muxers, demuxers and protocols.
Definition: allformats.c:52
static int movie_get_frame(AVFilterLink *outlink)
Definition: vsrc_movie.c:223