[FFmpeg-devel] [PATCH] lavfi: add amovie source - audio movie source

Stefano Sabatini stefano.sabatini-lala at poste.it
Sat Aug 20 16:09:10 CEST 2011


---
 libavfilter/Makefile     |    2 +
 libavfilter/allfilters.c |    1 +
 libavfilter/movie.c      |  156 ++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 159 insertions(+), 0 deletions(-)

diff --git a/libavfilter/Makefile b/libavfilter/Makefile
index 3eae17f..23c0298 100644
--- a/libavfilter/Makefile
+++ b/libavfilter/Makefile
@@ -2,6 +2,7 @@ include $(SUBDIR)../config.mak
 
 NAME = avfilter
 FFLIBS = avutil
+FFLIBS-$(CONFIG_AMOVIE_FILTER) += avformat avcodec
 FFLIBS-$(CONFIG_ARESAMPLE_FILTER) += avcodec
 FFLIBS-$(CONFIG_MOVIE_FILTER) += avformat avcodec
 FFLIBS-$(CONFIG_SCALE_FILTER) += swscale
@@ -24,6 +25,7 @@ OBJS-$(CONFIG_ANULL_FILTER)                  += af_anull.o
 OBJS-$(CONFIG_ARESAMPLE_FILTER)              += af_aresample.o
 OBJS-$(CONFIG_ASHOWINFO_FILTER)              += af_ashowinfo.o
 
+OBJS-$(CONFIG_AMOVIE_FILTER)                 += movie.o
 OBJS-$(CONFIG_ANULLSRC_FILTER)               += asrc_anullsrc.o
 
 OBJS-$(CONFIG_ABUFFERSINK_FILTER)            += asink_abuffer.o
diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c
index 5cf330c..57a856b 100644
--- a/libavfilter/allfilters.c
+++ b/libavfilter/allfilters.c
@@ -39,6 +39,7 @@ void avfilter_register_all(void)
     REGISTER_FILTER (ARESAMPLE,   aresample,   af);
     REGISTER_FILTER (ASHOWINFO,   ashowinfo,   af);
 
+    REGISTER_FILTER (AMOVIE,      amovie,      asrc);
     REGISTER_FILTER (ANULLSRC,    anullsrc,    asrc);
 
     REGISTER_FILTER (ABUFFERSINK, abuffersink, asink);
diff --git a/libavfilter/movie.c b/libavfilter/movie.c
index f3c11bc..753ed24 100644
--- a/libavfilter/movie.c
+++ b/libavfilter/movie.c
@@ -55,6 +55,13 @@ typedef struct {
     /* only video options */
     int w, h;
     AVFilterBufferRef *picref;
+
+    /* only audio fields */
+    int16_t *samples_buf;
+    int samples_buf_size;
+    int bps;            ///< bytes per sample
+    AVPacket pkt, pkt0;
+    AVFilterBufferRef *samplesref;
 } MovieContext;
 
 #define OFFSET(x) offsetof(MovieContext, x)
@@ -185,8 +192,11 @@ static av_cold void movie_common_uninit(AVFilterContext *ctx)
 
     avfilter_unref_buffer(movie->picref);
     av_freep(&movie->frame);
+    av_freep(&movie->samples_buf);
 }
 
+#if CONFIG_MOVIE_FILTER
+
 static av_cold int vsrc_movie_init(AVFilterContext *ctx, const char *args, void *opaque)
 {
     MovieContext *movie = ctx->priv;
@@ -317,3 +327,149 @@ AVFilter avfilter_vsrc_movie = {
                                     .config_props    = vsrc_movie_config_output_props, },
                                   { .name = NULL}},
 };
