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

libavfilter/src_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     /* common A/V fields */
00043     const AVClass *class;
00044     int64_t seek_point;   
00045     double seek_point_d;
00046     char *format_name;
00047     char *file_name;
00048     int stream_index;
00049 
00050     AVFormatContext *format_ctx;
00051     AVCodecContext *codec_ctx;
00052     int is_done;
00053     AVFrame *frame;   
00054 
00055     /* video-only fields */
00056     int w, h;
00057     AVFilterBufferRef *picref;
00058 
00059     /* audio-only fields */
00060     int bps;            
00061     AVPacket pkt, pkt0;
00062     AVFilterBufferRef *samplesref;
00063 } MovieContext;
00064 
00065 #define OFFSET(x) offsetof(MovieContext, x)
00066 
00067 static const AVOption movie_options[]= {
00068 {"format_name",  "set format name",         OFFSET(format_name),  AV_OPT_TYPE_STRING, {.str =  0},  CHAR_MIN, CHAR_MAX },
00069 {"f",            "set format name",         OFFSET(format_name),  AV_OPT_TYPE_STRING, {.str =  0},  CHAR_MIN, CHAR_MAX },
00070 {"stream_index", "set stream index",        OFFSET(stream_index), AV_OPT_TYPE_INT,    {.dbl = -1},  -1,       INT_MAX  },
00071 {"si",           "set stream index",        OFFSET(stream_index), AV_OPT_TYPE_INT,    {.dbl = -1},  -1,       INT_MAX  },
00072 {"seek_point",   "set seekpoint (seconds)", OFFSET(seek_point_d), AV_OPT_TYPE_DOUBLE, {.dbl =  0},  0,        (INT64_MAX-1) / 1000000 },
00073 {"sp",           "set seekpoint (seconds)", OFFSET(seek_point_d), AV_OPT_TYPE_DOUBLE, {.dbl =  0},  0,        (INT64_MAX-1) / 1000000 },
00074 {NULL},
00075 };
00076 
00077 static const char *movie_get_name(void *ctx)
00078 {
00079     return "movie";
00080 }
00081 
00082 static const AVClass movie_class = {
00083     "MovieContext",
00084     movie_get_name,
00085     movie_options
00086 };
00087 
00088 static av_cold int movie_common_init(AVFilterContext *ctx, const char *args, void *opaque,
00089                                      enum AVMediaType type)
00090 {
00091     MovieContext *movie = ctx->priv;
00092     AVInputFormat *iformat = NULL;
00093     AVCodec *codec;
00094     int64_t timestamp;
00095     int ret;
00096 
00097     movie->class = &movie_class;
00098     av_opt_set_defaults(movie);
00099 
00100     if (args)
00101         movie->file_name = av_get_token(&args, ":");
00102     if (!movie->file_name || !*movie->file_name) {
00103         av_log(ctx, AV_LOG_ERROR, "No filename provided!\n");
00104         return AVERROR(EINVAL);
00105     }
00106 
00107     if (*args++ == ':' && (ret = av_set_options_string(movie, args, "=", ":")) < 0) {
00108         av_log(ctx, AV_LOG_ERROR, "Error parsing options string: '%s'\n", args);
00109         return ret;
00110     }
00111 
00112     movie->seek_point = movie->seek_point_d * 1000000 + 0.5;
00113 
00114     av_register_all();
00115 
00116     // Try to find the movie format (container)
00117     iformat = movie->format_name ? av_find_input_format(movie->format_name) : NULL;
00118 
00119     movie->format_ctx = NULL;
00120     if ((ret = avformat_open_input(&movie->format_ctx, movie->file_name, iformat, NULL)) < 0) {
00121         av_log(ctx, AV_LOG_ERROR,
00122                "Failed to avformat_open_input '%s'\n", movie->file_name);
00123         return ret;
00124     }
00125     if ((ret = avformat_find_stream_info(movie->format_ctx, NULL)) < 0)
00126         av_log(ctx, AV_LOG_WARNING, "Failed to find stream info\n");
00127 
00128     // if seeking requested, we execute it
00129     if (movie->seek_point > 0) {
00130         timestamp = movie->seek_point;
00131         // add the stream start time, should it exist
00132         if (movie->format_ctx->start_time != AV_NOPTS_VALUE) {
00133             if (timestamp > INT64_MAX - movie->format_ctx->start_time) {
00134                 av_log(ctx, AV_LOG_ERROR,
00135                        "%s: seek value overflow with start_time:%"PRId64" seek_point:%"PRId64"\n",
00136                        movie->file_name, movie->format_ctx->start_time, movie->seek_point);
00137                 return AVERROR(EINVAL);
00138             }
00139             timestamp += movie->format_ctx->start_time;
00140         }
00141         if ((ret = av_seek_frame(movie->format_ctx, -1, timestamp, AVSEEK_FLAG_BACKWARD)) < 0) {
00142             av_log(ctx, AV_LOG_ERROR, "%s: could not seek to position %"PRId64"\n",
00143                    movie->file_name, timestamp);
00144             return ret;
00145         }
00146     }
00147 
00148     /* select the media stream */
00149     if ((ret = av_find_best_stream(movie->format_ctx, type,
00150                                    movie->stream_index, -1, NULL, 0)) < 0) {
00151         av_log(ctx, AV_LOG_ERROR, "No %s stream with index '%d' found\n",
00152                av_get_media_type_string(type), movie->stream_index);
00153         return ret;
00154     }
00155     movie->stream_index = ret;
00156     movie->codec_ctx = movie->format_ctx->streams[movie->stream_index]->codec;
00157 
00158     /*
00159      * So now we've got a pointer to the so-called codec context for our video
00160      * stream, but we still have to find the actual codec and open it.
00161      */
00162     codec = avcodec_find_decoder(movie->codec_ctx->codec_id);
00163     if (!codec) {
00164         av_log(ctx, AV_LOG_ERROR, "Failed to find any codec\n");
00165         return AVERROR(EINVAL);
00166     }
00167 
00168     if ((ret = avcodec_open2(movie->codec_ctx, codec, NULL)) < 0) {
00169         av_log(ctx, AV_LOG_ERROR, "Failed to open codec\n");
00170         return ret;
00171     }
00172 
00173     av_log(ctx, AV_LOG_INFO, "seek_point:%"PRIi64" format_name:%s file_name:%s stream_index:%d\n",
00174            movie->seek_point, movie->format_name, movie->file_name,
00175            movie->stream_index);
00176 
00177     if (!(movie->frame = avcodec_alloc_frame()) ) {
00178         av_log(ctx, AV_LOG_ERROR, "Failed to alloc frame\n");
00179         return AVERROR(ENOMEM);
00180     }
00181 
00182     return 0;
00183 }
00184 
00185 static av_cold void movie_common_uninit(AVFilterContext *ctx)
00186 {
00187     MovieContext *movie = ctx->priv;
00188 
00189     av_free(movie->file_name);
00190     av_free(movie->format_name);
00191     if (movie->codec_ctx)
00192         avcodec_close(movie->codec_ctx);
00193     if (movie->format_ctx)
00194         avformat_close_input(&movie->format_ctx);
00195 
00196     avfilter_unref_buffer(movie->picref);
00197     av_freep(&movie->frame);
00198 
00199     avfilter_unref_buffer(movie->samplesref);
00200 }
00201 
00202 #if CONFIG_MOVIE_FILTER
00203 
00204 static av_cold int movie_init(AVFilterContext *ctx, const char *args, void *opaque)
00205 {
00206     MovieContext *movie = ctx->priv;
00207     int ret;
00208 
00209     if ((ret = movie_common_init(ctx, args, opaque, AVMEDIA_TYPE_VIDEO)) < 0)
00210         return ret;
00211 
00212     movie->w = movie->codec_ctx->width;
00213     movie->h = movie->codec_ctx->height;
00214 
00215     return 0;
00216 }
00217 
00218 static int movie_query_formats(AVFilterContext *ctx)
00219 {
00220     MovieContext *movie = ctx->priv;
00221     enum PixelFormat pix_fmts[] = { movie->codec_ctx->pix_fmt, PIX_FMT_NONE };
00222 
00223     avfilter_set_common_pixel_formats(ctx, avfilter_make_format_list(pix_fmts));
00224     return 0;
00225 }
00226 
00227 static int movie_config_output_props(AVFilterLink *outlink)
00228 {
00229     MovieContext *movie = outlink->src->priv;
00230 
00231     outlink->w = movie->w;
00232     outlink->h = movie->h;
00233     outlink->time_base = movie->format_ctx->streams[movie->stream_index]->time_base;
00234 
00235     return 0;
00236 }
00237 
00238 static int movie_get_frame(AVFilterLink *outlink)
00239 {
00240     MovieContext *movie = outlink->src->priv;
00241     AVPacket pkt;
00242     int ret, frame_decoded;
00243     AVStream *st = movie->format_ctx->streams[movie->stream_index];
00244 
00245     if (movie->is_done == 1)
00246         return 0;
00247 
00248     while ((ret = av_read_frame(movie->format_ctx, &pkt)) >= 0) {
00249         // Is this a packet from the video stream?
00250         if (pkt.stream_index == movie->stream_index) {
00251             avcodec_decode_video2(movie->codec_ctx, movie->frame, &frame_decoded, &pkt);
00252 
00253             if (frame_decoded) {
00254                 /* FIXME: avoid the memcpy */
00255                 movie->picref = avfilter_get_video_buffer(outlink, AV_PERM_WRITE | AV_PERM_PRESERVE |
00256                                                           AV_PERM_REUSE2, outlink->w, outlink->h);
00257                 av_image_copy(movie->picref->data, movie->picref->linesize,
00258                               (void*)movie->frame->data,  movie->frame->linesize,
00259                               movie->picref->format, outlink->w, outlink->h);
00260                 avfilter_copy_frame_props(movie->picref, movie->frame);
00261 
00262                 /* FIXME: use a PTS correction mechanism as that in
00263                  * ffplay.c when some API will be available for that */
00264                 /* use pkt_dts if pkt_pts is not available */
00265                 movie->picref->pts = movie->frame->pkt_pts == AV_NOPTS_VALUE ?
00266                     movie->frame->pkt_dts : movie->frame->pkt_pts;
00267 
00268                 if (!movie->frame->sample_aspect_ratio.num)
00269                     movie->picref->video->sample_aspect_ratio = st->sample_aspect_ratio;
00270                 av_dlog(outlink->src,
00271                         "movie_get_frame(): file:'%s' pts:%"PRId64" time:%lf pos:%"PRId64" aspect:%d/%d\n",
00272                         movie->file_name, movie->picref->pts,
00273                         (double)movie->picref->pts * av_q2d(st->time_base),
00274                         movie->picref->pos,
00275                         movie->picref->video->sample_aspect_ratio.num,
00276                         movie->picref->video->sample_aspect_ratio.den);
00277                 // We got it. Free the packet since we are returning
00278                 av_free_packet(&pkt);
00279 
00280                 return 0;
00281             }
00282         }
00283         // Free the packet that was allocated by av_read_frame
00284         av_free_packet(&pkt);
00285     }
00286 
00287     // On multi-frame source we should stop the mixing process when
00288     // the movie source does not have more frames
00289     if (ret == AVERROR_EOF)
00290         movie->is_done = 1;
00291     return ret;
00292 }
00293 
00294 static int movie_request_frame(AVFilterLink *outlink)
00295 {
00296     AVFilterBufferRef *outpicref;
00297     MovieContext *movie = outlink->src->priv;
00298     int ret;
00299 
00300     if (movie->is_done)
00301         return AVERROR_EOF;
00302     if ((ret = movie_get_frame(outlink)) < 0)
00303         return ret;
00304 
00305     outpicref = avfilter_ref_buffer(movie->picref, ~0);
00306     avfilter_start_frame(outlink, outpicref);
00307     avfilter_draw_slice(outlink, 0, outlink->h, 1);
00308     avfilter_end_frame(outlink);
00309     avfilter_unref_buffer(movie->picref);
00310     movie->picref = NULL;
00311 
00312     return 0;
00313 }
00314 
00315 AVFilter avfilter_vsrc_movie = {
00316     .name          = "movie",
00317     .description   = NULL_IF_CONFIG_SMALL("Read from a movie source."),
00318     .priv_size     = sizeof(MovieContext),
00319     .init          = movie_init,
00320     .uninit        = movie_common_uninit,
00321     .query_formats = movie_query_formats,
00322 
00323     .inputs    = (const AVFilterPad[]) {{ .name = NULL }},
00324     .outputs   = (const AVFilterPad[]) {{ .name      = "default",
00325                                     .type            = AVMEDIA_TYPE_VIDEO,
00326                                     .request_frame   = movie_request_frame,
00327                                     .config_props    = movie_config_output_props, },
00328                                   { .name = NULL}},
00329 };
00330 
00331 #endif  /* CONFIG_MOVIE_FILTER */
00332 
00333 #if CONFIG_AMOVIE_FILTER
00334 
00335 static av_cold int amovie_init(AVFilterContext *ctx, const char *args, void *opaque)
00336 {
00337     MovieContext *movie = ctx->priv;
00338     int ret;
00339 
00340     if ((ret = movie_common_init(ctx, args, opaque, AVMEDIA_TYPE_AUDIO)) < 0)
00341         return ret;
00342 
00343     movie->bps = av_get_bytes_per_sample(movie->codec_ctx->sample_fmt);
00344     return 0;
00345 }
00346 
00347 static int amovie_query_formats(AVFilterContext *ctx)
00348 {
00349     MovieContext *movie = ctx->priv;
00350     AVCodecContext *c = movie->codec_ctx;
00351 
00352     enum AVSampleFormat sample_fmts[] = { c->sample_fmt, -1 };
00353     int packing_fmts[] = { AVFILTER_PACKED, -1 };
00354     int64_t chlayouts[] = { c->channel_layout ? c->channel_layout :
00355                             av_get_default_channel_layout(c->channels), -1 };
00356 
00357     avfilter_set_common_sample_formats (ctx, avfilter_make_format_list(sample_fmts));
00358     avfilter_set_common_packing_formats(ctx, avfilter_make_format_list(packing_fmts));
00359     avfilter_set_common_channel_layouts(ctx, avfilter_make_format64_list(chlayouts));
00360 
00361     return 0;
00362 }
00363 
00364 static int amovie_config_output_props(AVFilterLink *outlink)
00365 {
00366     MovieContext *movie = outlink->src->priv;
00367     AVCodecContext *c = movie->codec_ctx;
00368 
00369     outlink->sample_rate = c->sample_rate;
00370     outlink->time_base = movie->format_ctx->streams[movie->stream_index]->time_base;
00371 
00372     return 0;
00373 }
00374 
00375 static int amovie_get_samples(AVFilterLink *outlink)
00376 {
00377     MovieContext *movie = outlink->src->priv;
00378     AVPacket pkt;
00379     int ret, got_frame = 0;
00380 
00381     if (!movie->pkt.size && movie->is_done == 1)
00382         return AVERROR_EOF;
00383 
00384     /* check for another frame, in case the previous one was completely consumed */
00385     if (!movie->pkt.size) {
00386         while ((ret = av_read_frame(movie->format_ctx, &pkt)) >= 0) {
00387             // Is this a packet from the selected stream?
00388             if (pkt.stream_index != movie->stream_index) {
00389                 av_free_packet(&pkt);
00390                 continue;
00391             } else {
00392                 movie->pkt0 = movie->pkt = pkt;
00393                 break;
00394             }
00395         }
00396 
00397         if (ret == AVERROR_EOF) {
00398             movie->is_done = 1;
00399             return ret;
00400         }
00401     }
00402 
00403     /* decode and update the movie pkt */
00404     avcodec_get_frame_defaults(movie->frame);
00405     ret = avcodec_decode_audio4(movie->codec_ctx, movie->frame, &got_frame, &movie->pkt);
00406     if (ret < 0) {
00407         movie->pkt.size = 0;
00408         return ret;
00409     }
00410     movie->pkt.data += ret;
00411     movie->pkt.size -= ret;
00412 
00413     /* wrap the decoded data in a samplesref */
00414     if (got_frame) {
00415         int nb_samples = movie->frame->nb_samples;
00416         int data_size =
00417             av_samples_get_buffer_size(NULL, movie->codec_ctx->channels,
00418                                        nb_samples, movie->codec_ctx->sample_fmt, 1);
00419         if (data_size < 0)
00420             return data_size;
00421         movie->samplesref =
00422             avfilter_get_audio_buffer(outlink, AV_PERM_WRITE, nb_samples);
00423         memcpy(movie->samplesref->data[0], movie->frame->data[0], data_size);
00424         movie->samplesref->pts = movie->pkt.pts;
00425         movie->samplesref->pos = movie->pkt.pos;
00426         movie->samplesref->audio->sample_rate = movie->codec_ctx->sample_rate;
00427     }
00428 
00429     // We got it. Free the packet since we are returning
00430     if (movie->pkt.size <= 0)
00431         av_free_packet(&movie->pkt0);
00432 
00433     return 0;
00434 }
00435 
00436 static int amovie_request_frame(AVFilterLink *outlink)
00437 {
00438     MovieContext *movie = outlink->src->priv;
00439     int ret;
00440 
00441     if (movie->is_done)
00442         return AVERROR_EOF;
00443     do {
00444         if ((ret = amovie_get_samples(outlink)) < 0)
00445             return ret;
00446     } while (!movie->samplesref);
00447 
00448     avfilter_filter_samples(outlink, avfilter_ref_buffer(movie->samplesref, ~0));
00449     avfilter_unref_buffer(movie->samplesref);
00450     movie->samplesref = NULL;
00451 
00452     return 0;
00453 }
00454 
00455 AVFilter avfilter_asrc_amovie = {
00456     .name          = "amovie",
00457     .description   = NULL_IF_CONFIG_SMALL("Read audio from a movie source."),
00458     .priv_size     = sizeof(MovieContext),
00459     .init          = amovie_init,
00460     .uninit        = movie_common_uninit,
00461     .query_formats = amovie_query_formats,
00462 
00463     .inputs    = (const AVFilterPad[]) {{ .name = NULL }},
00464     .outputs   = (const AVFilterPad[]) {{ .name      = "default",
00465                                     .type            = AVMEDIA_TYPE_AUDIO,
00466                                     .request_frame   = amovie_request_frame,
00467                                     .config_props    = amovie_config_output_props, },
00468                                   { .name = NULL}},
00469 };
00470 
00471 #endif /* CONFIG_AMOVIE_FILTER */
Generated on Fri Feb 1 2013 14:34:49 for FFmpeg by doxygen 1.7.1