[FFmpeg-devel] [PATCH 5/6] sink_buffer: implement fixed frame size.
Nicolas George
nicolas.george at normalesup.org
Mon Jun 25 00:35:59 CEST 2012
Work in progress: currently, only formats where samples are packet at 8
bytes per sample work, and the padding of the last frame is not cleared.
Signed-off-by: Nicolas George <nicolas.george at normalesup.org>
---
libavfilter/buffersink.h | 9 +++++
libavfilter/sink_buffer.c | 86 ++++++++++++++++++++++++++++++++++++++++-----
2 files changed, 87 insertions(+), 8 deletions(-)
diff --git a/libavfilter/buffersink.h b/libavfilter/buffersink.h
index d92b903..b4991b4 100644
--- a/libavfilter/buffersink.h
+++ b/libavfilter/buffersink.h
@@ -59,6 +59,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 2ee3886..39ae12e 100644
--- a/libavfilter/sink_buffer.c
+++ b/libavfilter/sink_buffer.c
@@ -28,6 +28,7 @@
#include "avfilter.h"
#include "buffersink.h"
#include "internal.h"
+#include "audio.h"
extern AVFilter avfilter_vsink_buffersink;
extern AVFilter avfilter_asink_abuffersink;
@@ -66,6 +67,10 @@ 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 partial_buf_pos; ///< number of samples already present
+ unsigned frame_size; ///< frame size, 0 if unused
} BufferSinkContext;
#define FIFO_INIT_SIZE 8
@@ -97,12 +102,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) {
@@ -115,7 +118,32 @@ static void end_frame(AVFilterLink *inlink)
/* cache frame */
av_fifo_generic_write(buf->fifo,
- &inlink->cur_buf, sizeof(AVFilterBufferRef *), NULL);
+ &frame, sizeof(AVFilterBufferRef *), NULL);
+}
+
+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;
+ buf->partial_buf_pos = 0;
}
int av_buffersink_get_buffer_ref(AVFilterContext *ctx,
@@ -132,8 +160,17 @@ 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) {
+ /* FIXME why is it not reached? */
+ if (ret == AVERROR_EOF && buf->frame_size && buf->partial_buf) {
+ av_log(0, 16, "padding last frame\n");
+ flush_partial_frame(ctx);
+ if (!av_fifo_size(buf->fifo))
+ return AVERROR(ENOMEM);
+ } else {
+ return ret;
+ }
+ }
}
if (!av_fifo_size(buf->fifo))
@@ -240,7 +277,39 @@ AVFilter avfilter_vsink_buffersink = {
static void filter_samples(AVFilterLink *link, AVFilterBufferRef *samplesref)
{
- end_frame(link);
+ AVFilterContext *ctx = link->dst;
+ BufferSinkContext *buf = ctx->priv;
+ unsigned insize, inpos = 0, nb_samples;
+ unsigned sample_size = 8; /* TODO (hardcoded for stereo float) */
+
+ if (!buf->frame_size) {
+ add_frame(ctx, samplesref);
+ return;
+ }
+ /* TODO move this piece of code (and the corresponding part to handle
+ EOF) into an inline function in a separate header to make it
+ available for any filter requiring fixed frames. */
+ insize = samplesref->audio->nb_samples;
+ while (insize) {
+ if (!buf->partial_buf) {
+ buf->partial_buf = ff_default_get_audio_buffer(link, AV_PERM_READ,
+ buf->frame_size);
+ if (!buf->partial_buf)
+ break;
+ }
+ nb_samples = FFMIN(insize, buf->frame_size - buf->partial_buf_pos);
+ /* TODO all planes */
+ memcpy(buf->partial_buf->data[0] + buf->partial_buf_pos * sample_size,
+ samplesref ->data[0] + inpos * sample_size,
+ nb_samples * sample_size);
+ insize -= nb_samples;
+ inpos += nb_samples;
+ buf->partial_buf_pos += nb_samples;
+ if (buf->partial_buf_pos == buf->frame_size)
+ flush_partial_frame(ctx);
+ }
+ avfilter_unref_buffer(samplesref);
+ return;
}
static av_cold int asink_init(AVFilterContext *ctx, const char *args, void *opaque)
@@ -273,6 +342,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);
}
--
1.7.10
More information about the ffmpeg-devel
mailing list