00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00031
00032
00033 #include <float.h>
00034 #include "libavutil/avstring.h"
00035 #include "libavutil/opt.h"
00036 #include "libavutil/imgutils.h"
00037 #include "libavformat/avformat.h"
00038 #include "avfilter.h"
00039
00040 typedef struct {
00041 const AVClass *class;
00042 int64_t seek_point;
00043 double seek_point_d;
00044 char *format_name;
00045 char *file_name;
00046 int stream_index;
00047
00048 AVFormatContext *format_ctx;
00049 AVCodecContext *codec_ctx;
00050 int is_done;
00051 AVFrame *frame;
00052
00053 int w, h;
00054 AVFilterBufferRef *picref;
00055 } MovieContext;
00056
00057 #define OFFSET(x) offsetof(MovieContext, x)
00058
00059 static const AVOption movie_options[]= {
00060 {"format_name", "set format name", OFFSET(format_name), AV_OPT_TYPE_STRING, {.str = 0}, CHAR_MIN, CHAR_MAX },
00061 {"f", "set format name", OFFSET(format_name), AV_OPT_TYPE_STRING, {.str = 0}, CHAR_MIN, CHAR_MAX },
00062 {"stream_index", "set stream index", OFFSET(stream_index), AV_OPT_TYPE_INT, {.dbl = -1}, -1, INT_MAX },
00063 {"si", "set stream index", OFFSET(stream_index), AV_OPT_TYPE_INT, {.dbl = -1}, -1, INT_MAX },
00064 {"seek_point", "set seekpoint (seconds)", OFFSET(seek_point_d), AV_OPT_TYPE_DOUBLE, {.dbl = 0}, 0, (INT64_MAX-1) / 1000000 },
00065 {"sp", "set seekpoint (seconds)", OFFSET(seek_point_d), AV_OPT_TYPE_DOUBLE, {.dbl = 0}, 0, (INT64_MAX-1) / 1000000 },
00066 {NULL},
00067 };
00068
00069 static const char *movie_get_name(void *ctx)
00070 {
00071 return "movie";
00072 }
00073
00074 static const AVClass movie_class = {
00075 "MovieContext",
00076 movie_get_name,
00077 movie_options
00078 };
00079
00080 static int movie_init(AVFilterContext *ctx)
00081 {
00082 MovieContext *movie = ctx->priv;
00083 AVInputFormat *iformat = NULL;
00084 AVCodec *codec;
00085 int ret;
00086 int64_t timestamp;
00087
00088 av_register_all();
00089
00090
00091 iformat = movie->format_name ? av_find_input_format(movie->format_name) : NULL;
00092
00093 movie->format_ctx = NULL;
00094 if ((ret = avformat_open_input(&movie->format_ctx, movie->file_name, iformat, NULL)) < 0) {
00095 av_log(ctx, AV_LOG_ERROR,
00096 "Failed to avformat_open_input '%s'\n", movie->file_name);
00097 return ret;
00098 }
00099 if ((ret = avformat_find_stream_info(movie->format_ctx, NULL)) < 0)
00100 av_log(ctx, AV_LOG_WARNING, "Failed to find stream info\n");
00101
00102
00103 if (movie->seek_point > 0) {
00104 timestamp = movie->seek_point;
00105
00106 if (movie->format_ctx->start_time != AV_NOPTS_VALUE) {
00107 if (timestamp > INT64_MAX - movie->format_ctx->start_time) {
00108 av_log(ctx, AV_LOG_ERROR,
00109 "%s: seek value overflow with start_time:%"PRId64" seek_point:%"PRId64"\n",
00110 movie->file_name, movie->format_ctx->start_time, movie->seek_point);
00111 return AVERROR(EINVAL);
00112 }
00113 timestamp += movie->format_ctx->start_time;
00114 }
00115 if ((ret = av_seek_frame(movie->format_ctx, -1, timestamp, AVSEEK_FLAG_BACKWARD)) < 0) {
00116 av_log(ctx, AV_LOG_ERROR, "%s: could not seek to position %"PRId64"\n",
00117 movie->file_name, timestamp);
00118 return ret;
00119 }
00120 }
00121
00122
00123 if ((ret = av_find_best_stream(movie->format_ctx, AVMEDIA_TYPE_VIDEO,
00124 movie->stream_index, -1, NULL, 0)) < 0) {
00125 av_log(ctx, AV_LOG_ERROR, "No video stream with index '%d' found\n",
00126 movie->stream_index);
00127 return ret;
00128 }
00129 movie->stream_index = ret;
00130 movie->codec_ctx = movie->format_ctx->streams[movie->stream_index]->codec;
00131
00132
00133
00134
00135
00136 codec = avcodec_find_decoder(movie->codec_ctx->codec_id);
00137 if (!codec) {
00138 av_log(ctx, AV_LOG_ERROR, "Failed to find any codec\n");
00139 return AVERROR(EINVAL);
00140 }
00141
00142 if ((ret = avcodec_open2(movie->codec_ctx, codec, NULL)) < 0) {
00143 av_log(ctx, AV_LOG_ERROR, "Failed to open codec\n");
00144 return ret;
00145 }
00146
00147 if (!(movie->frame = avcodec_alloc_frame()) ) {
00148 av_log(ctx, AV_LOG_ERROR, "Failed to alloc frame\n");
00149 return AVERROR(ENOMEM);
00150 }
00151
00152 movie->w = movie->codec_ctx->width;
00153 movie->h = movie->codec_ctx->height;
00154
00155 av_log(ctx, AV_LOG_INFO, "seek_point:%"PRIi64" format_name:%s file_name:%s stream_index:%d\n",
00156 movie->seek_point, movie->format_name, movie->file_name,
00157 movie->stream_index);
00158
00159 return 0;
00160 }
00161
00162 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
00163 {
00164 MovieContext *movie = ctx->priv;
00165 int ret;
00166 movie->class = &movie_class;
00167 av_opt_set_defaults(movie);
00168
00169 if (args)
00170 movie->file_name = av_get_token(&args, ":");
00171 if (!movie->file_name || !*movie->file_name) {
00172 av_log(ctx, AV_LOG_ERROR, "No filename provided!\n");
00173 return AVERROR(EINVAL);
00174 }
00175
00176 if (*args++ == ':' && (ret = av_set_options_string(movie, args, "=", ":")) < 0) {
00177 av_log(ctx, AV_LOG_ERROR, "Error parsing options string: '%s'\n", args);
00178 return ret;
00179 }
00180
00181 movie->seek_point = movie->seek_point_d * 1000000 + 0.5;
00182
00183 return movie_init(ctx);
00184 }
00185
00186 static av_cold void uninit(AVFilterContext *ctx)
00187 {
00188 MovieContext *movie = ctx->priv;
00189
00190 av_free(movie->file_name);
00191 av_free(movie->format_name);
00192 if (movie->codec_ctx)
00193 avcodec_close(movie->codec_ctx);
00194 if (movie->format_ctx)
00195 avformat_close_input(&movie->format_ctx);
00196 avfilter_unref_buffer(movie->picref);
00197 av_freep(&movie->frame);
00198 }
00199
00200 static int query_formats(AVFilterContext *ctx)
00201 {
00202 MovieContext *movie = ctx->priv;
00203 enum PixelFormat pix_fmts[] = { movie->codec_ctx->pix_fmt, PIX_FMT_NONE };
00204
00205 avfilter_set_common_formats(ctx, avfilter_make_format_list(pix_fmts));
00206 return 0;
00207 }
00208
00209 static int config_output_props(AVFilterLink *outlink)
00210 {
00211 MovieContext *movie = outlink->src->priv;
00212
00213 outlink->w = movie->w;
00214 outlink->h = movie->h;
00215 outlink->time_base = movie->format_ctx->streams[movie->stream_index]->time_base;
00216
00217 return 0;
00218 }
00219
00220 static int movie_get_frame(AVFilterLink *outlink)
00221 {
00222 MovieContext *movie = outlink->src->priv;
00223 AVPacket pkt;
00224 int ret, frame_decoded;
00225 AVStream *st = movie->format_ctx->streams[movie->stream_index];
00226
00227 if (movie->is_done == 1)
00228 return 0;
00229
00230 while ((ret = av_read_frame(movie->format_ctx, &pkt)) >= 0) {
00231
00232 if (pkt.stream_index == movie->stream_index) {
00233 movie->codec_ctx->reordered_opaque = pkt.pos;
00234 avcodec_decode_video2(movie->codec_ctx, movie->frame, &frame_decoded, &pkt);
00235
00236 if (frame_decoded) {
00237
00238 movie->picref = avfilter_get_video_buffer(outlink, AV_PERM_WRITE | AV_PERM_PRESERVE |
00239 AV_PERM_REUSE2, outlink->w, outlink->h);
00240 av_image_copy(movie->picref->data, movie->picref->linesize,
00241 movie->frame->data, movie->frame->linesize,
00242 movie->picref->format, outlink->w, outlink->h);
00243 avfilter_copy_frame_props(movie->picref, movie->frame);
00244
00245
00246
00247
00248 movie->picref->pts = movie->frame->pkt_pts == AV_NOPTS_VALUE ?
00249 movie->frame->pkt_dts : movie->frame->pkt_pts;
00250
00251 movie->picref->pos = movie->frame->reordered_opaque;
00252 if (!movie->frame->sample_aspect_ratio.num)
00253 movie->picref->video->pixel_aspect = st->sample_aspect_ratio;
00254 av_dlog(outlink->src,
00255 "movie_get_frame(): file:'%s' pts:%"PRId64" time:%lf pos:%"PRId64" aspect:%d/%d\n",
00256 movie->file_name, movie->picref->pts,
00257 (double)movie->picref->pts * av_q2d(st->time_base),
00258 movie->picref->pos,
00259 movie->picref->video->pixel_aspect.num, movie->picref->video->pixel_aspect.den);
00260
00261 av_free_packet(&pkt);
00262
00263 return 0;
00264 }
00265 }
00266
00267 av_free_packet(&pkt);
00268 }
00269
00270
00271
00272 if (ret == AVERROR_EOF)
00273 movie->is_done = 1;
00274 return ret;
00275 }
00276
00277 static int request_frame(AVFilterLink *outlink)
00278 {
00279 AVFilterBufferRef *outpicref;
00280 MovieContext *movie = outlink->src->priv;
00281 int ret;
00282
00283 if (movie->is_done)
00284 return AVERROR_EOF;
00285 if ((ret = movie_get_frame(outlink)) < 0)
00286 return ret;
00287
00288 outpicref = avfilter_ref_buffer(movie->picref, ~0);
00289 avfilter_start_frame(outlink, outpicref);
00290 avfilter_draw_slice(outlink, 0, outlink->h, 1);
00291 avfilter_end_frame(outlink);
00292 avfilter_unref_buffer(movie->picref);
00293 movie->picref = NULL;
00294
00295 return 0;
00296 }
00297
00298 AVFilter avfilter_vsrc_movie = {
00299 .name = "movie",
00300 .description = NULL_IF_CONFIG_SMALL("Read from a movie source."),
00301 .priv_size = sizeof(MovieContext),
00302 .init = init,
00303 .uninit = uninit,
00304 .query_formats = query_formats,
00305
00306 .inputs = (AVFilterPad[]) {{ .name = NULL }},
00307 .outputs = (AVFilterPad[]) {{ .name = "default",
00308 .type = AVMEDIA_TYPE_VIDEO,
00309 .request_frame = request_frame,
00310 .config_props = config_output_props, },
00311 { .name = NULL}},
00312 };