• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

libavfilter/vsrc_movie.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2010 Stefano Sabatini
00003  * Copyright (c) 2008 Victor Paesa
00004  *
00005  * This file is part of FFmpeg.
00006  *
00007  * FFmpeg is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * FFmpeg is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with FFmpeg; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00020  */
00021 
00031 /* #define DEBUG */
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 "avcodec.h"
00039 #include "avfilter.h"
00040 
00041 typedef struct {
00042     const AVClass *class;
00043     int64_t seek_point;   
00044     double seek_point_d;
00045     char *format_name;
00046     char *file_name;
00047     int stream_index;
00048 
00049     AVFormatContext *format_ctx;
00050     AVCodecContext *codec_ctx;
00051     int is_done;
00052     AVFrame *frame;   
00053 
00054     int w, h;
00055     AVFilterBufferRef *picref;
00056 } MovieContext;
00057 
00058 #define OFFSET(x) offsetof(MovieContext, x)
00059 
00060 static const AVOption movie_options[]= {
00061 {"format_name",  "set format name",         OFFSET(format_name),  FF_OPT_TYPE_STRING, {.str =  0},  CHAR_MIN, CHAR_MAX },
00062 {"f",            "set format name",         OFFSET(format_name),  FF_OPT_TYPE_STRING, {.str =  0},  CHAR_MIN, CHAR_MAX },
00063 {"stream_index", "set stream index",        OFFSET(stream_index), FF_OPT_TYPE_INT,    {.dbl = -1},  -1,       INT_MAX  },
00064 {"si",           "set stream index",        OFFSET(stream_index), FF_OPT_TYPE_INT,    {.dbl = -1},  -1,       INT_MAX  },
00065 {"seek_point",   "set seekpoint (seconds)", OFFSET(seek_point_d), FF_OPT_TYPE_DOUBLE, {.dbl =  0},  0,        (INT64_MAX-1) / 1000000 },
00066 {"sp",           "set seekpoint (seconds)", OFFSET(seek_point_d), FF_OPT_TYPE_DOUBLE, {.dbl =  0},  0,        (INT64_MAX-1) / 1000000 },
00067 {NULL},
00068 };
00069 
00070 static const char *movie_get_name(void *ctx)
00071 {
00072     return "movie";
00073 }
00074 
00075 static const AVClass movie_class = {
00076     "MovieContext",
00077     movie_get_name,
00078     movie_options
00079 };
00080 
00081 static int movie_init(AVFilterContext *ctx)
00082 {
00083     MovieContext *movie = ctx->priv;
00084     AVInputFormat *iformat = NULL;
00085     AVCodec *codec;
00086     int ret;
00087     int64_t timestamp;
00088 
00089     av_register_all();
00090 
00091     // Try to find the movie format (container)
00092     iformat = movie->format_name ? av_find_input_format(movie->format_name) : NULL;
00093 
00094     movie->format_ctx = NULL;
00095     if ((ret = avformat_open_input(&movie->format_ctx, movie->file_name, iformat, NULL)) < 0) {
00096         av_log(ctx, AV_LOG_ERROR,
00097                "Failed to avformat_open_input '%s'\n", movie->file_name);
00098         return ret;
00099     }
00100     if ((ret = av_find_stream_info(movie->format_ctx)) < 0)
00101         av_log(ctx, AV_LOG_WARNING, "Failed to find stream info\n");
00102 
00103     // if seeking requested, we execute it
00104     if (movie->seek_point > 0) {
00105         timestamp = movie->seek_point;
00106         // add the stream start time, should it exist
00107         if (movie->format_ctx->start_time != AV_NOPTS_VALUE) {
00108             if (timestamp > INT64_MAX - movie->format_ctx->start_time) {
00109                 av_log(ctx, AV_LOG_ERROR,
00110                        "%s: seek value overflow with start_time:%"PRId64" seek_point:%"PRId64"\n",
00111                        movie->file_name, movie->format_ctx->start_time, movie->seek_point);
00112                 return AVERROR(EINVAL);
00113             }
00114             timestamp += movie->format_ctx->start_time;
00115         }
00116         if ((ret = av_seek_frame(movie->format_ctx, -1, timestamp, AVSEEK_FLAG_BACKWARD)) < 0) {
00117             av_log(ctx, AV_LOG_ERROR, "%s: could not seek to position %"PRId64"\n",
00118                    movie->file_name, timestamp);
00119             return ret;
00120         }
00121     }
00122 
00123     /* select the video stream */
00124     if ((ret = av_find_best_stream(movie->format_ctx, AVMEDIA_TYPE_VIDEO,
00125                                    movie->stream_index, -1, NULL, 0)) < 0) {
00126         av_log(ctx, AV_LOG_ERROR, "No video stream with index '%d' found\n",
00127                movie->stream_index);
00128         return ret;
00129     }
00130     movie->stream_index = ret;
00131     movie->codec_ctx = movie->format_ctx->streams[movie->stream_index]->codec;
00132 
00133     /*
00134      * So now we've got a pointer to the so-called codec context for our video
00135      * stream, but we still have to find the actual codec and open it.
00136      */
00137     codec = avcodec_find_decoder(movie->codec_ctx->codec_id);
00138     if (!codec) {
00139         av_log(ctx, AV_LOG_ERROR, "Failed to find any codec\n");
00140         return AVERROR(EINVAL);
00141     }
00142 
00143     if ((ret = avcodec_open(movie->codec_ctx, codec)) < 0) {
00144         av_log(ctx, AV_LOG_ERROR, "Failed to open codec\n");
00145         return ret;
00146     }
00147 
00148     if (!(movie->frame = avcodec_alloc_frame()) ) {
00149         av_log(ctx, AV_LOG_ERROR, "Failed to alloc frame\n");
00150         return AVERROR(ENOMEM);
00151     }
00152 
00153     movie->w = movie->codec_ctx->width;
00154     movie->h = movie->codec_ctx->height;
00155 
00156     av_log(ctx, AV_LOG_INFO, "seek_point:%"PRIi64" format_name:%s file_name:%s stream_index:%d\n",
00157            movie->seek_point, movie->format_name, movie->file_name,
00158            movie->stream_index);
00159 
00160     return 0;
00161 }
00162 
00163 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
00164 {
00165     MovieContext *movie = ctx->priv;
00166     int ret;
00167     movie->class = &movie_class;
00168     av_opt_set_defaults2(movie, 0, 0);
00169 
00170     if (args)
00171         movie->file_name = av_get_token(&args, ":");
00172     if (!movie->file_name || !*movie->file_name) {
00173         av_log(ctx, AV_LOG_ERROR, "No filename provided!\n");
00174         return AVERROR(EINVAL);
00175     }
00176 
00177     if (*args++ == ':' && (ret = av_set_options_string(movie, args, "=", ":")) < 0) {
00178         av_log(ctx, AV_LOG_ERROR, "Error parsing options string: '%s'\n", args);
00179         return ret;
00180     }
00181 
00182     movie->seek_point = movie->seek_point_d * 1000000 + 0.5;
00183 
00184     return movie_init(ctx);
00185 }
00186 
00187 static av_cold void uninit(AVFilterContext *ctx)
00188 {
00189     MovieContext *movie = ctx->priv;
00190 
00191     av_free(movie->file_name);
00192     av_free(movie->format_name);
00193     if (movie->codec_ctx)
00194         avcodec_close(movie->codec_ctx);
00195     if (movie->format_ctx)
00196         av_close_input_file(movie->format_ctx);
00197     avfilter_unref_buffer(movie->picref);
00198     av_freep(&movie->frame);
00199 }
00200 
00201 static int query_formats(AVFilterContext *ctx)
00202 {
00203     MovieContext *movie = ctx->priv;
00204     enum PixelFormat pix_fmts[] = { movie->codec_ctx->pix_fmt, PIX_FMT_NONE };
00205 
00206     avfilter_set_common_pixel_formats(ctx, avfilter_make_format_list(pix_fmts));
00207     return 0;
00208 }
00209 
00210 static int config_output_props(AVFilterLink *outlink)
00211 {
00212     MovieContext *movie = outlink->src->priv;
00213 
00214     outlink->w = movie->w;
00215     outlink->h = movie->h;
00216     outlink->time_base = movie->format_ctx->streams[movie->stream_index]->time_base;
00217 
00218     return 0;
00219 }
00220 
00221 static int movie_get_frame(AVFilterLink *outlink)
00222 {
00223     MovieContext *movie = outlink->src->priv;
00224     AVPacket pkt;
00225     int ret, frame_decoded;
00226     AVStream *st = movie->format_ctx->streams[movie->stream_index];
00227 
00228     if (movie->is_done == 1)
00229         return 0;
00230 
00231     while ((ret = av_read_frame(movie->format_ctx, &pkt)) >= 0) {
00232         // Is this a packet from the video stream?
00233         if (pkt.stream_index == movie->stream_index) {
00234             avcodec_decode_video2(movie->codec_ctx, movie->frame, &frame_decoded, &pkt);
00235 
00236             if (frame_decoded) {
00237                 /* FIXME: avoid the memcpy */
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                 /* FIXME: use a PTS correction mechanism as that in
00246                  * ffplay.c when some API will be available for that */
00247                 /* use pkt_dts if pkt_pts is not available */
00248                 movie->picref->pts = movie->frame->pkt_pts == AV_NOPTS_VALUE ?
00249                     movie->frame->pkt_dts : movie->frame->pkt_pts;
00250                 if (!movie->frame->sample_aspect_ratio.num)
00251                     movie->picref->video->sample_aspect_ratio = st->sample_aspect_ratio;
00252                 av_dlog(outlink->src,
00253                         "movie_get_frame(): file:'%s' pts:%"PRId64" time:%lf pos:%"PRId64" aspect:%d/%d\n",
00254                         movie->file_name, movie->picref->pts,
00255                         (double)movie->picref->pts * av_q2d(st->time_base),
00256                         movie->picref->pos,
00257                         movie->picref->video->sample_aspect_ratio.num,
00258                         movie->picref->video->sample_aspect_ratio.den);
00259                 // We got it. Free the packet since we are returning
00260                 av_free_packet(&pkt);
00261 
00262                 return 0;
00263             }
00264         }
00265         // Free the packet that was allocated by av_read_frame
00266         av_free_packet(&pkt);
00267     }
00268 
00269     // On multi-frame source we should stop the mixing process when
00270     // the movie source does not have more frames
00271     if (ret == AVERROR_EOF)
00272         movie->is_done = 1;
00273     return ret;
00274 }
00275 
00276 static int request_frame(AVFilterLink *outlink)
00277 {
00278     AVFilterBufferRef *outpicref;
00279     MovieContext *movie = outlink->src->priv;
00280     int ret;
00281 
00282     if (movie->is_done)
00283         return AVERROR_EOF;
00284     if ((ret = movie_get_frame(outlink)) < 0)
00285         return ret;
00286 
00287     outpicref = avfilter_ref_buffer(movie->picref, ~0);
00288     avfilter_start_frame(outlink, outpicref);
00289     avfilter_draw_slice(outlink, 0, outlink->h, 1);
00290     avfilter_end_frame(outlink);
00291     avfilter_unref_buffer(movie->picref);
00292     movie->picref = NULL;
00293 
00294     return 0;
00295 }
00296 
00297 AVFilter avfilter_vsrc_movie = {
00298     .name          = "movie",
00299     .description   = NULL_IF_CONFIG_SMALL("Read from a movie source."),
00300     .priv_size     = sizeof(MovieContext),
00301     .init          = init,
00302     .uninit        = uninit,
00303     .query_formats = query_formats,
00304 
00305     .inputs    = (AVFilterPad[]) {{ .name = NULL }},
00306     .outputs   = (AVFilterPad[]) {{ .name            = "default",
00307                                     .type            = AVMEDIA_TYPE_VIDEO,
00308                                     .request_frame   = request_frame,
00309                                     .config_props    = config_output_props, },
00310                                   { .name = NULL}},
00311 };

Generated on Fri Feb 22 2013 07:24:31 for FFmpeg by  doxygen 1.7.1