[FFmpeg-devel] [PATCH 3/4] sink_buffer: implement fixed frame size.

Nicolas George nicolas.george at normalesup.org
Fri Jun 29 14:41:08 CEST 2012


Signed-off-by: Nicolas George <nicolas.george at normalesup.org>
---
 libavfilter/buffersink.h  |    9 ++++++
 libavfilter/sink_buffer.c |   76 ++++++++++++++++++++++++++++++++++++++++-----
 2 files changed, 77 insertions(+), 8 deletions(-)

diff --git a/libavfilter/buffersink.h b/libavfilter/buffersink.h
index e9148f7..808c0c7 100644
--- a/libavfilter/buffersink.h
+++ b/libavfilter/buffersink.h
@@ -56,6 +56,15 @@ typedef struct {
 AVABufferSinkParams *av_abuffersink_params_alloc(void);
 
 /**
+ * Set the frame size for an audio buffer sink.
+ *
+ * All calls to av_buffersink_get_buffer_ref will return a buffer with
+ * exactly the specified number of samples, or AVERROR(EAGAIN) if there is
+ * not enough. The last buffer at EOF will be padded with 0.
+ */
+void av_buffersink_set_frame_size(AVFilterContext *ctx, unsigned frame_size);
+
+/**
  * Tell av_buffersink_get_buffer_ref() to read video/samples buffer
  * reference, but not remove it from the buffer. This is useful if you
  * need only to read a video/samples buffer, without to fetch it.
diff --git a/libavfilter/sink_buffer.c b/libavfilter/sink_buffer.c
index db35858..f18a250 100644
--- a/libavfilter/sink_buffer.c
+++ b/libavfilter/sink_buffer.c
@@ -23,11 +23,14 @@
  * buffer video sink
  */
 
+#include "libavutil/audioconvert.h"
 #include "libavutil/avassert.h"
 #include "libavutil/fifo.h"
 #include "avfilter.h"
 #include "buffersink.h"
 #include "internal.h"
+#include "audio.h"
+#include "audioframe.h"
 
 extern AVFilter avfilter_vsink_buffersink;
 extern AVFilter avfilter_asink_abuffersink;
@@ -67,6 +70,9 @@ typedef struct {
     /* only used for audio */
     enum AVSampleFormat *sample_fmts;       ///< list of accepted sample formats, terminated by AV_SAMPLE_FMT_NONE
     int64_t *channel_layouts;               ///< list of accepted channel layouts, terminated by -1
+
+    AVFilterBufferRef *partial_buf;         ///< buffer being padded to frame_size
+    unsigned frame_size;                    ///< frame size, 0 if unused
 } BufferSinkContext;
 
 #define FIFO_INIT_SIZE 8
@@ -99,12 +105,10 @@ static av_cold void common_uninit(AVFilterContext *ctx)
     }
 }
 