+
+#endif  /* CONFIG_MOVIE_FILTER */
+
+#if CONFIG_AMOVIE_FILTER
+
+static av_cold int asrc_movie_init(AVFilterContext *ctx, const char *args, void *opaque)
+{
+    MovieContext *movie = ctx->priv;
+    int ret;
+
+    if ((ret = movie_common_init(ctx, args, opaque, AVMEDIA_TYPE_AUDIO)) < 0)
+        return ret;
+
+    movie->bps = av_get_bytes_per_sample(movie->codec_ctx->sample_fmt);
+    return 0;
+}
+
+static int asrc_movie_query_formats(AVFilterContext *ctx)
+{
+    MovieContext *movie = ctx->priv;
+    AVCodecContext *c = movie->codec_ctx;
+
+    enum AVSampleFormat sample_fmts[] = { c->sample_fmt, AV_SAMPLE_FMT_NONE };
+    int64_t chlayouts[] = { c->channel_layout, -1 };
+    int packing_fmts[] = { AVFILTER_PACKED, -1 };
+
+    avfilter_set_common_sample_formats (ctx, avfilter_make_format_list(sample_fmts));
+    avfilter_set_common_channel_layouts(ctx, avfilter_make_format64_list(chlayouts));
+    avfilter_set_common_packing_formats(ctx, avfilter_make_format_list(packing_fmts));
+
+    return 0;
+}
+
+static int asrc_movie_config_output_props(AVFilterLink *outlink)
+{
+    MovieContext *movie = outlink->src->priv;
+    AVCodecContext *c = movie->codec_ctx;
+
+    outlink->sample_rate = c->sample_rate;
+    outlink->time_base = movie->format_ctx->streams[movie->stream_index]->time_base;
+
+    return 0;
+}
+
+static int asrc_movie_get_samples(AVFilterLink *outlink)
+{
+    MovieContext *movie = outlink->src->priv;
+    AVPacket pkt;
+    int ret, samples_size, decoded_data_size;
+
+    if (!movie->pkt.size && movie->is_done == 1)
+        return AVERROR_EOF;
+
+    /* check for another frame, in case the previous one was completely consumed */
+    if (!movie->pkt.size) {
+        while ((ret = av_read_frame(movie->format_ctx, &pkt)) >= 0) {
+            // Is this a packet from the selected stream?
+            if (pkt.stream_index != movie->stream_index) {
+                av_free_packet(&pkt);
+                continue;
+            } else {
+                movie->pkt0 = movie->pkt = pkt;
+                break;
+            }
+        }
+
+        if (ret == AVERROR_EOF) {
+            movie->is_done = 1;
+            return ret;
+        }
+    }
+
+    /* reallocate the buffer for the decoded samples, if necessary */
+    samples_size =
+        FFMAX(movie->pkt.size*sizeof(*movie->samples_buf),
+              AVCODEC_MAX_AUDIO_FRAME_SIZE);
+    if (samples_size > movie->samples_buf_size) {
+        movie->samples_buf = av_fast_realloc(movie->samples_buf,
+                                             &movie->samples_buf_size, samples_size);
+        if (!movie->samples_buf)
+            return AVERROR(ENOMEM);
+    }
+    decoded_data_size = movie->samples_buf_size;
+
+    /* decode and update the movie pkt */
+    ret = avcodec_decode_audio3(movie->codec_ctx, movie->samples_buf,
+                                &decoded_data_size, &movie->pkt);
+    if (ret < 0)
+        return ret;
+    movie->pkt.data += ret;
+    movie->pkt.size -= ret;
+
+    /* wrap the decoded data in a samplesref */
+    if (decoded_data_size > 0) {
+        int nb_samples = decoded_data_size / movie->bps / movie->codec_ctx->channels;
+        movie->samplesref =
+            avfilter_get_audio_buffer(outlink, AV_PERM_WRITE,
+                                      movie->codec_ctx->sample_fmt, nb_samples,
+                                      movie->codec_ctx->channel_layout, 0);
+        memcpy(movie->samplesref->data[0], movie->samples_buf, decoded_data_size);
+        movie->samplesref->pts = movie->pkt.pts;
+        movie->samplesref->pos = movie->pkt.pos;
+        movie->samplesref->audio->sample_rate = movie->codec_ctx->sample_rate;
+    }
+
+    // We got it. Free the packet since we are returning
+    if (movie->pkt.size <= 0)
+        av_free_packet(&movie->pkt0);
+
+    return 0;
+}
+
+static int asrc_movie_request_frame(AVFilterLink *outlink)
+{
+    MovieContext *movie = outlink->src->priv;
+    int ret;
+
+    if (movie->is_done)
+        return AVERROR_EOF;
+    if ((ret = asrc_movie_get_samples(outlink)) < 0)
+        return ret;
+
+    avfilter_filter_samples(outlink, avfilter_ref_buffer(movie->samplesref, ~0));
+    avfilter_unref_buffer(movie->samplesref);
+    movie->samplesref = NULL;
+
+    return 0;
+}
+
+AVFilter avfilter_asrc_amovie = {
+    .name          = "amovie",
+    .description   = NULL_IF_CONFIG_SMALL("Read audio from a movie source."),
+    .priv_size     = sizeof(MovieContext),
+    .init          = asrc_movie_init,
+    .uninit        = movie_common_uninit,
+    .query_formats = asrc_movie_query_formats,
+
+    .inputs    = (AVFilterPad[]) {{ .name = NULL }},
+    .outputs   = (AVFilterPad[]) {{ .name            = "default",
+                                    .type            = AVMEDIA_TYPE_AUDIO,
+                                    .request_frame   = asrc_movie_request_frame,
+                                    .config_props    = asrc_movie_config_output_props, },
+                                  { .name = NULL}},
+};
+
+#endif /* CONFIG_AMOVIE_FILTER */
-- 
1.7.2.5



More information about the ffmpeg-devel mailing list