Libav
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 #include <float.h>
32 #include <stdint.h>
33 
34 #include "libavutil/attributes.h"
35 #include "libavutil/avstring.h"
36 #include "libavutil/opt.h"
37 #include "libavutil/imgutils.h"
38 #include "libavformat/avformat.h"
39 #include "avfilter.h"
40 #include "formats.h"
41 #include "internal.h"
42 #include "video.h"
43 
44 typedef struct MovieContext {
45  const AVClass *class;
46  int64_t seek_point;
47  double seek_point_d;
48  char *format_name;
49  char *file_name;
51 
54  int is_done;
56 
57  int w, h;
58 } MovieContext;
59 
60 #define OFFSET(x) offsetof(MovieContext, x)
61 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM
62 
63 static const AVOption movie_options[]= {
64  { "filename", NULL, OFFSET(file_name), AV_OPT_TYPE_STRING, .flags = FLAGS },
65  { "format_name", "set format name", OFFSET(format_name), AV_OPT_TYPE_STRING, .flags = FLAGS },
66  { "f", "set format name", OFFSET(format_name), AV_OPT_TYPE_STRING, .flags = FLAGS },
67  { "stream_index", "set stream index", OFFSET(stream_index), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, FLAGS },
68  { "si", "set stream index", OFFSET(stream_index), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, FLAGS },
69  { "seek_point", "set seekpoint (seconds)", OFFSET(seek_point_d), AV_OPT_TYPE_DOUBLE, { .dbl = 0 }, 0, (INT64_MAX-1) / 1000000, FLAGS },
70  { "sp", "set seekpoint (seconds)", OFFSET(seek_point_d), AV_OPT_TYPE_DOUBLE, { .dbl = 0 }, 0, (INT64_MAX-1) / 1000000, FLAGS },
71  { NULL },
72 };
73 
74 static const char *movie_get_name(void *ctx)
75 {
76  return "movie";
77 }
78 
79 static const AVClass movie_class = {
80  "MovieContext",
82  movie_options
83 };
84 
86 {
87  MovieContext *movie = ctx->priv;
89  AVStream *st;
90  AVCodec *codec;
91  int ret;
92  int64_t timestamp;
93 
95 
96  // Try to find the movie format (container)
97  iformat = movie->format_name ? av_find_input_format(movie->format_name) : NULL;
98 
99  movie->format_ctx = NULL;
100  if ((ret = avformat_open_input(&movie->format_ctx, movie->file_name, iformat, NULL)) < 0) {
101  av_log(ctx, AV_LOG_ERROR,
102  "Failed to avformat_open_input '%s'\n", movie->file_name);
103  return ret;
104  }
105  if ((ret = avformat_find_stream_info(movie->format_ctx, NULL)) < 0)
106  av_log(ctx, AV_LOG_WARNING, "Failed to find stream info\n");
107 
108  // if seeking requested, we execute it
109  if (movie->seek_point > 0) {
110  timestamp = movie->seek_point;
111  // add the stream start time, should it exist
112  if (movie->format_ctx->start_time != AV_NOPTS_VALUE) {
113  if (timestamp > INT64_MAX - movie->format_ctx->start_time) {
114  av_log(ctx, AV_LOG_ERROR,
115  "%s: seek value overflow with start_time:%"PRId64" seek_point:%"PRId64"\n",
116  movie->file_name, movie->format_ctx->start_time, movie->seek_point);
117  return AVERROR(EINVAL);
118  }
119  timestamp += movie->format_ctx->start_time;
120  }
121  if ((ret = av_seek_frame(movie->format_ctx, -1, timestamp, AVSEEK_FLAG_BACKWARD)) < 0) {
122  av_log(ctx, AV_LOG_ERROR, "%s: could not seek to position %"PRId64"\n",
123  movie->file_name, timestamp);
124  return ret;
125  }
126  }
127 
128  /* select the video stream */
130  movie->stream_index, -1, NULL, 0)) < 0) {
131  av_log(ctx, AV_LOG_ERROR, "No video stream with index '%d' found\n",
132  movie->stream_index);
133  return ret;
134  }
135  movie->stream_index = ret;
136  st = movie->format_ctx->streams[movie->stream_index];
137 
138  /*
139  * So now we've got a pointer to the so-called codec context for our video
140  * stream, but we still have to find the actual codec and open it.
141  */
142  codec = avcodec_find_decoder(st->codecpar->codec_id);
143  if (!codec) {
144  av_log(ctx, AV_LOG_ERROR, "Failed to find any codec\n");
145  return AVERROR(EINVAL);
146  }
147 
148  movie->codec_ctx = avcodec_alloc_context3(codec);
149  if (!movie->codec_ctx)
150  return AVERROR(ENOMEM);
151 
153  if (ret < 0)
154  return ret;
155 
156  movie->codec_ctx->refcounted_frames = 1;
157 
158  if ((ret = avcodec_open2(movie->codec_ctx, codec, NULL)) < 0) {
159  av_log(ctx, AV_LOG_ERROR, "Failed to open codec\n");
160  return ret;
161  }
162 
163  movie->w = movie->codec_ctx->width;
164  movie->h = movie->codec_ctx->height;
165 
166  av_log(ctx, AV_LOG_VERBOSE, "seek_point:%"PRIi64" format_name:%s file_name:%s stream_index:%d\n",
167  movie->seek_point, movie->format_name, movie->file_name,
168  movie->stream_index);
169 
170  return 0;
171 }
172 
174 {
175  MovieContext *movie = ctx->priv;
176 
177  movie->seek_point = movie->seek_point_d * 1000000 + 0.5;
178 
179  return movie_init(ctx);
180 }
181 
183 {
184  MovieContext *movie = ctx->priv;
185 
187  if (movie->format_ctx)
189  av_frame_free(&movie->frame);
190 }
191 
193 {
194  MovieContext *movie = ctx->priv;
196 
198  return 0;
199 }
200 
201 static int config_output_props(AVFilterLink *outlink)
202 {
203  MovieContext *movie = outlink->src->priv;
204 
205  outlink->w = movie->w;
206  outlink->h = movie->h;
207  outlink->time_base = movie->format_ctx->streams[movie->stream_index]->time_base;
208 
209  return 0;
210 }
211 
212 static int movie_get_frame(AVFilterLink *outlink)
213 {
214  MovieContext *movie = outlink->src->priv;
215  AVPacket pkt;
216  int ret, frame_decoded;
217 
218  if (movie->is_done == 1)
219  return 0;
220 
221  movie->frame = av_frame_alloc();
222  if (!movie->frame)
223  return AVERROR(ENOMEM);
224 
225  while ((ret = av_read_frame(movie->format_ctx, &pkt)) >= 0) {
226  // Is this a packet from the video stream?
227  if (pkt.stream_index == movie->stream_index) {
228  avcodec_decode_video2(movie->codec_ctx, movie->frame, &frame_decoded, &pkt);
229 
230  if (frame_decoded) {
231  av_log(outlink->src, AV_LOG_TRACE,
232  "movie_get_frame(): file:'%s' pts:%"PRId64" time:%f aspect:%d/%d\n",
233  movie->file_name, movie->frame->pts,
234  (double)movie->frame->pts *
235  av_q2d(movie->format_ctx->streams[movie->stream_index]->time_base),
236  movie->frame->sample_aspect_ratio.num,
237  movie->frame->sample_aspect_ratio.den);
238  // We got it. Free the packet since we are returning
239  av_packet_unref(&pkt);
240 
241  return 0;
242  }
243  }
244  // Free the packet that was allocated by av_read_frame
245  av_packet_unref(&pkt);
246  }
247 
248  // On multi-frame source we should stop the mixing process when
249  // the movie source does not have more frames
250  if (ret == AVERROR_EOF)
251  movie->is_done = 1;
252  return ret;
253 }
254 
255 static int request_frame(AVFilterLink *outlink)
256 {
257  MovieContext *movie = outlink->src->priv;
258  int ret;
259 
260  if (movie->is_done)
261  return AVERROR_EOF;
262  if ((ret = movie_get_frame(outlink)) < 0)
263  return ret;
264 
265  ret = ff_filter_frame(outlink, movie->frame);
266  movie->frame = NULL;
267 
268  return ret;
269 }
270 
272  {
273  .name = "default",
274  .type = AVMEDIA_TYPE_VIDEO,
275  .request_frame = request_frame,
276  .config_props = config_output_props,
277  },
278  { NULL }
279 };
280 
282  .name = "movie",
283  .description = NULL_IF_CONFIG_SMALL("Read from a movie source."),
284  .priv_size = sizeof(MovieContext),
285  .priv_class = &movie_class,
286  .init = init,
287  .uninit = uninit,
289 
290  .inputs = NULL,
291  .outputs = avfilter_vsrc_movie_outputs,
292 };
#define AVSEEK_FLAG_BACKWARD
Definition: avformat.h:1657
This structure describes decoded (raw) audio or video data.
Definition: frame.h:140
AVOption.
Definition: opt.h:234
misc image utilities
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:130
static const AVFilterPad outputs[]
Definition: af_ashowinfo.c:259
Main libavfilter public API header.
int avformat_open_input(AVFormatContext **ps, const char *filename, AVInputFormat *fmt, AVDictionary **options)
Open an input stream and read the header.
Definition: utils.c:284
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:3483
int num
numerator
Definition: rational.h:44
av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (%s)\, 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)
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1621
static int config_output_props(AVFilterLink *outlink)
Definition: vsrc_movie.c:201
AVCodec.
Definition: avcodec.h:3120
char * file_name
Definition: vsrc_movie.c:49
Macro definitions for various function/variable attributes.
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:940
double seek_point_d
Definition: vsrc_movie.c:47
const char * name
Pad name.
Definition: internal.h:41
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:747
#define av_cold
Definition: attributes.h:66
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:68
AVOptions.
#define AV_LOG_TRACE
Extremely verbose debugging, useful for libav* development.
Definition: log.h:150
static int request_frame(AVFilterLink *outlink)
Definition: vsrc_movie.c:255
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:211
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1008
int avcodec_parameters_to_context(AVCodecContext *codec, const AVCodecParameters *par)
Fill the codec context based on the values from the supplied codec parameters.
Definition: utils.c:2885
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:55
#define AVERROR_EOF
End of file.
Definition: error.h:51
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:140
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:389
A filter pad used for either input or output.
Definition: internal.h:35
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:2482
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:124
static const AVClass movie_class
Definition: vsrc_movie.c:79
#define AVERROR(e)
Definition: error.h:43
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:80
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:148
void * priv
private data for use by the filter
Definition: avfilter.h:277
static av_cold int movie_init(AVFilterContext *ctx)
Definition: vsrc_movie.c:85
AVInputFormat * av_find_input_format(const char *short_name)
Find AVInputFormat based on the short name of the input format.
Definition: format.c:162
AVCodecContext * avcodec_alloc_context3(const AVCodec *codec)
Allocate an AVCodecContext and set its fields to default values.
Definition: options.c:136
int width
picture width / height.
Definition: avcodec.h:1580
AVFormatContext * ctx
Definition: movenc.c:48
static const AVFilterPad avfilter_vsrc_movie_outputs[]
Definition: vsrc_movie.c:271
AVFilter ff_vsrc_movie
Definition: vsrc_movie.c:281
static av_cold void uninit(AVFilterContext *ctx)
Definition: vsrc_movie.c:182
char * format_name
Definition: vsrc_movie.c:48
int refcounted_frames
If non-zero, the decoded audio and video frames returned from avcodec_decode_video2() and avcodec_dec...
Definition: avcodec.h:2319
static int query_formats(AVFilterContext *ctx)
Definition: vsrc_movie.c:192
Stream structure.
Definition: avformat.h:705
NULL
Definition: eval.c:55
void avcodec_free_context(AVCodecContext **avctx)
Free the codec context and everything associated with it and write NULL to the provided pointer...
Definition: options.c:151
main external API structure.
Definition: avcodec.h:1409
AVCodec * avcodec_find_decoder(enum AVCodecID id)
Find a registered decoder with a matching codec ID.
Definition: utils.c:2029
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:347
AVRational sample_aspect_ratio
Sample aspect ratio for the video frame, 0/1 if unknown/unspecified.
Definition: frame.h:206
#define OFFSET(x)
Definition: vsrc_movie.c:60
AVCodecContext * codec_ctx
Definition: vsrc_movie.c:53
Describe the class of an AVClass context structure.
Definition: log.h:34
Filter definition.
Definition: avfilter.h:120
static const AVFilterPad inputs[]
Definition: af_ashowinfo.c:248
int64_t seek_point
seekpoint in microseconds
Definition: vsrc_movie.c:46
const char * name
Filter name.
Definition: avfilter.h:124
int avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
Initialize the AVCodecContext to use the given AVCodec.
Definition: utils.c:850
int av_read_frame(AVFormatContext *s, AVPacket *pkt)
Return the next frame of a stream.
Definition: utils.c:1021
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:257
int64_t start_time
Position of the first frame of the component, in AV_TIME_BASE fractional seconds. ...
Definition: avformat.h:1025
int stream_index
Definition: vsrc_movie.c:50
int av_seek_frame(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
Seek to the keyframe at timestamp.
Definition: utils.c:1564
static const char * movie_get_name(void *ctx)
Definition: vsrc_movie.c:74
Main libavformat public API header.
static AVInputFormat * iformat
Definition: avprobe.c:68
int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options)
Read packets of a media file to get stream information.
Definition: utils.c:2092
#define FLAGS
Definition: vsrc_movie.c:61
int den
denominator
Definition: rational.h:45
void avformat_close_input(AVFormatContext **s)
Close an opened input AVFormatContext.
Definition: utils.c:2626
static const AVOption movie_options[]
Definition: vsrc_movie.c:63
AVFormatContext * format_ctx
Definition: vsrc_movie.c:52
An instance of a filter.
Definition: avfilter.h:262
AVCodecParameters * codecpar
Definition: avformat.h:831
int stream_index
Definition: avcodec.h:1348
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:742
attribute_deprecated 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:1559
static av_cold int init(AVFilterContext *ctx)
Definition: vsrc_movie.c:173
AVPixelFormat
Pixel format.
Definition: pixfmt.h:57
This structure stores compressed data.
Definition: avcodec.h:1323
void av_register_all(void)
Initialize libavformat and register all the muxers, demuxers and protocols.
Definition: allformats.c:44
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:235
static int movie_get_frame(AVFilterLink *outlink)
Definition: vsrc_movie.c:212