h264_metadata: Add option to delete filler data
[libav.git] / libavfilter / vsrc_movie.c
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
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 #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
39 #include "libavcodec/avcodec.h"
40
41 #include "libavformat/avformat.h"
42
43 #include "avfilter.h"
44 #include "formats.h"
45 #include "internal.h"
46 #include "video.h"
47
48 typedef struct MovieContext {
49 const AVClass *class;
50 int64_t seek_point; ///< seekpoint in microseconds
51 double seek_point_d;
52 char *format_name;
53 char *file_name;
54 int stream_index;
55
56 AVFormatContext *format_ctx;
57 AVCodecContext *codec_ctx;
58 int is_done;
59 AVFrame *frame; ///< video frame to store the decoded images in
60
61 int w, h;
62 } MovieContext;
63
64 #define OFFSET(x) offsetof(MovieContext, x)
65 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM
66
67 static const AVOption movie_options[]= {
68 { "filename", NULL, OFFSET(file_name), AV_OPT_TYPE_STRING, .flags = FLAGS },
69 { "format_name", "set format name", OFFSET(format_name), AV_OPT_TYPE_STRING, .flags = FLAGS },
70 { "f", "set format name", OFFSET(format_name), AV_OPT_TYPE_STRING, .flags = FLAGS },
71 { "stream_index", "set stream index", OFFSET(stream_index), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, FLAGS },
72 { "si", "set stream index", OFFSET(stream_index), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, FLAGS },
73 { "seek_point", "set seekpoint (seconds)", OFFSET(seek_point_d), AV_OPT_TYPE_DOUBLE, { .dbl = 0 }, 0, (INT64_MAX-1) / 1000000, FLAGS },
74 { "sp", "set seekpoint (seconds)", OFFSET(seek_point_d), AV_OPT_TYPE_DOUBLE, { .dbl = 0 }, 0, (INT64_MAX-1) / 1000000, FLAGS },
75 { NULL },
76 };
77
78 static const char *movie_get_name(void *ctx)
79 {
80 return "movie";
81 }
82
83 static const AVClass movie_class = {
84 .class_name = "MovieContext",
85 .item_name = movie_get_name,
86 .option = movie_options,
87 };
88
89 static av_cold int movie_init(AVFilterContext *ctx)
90 {
91 MovieContext *movie = ctx->priv;
92 AVInputFormat *iformat = NULL;
93 AVStream *st;
94 AVCodec *codec;
95 int ret;
96 int64_t timestamp;
97
98 av_register_all();
99
100 // Try to find the movie format (container)
101 iformat = movie->format_name ? av_find_input_format(movie->format_name) : NULL;
102
103 movie->format_ctx = NULL;
104 if ((ret = avformat_open_input(&movie->format_ctx, movie->file_name, iformat, NULL)) < 0) {
105 av_log(ctx, AV_LOG_ERROR,
106 "Failed to avformat_open_input '%s'\n", movie->file_name);
107 return ret;
108 }
109 if ((ret = avformat_find_stream_info(movie->format_ctx, NULL)) < 0)
110 av_log(ctx, AV_LOG_WARNING, "Failed to find stream info\n");
111
112 // if seeking requested, we execute it
113 if (movie->seek_point > 0) {
114 timestamp = movie->seek_point;
115 // add the stream start time, should it exist
116 if (movie->format_ctx->start_time != AV_NOPTS_VALUE) {
117 if (timestamp > INT64_MAX - movie->format_ctx->start_time) {
118 av_log(ctx, AV_LOG_ERROR,
119 "%s: seek value overflow with start_time:%"PRId64" seek_point:%"PRId64"\n",
120 movie->file_name, movie->format_ctx->start_time, movie->seek_point);
121 return AVERROR(EINVAL);
122 }
123 timestamp += movie->format_ctx->start_time;
124 }
125 if ((ret = av_seek_frame(movie->format_ctx, -1, timestamp, AVSEEK_FLAG_BACKWARD)) < 0) {
126 av_log(ctx, AV_LOG_ERROR, "%s: could not seek to position %"PRId64"\n",
127 movie->file_name, timestamp);
128 return ret;
129 }
130 }
131
132 /* select the video stream */
133 if ((ret = av_find_best_stream(movie->format_ctx, AVMEDIA_TYPE_VIDEO,
134 movie->stream_index, -1, NULL, 0)) < 0) {
135 av_log(ctx, AV_LOG_ERROR, "No video stream with index '%d' found\n",
136 movie->stream_index);
137 return ret;
138 }
139 movie->stream_index = ret;
140 st = movie->format_ctx->streams[movie->stream_index];
141
142 /*
143 * So now we've got a pointer to the so-called codec context for our video
144 * stream, but we still have to find the actual codec and open it.
145 */
146 codec = avcodec_find_decoder(st->codecpar->codec_id);
147 if (!codec) {
148 av_log(ctx, AV_LOG_ERROR, "Failed to find any codec\n");
149 return AVERROR(EINVAL);
150 }
151
152 movie->codec_ctx = avcodec_alloc_context3(codec);
153 if (!movie->codec_ctx)
154 return AVERROR(ENOMEM);
155
156 ret = avcodec_parameters_to_context(movie->codec_ctx, st->codecpar);
157 if (ret < 0)
158 return ret;
159
160 movie->codec_ctx->refcounted_frames = 1;
161
162 if ((ret = avcodec_open2(movie->codec_ctx, codec, NULL)) < 0) {
163 av_log(ctx, AV_LOG_ERROR, "Failed to open codec\n");
164 return ret;
165 }
166
167 movie->w = movie->codec_ctx->width;
168 movie->h = movie->codec_ctx->height;
169
170 av_log(ctx, AV_LOG_VERBOSE, "seek_point:%"PRIi64" format_name:%s file_name:%s stream_index:%d\n",
171 movie->seek_point, movie->format_name, movie->file_name,
172 movie->stream_index);
173
174 return 0;
175 }
176
177 static av_cold int init(AVFilterContext *ctx)
178 {
179 MovieContext *movie = ctx->priv;
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 avcodec_free_context(&movie->codec_ctx);
191 if (movie->format_ctx)
192 avformat_close_input(&movie->format_ctx);
193 av_frame_free(&movie->frame);
194 }
195
196 static int query_formats(AVFilterContext *ctx)
197 {
198 MovieContext *movie = ctx->priv;
199 enum AVPixelFormat pix_fmts[] = { movie->codec_ctx->pix_fmt, AV_PIX_FMT_NONE };
200
201 ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
202 return 0;
203 }
204
205 static int config_output_props(AVFilterLink *outlink)
206 {
207 MovieContext *movie = outlink->src->priv;
208
209 outlink->w = movie->w;
210 outlink->h = movie->h;
211 outlink->time_base = movie->format_ctx->streams[movie->stream_index]->time_base;
212
213 return 0;
214 }
215
216 static int movie_get_frame(AVFilterLink *outlink)
217 {
218 MovieContext *movie = outlink->src->priv;
219 AVPacket pkt;
220 int ret, frame_decoded;
221
222 if (movie->is_done == 1)
223 return 0;
224
225 movie->frame = av_frame_alloc();
226 if (!movie->frame)
227 return AVERROR(ENOMEM);
228
229 while ((ret = av_read_frame(movie->format_ctx, &pkt)) >= 0) {
230 // Is this a packet from the video stream?
231 if (pkt.stream_index == movie->stream_index) {
232 avcodec_decode_video2(movie->codec_ctx, movie->frame, &frame_decoded, &pkt);
233
234 if (frame_decoded) {
235 av_log(outlink->src, AV_LOG_TRACE,
236 "movie_get_frame(): file:'%s' pts:%"PRId64" time:%f aspect:%d/%d\n",
237 movie->file_name, movie->frame->pts,
238 (double)movie->frame->pts *
239 av_q2d(movie->format_ctx->streams[movie->stream_index]->time_base),
240 movie->frame->sample_aspect_ratio.num,
241 movie->frame->sample_aspect_ratio.den);
242 // We got it. Free the packet since we are returning
243 av_packet_unref(&pkt);
244
245 return 0;
246 }
247 }
248 // Free the packet that was allocated by av_read_frame
249 av_packet_unref(&pkt);
250 }
251
252 // On multi-frame source we should stop the mixing process when
253 // the movie source does not have more frames
254 if (ret == AVERROR_EOF)
255 movie->is_done = 1;
256 return ret;
257 }
258
259 static int request_frame(AVFilterLink *outlink)
260 {
261 MovieContext *movie = outlink->src->priv;
262 int ret;
263
264 if (movie->is_done)
265 return AVERROR_EOF;
266 if ((ret = movie_get_frame(outlink)) < 0)
267 return ret;
268
269 ret = ff_filter_frame(outlink, movie->frame);
270 movie->frame = NULL;
271
272 return ret;
273 }
274
275 static const AVFilterPad avfilter_vsrc_movie_outputs[] = {
276 {
277 .name = "default",
278 .type = AVMEDIA_TYPE_VIDEO,
279 .request_frame = request_frame,
280 .config_props = config_output_props,
281 },
282 { NULL }
283 };
284
285 AVFilter ff_vsrc_movie = {
286 .name = "movie",
287 .description = NULL_IF_CONFIG_SMALL("Read from a movie source."),
288 .priv_size = sizeof(MovieContext),
289 .priv_class = &movie_class,
290 .init = init,
291 .uninit = uninit,
292 .query_formats = query_formats,
293
294 .inputs = NULL,
295 .outputs = avfilter_vsrc_movie_outputs,
296 };