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 };