-static void end_frame(AVFilterLink *inlink)
+static void add_frame(AVFilterContext *ctx, AVFilterBufferRef *frame)
 {
-    AVFilterContext *ctx = inlink->dst;
-    BufferSinkContext *buf = inlink->dst->priv;
+    BufferSinkContext *buf = ctx->priv;
 
-    av_assert1(inlink->cur_buf);
     if (av_fifo_space(buf->fifo) < sizeof(AVFilterBufferRef *)) {
         /* realloc fifo size */
         if (av_fifo_realloc2(buf->fifo, av_fifo_size(buf->fifo) * 2) < 0) {
@@ -117,7 +121,7 @@ static void end_frame(AVFilterLink *inlink)
 
     /* cache frame */
     av_fifo_generic_write(buf->fifo,
-                          &inlink->cur_buf, sizeof(AVFilterBufferRef *), NULL);
+                          &frame, sizeof(AVFilterBufferRef *), NULL);
     if (buf->warning_limit &&
         av_fifo_size(buf->fifo) / sizeof(AVFilterBufferRef *) >= buf->warning_limit) {
         av_log(ctx, AV_LOG_WARNING,
@@ -128,6 +132,30 @@ static void end_frame(AVFilterLink *inlink)
     }
 }
 
+static void end_frame(AVFilterLink *inlink)
+{
+    AVFilterContext *ctx = inlink->dst;
+
+    av_assert1(inlink->cur_buf);
+    add_frame(ctx, inlink->cur_buf);
+}
+
+void av_buffersink_set_frame_size(AVFilterContext *ctx, unsigned frame_size)
+{
+    BufferSinkContext *buf = ctx->priv;
+
+    av_assert1(ctx->filter == &avfilter_asink_abuffersink);
+    buf->frame_size = frame_size;
+}
+
+static void flush_partial_frame(AVFilterContext *ctx)
+{
+    BufferSinkContext *buf = ctx->priv;
+
+    add_frame(ctx, buf->partial_buf);
+    buf->partial_buf = NULL;
+}
+
 int av_buffersink_get_buffer_ref(AVFilterContext *ctx,
                                   AVFilterBufferRef **bufref, int flags)
 {
@@ -142,8 +170,15 @@ int av_buffersink_get_buffer_ref(AVFilterContext *ctx,
     if (!av_fifo_size(buf->fifo)) {
         if (flags & AV_BUFFERSINK_FLAG_NO_REQUEST)
             return AVERROR(EAGAIN);
-        if ((ret = ff_request_frame(inlink)) < 0)
-            return ret;
+        if ((ret = ff_request_frame(inlink)) < 0) {
+            if (ret == AVERROR_EOF && buf->frame_size && buf->partial_buf) {
+                flush_partial_frame(ctx);
+                if (!av_fifo_size(buf->fifo))
+                    return AVERROR(ENOMEM);
+            } else {
+                return ret;
+            }
+        }
     }
 
     if (!av_fifo_size(buf->fifo))
@@ -241,7 +276,30 @@ AVFilter avfilter_vsink_buffersink = {
 
 static void filter_samples(AVFilterLink *link, AVFilterBufferRef *samplesref)
 {
-    end_frame(link);
+    AVFilterContext *ctx = link->dst;
+    BufferSinkContext *buf = ctx->priv;
+    int inpos = 0;
+
+    if (!buf->frame_size) {
+        add_frame(ctx, samplesref);
+        return;
+    }
+    while (!ff_audioframe_feed(buf->frame_size, link, NULL, AV_PERM_READ,
+                               samplesref, &buf->partial_buf, &inpos))
+        flush_partial_frame(ctx);
+    avfilter_unref_buffer(samplesref);
+    return;
+}
+
+static int flush_samples(AVFilterLink *link)
+{
+    AVFilterContext *ctx = link->dst;
+    BufferSinkContext *buf = ctx->priv;
+
+    if (!buf->partial_buf)
+        return AVERROR_EOF;
+    flush_partial_frame(ctx);
+    return 0;
 }
 
 static av_cold int asink_init(AVFilterContext *ctx, const char *args)
@@ -277,6 +335,7 @@ static av_cold void asink_uninit(AVFilterContext *ctx)
 
     av_freep(&buf->sample_fmts);
     av_freep(&buf->channel_layouts);
+    avfilter_unref_bufferp(&buf->partial_buf);
     common_uninit(ctx);
 }
 
@@ -312,6 +371,7 @@ AVFilter avfilter_asink_abuffersink = {
     .inputs    = (const AVFilterPad[]) {{ .name     = "default",
                                     .type           = AVMEDIA_TYPE_AUDIO,
                                     .filter_samples = filter_samples,
+                                    .flush          = flush_samples,
                                     .min_perms      = AV_PERM_READ, },
                                   { .name = NULL }},
     .outputs   = (const AVFilterPad[]) {{ .name = NULL }},
-- 
1.7.10



More information about the ffmpeg-devel mailing list