[FFmpeg-devel] [PATCH] Implement avfilter_get_audio_buffer_ref_from_buffer().

Stefano Sabatini stefano.sabatini-lala
Sat Jan 15 02:27:19 CET 2011


---
 libavfilter/avfilter.c |   53 ++++++++++++++++++++++++++++++++++++++++++++++++
 libavfilter/avfilter.h |   17 +++++++++++++++
 2 files changed, 70 insertions(+), 0 deletions(-)

diff --git a/libavfilter/avfilter.c b/libavfilter/avfilter.c
index 6ecd4ce..9812610 100644
--- a/libavfilter/avfilter.c
+++ b/libavfilter/avfilter.c
@@ -23,6 +23,7 @@
 
 #include "libavutil/pixdesc.h"
 #include "libavutil/rational.h"
+#include "libavcore/audioconvert.h"
 #include "libavcore/imgutils.h"
 #include "avfilter.h"
 #include "internal.h"
@@ -333,6 +334,58 @@ AVFilterBufferRef *avfilter_get_audio_buffer(AVFilterLink *link, int perms,
     return ret;
 }
 
+AVFilterBufferRef *
+avfilter_get_audio_buffer_ref_from_buffer(uint8_t *buf, int buf_size, int perms,
+                                          enum AVSampleFormat sample_fmt,
+                                          int planar,
+                                          int64_t channel_layout, int nb_channels)
+{
+    AVFilterBuffer *samples = av_mallocz(sizeof(AVFilterBuffer));
+    AVFilterBufferRef *samplesref = av_mallocz(sizeof(AVFilterBufferRef));
+    int nb_samples, linesize[8];
+    uint8_t *data[8];
+
+    if (!samples || !samplesref)
+        goto fail;
+
+    samplesref->buf = samples;
+    samplesref->buf->free = ff_avfilter_default_free_buffer;
+    if (!(samplesref->audio = av_mallocz(sizeof(AVFilterBufferRefAudioProps))))
+        goto fail;
+
+    samplesref->audio->size           = buf_size;
+    samplesref->audio->channel_layout = channel_layout;
+    samplesref->audio->planar         = planar;
+
+    if ((nb_samples = av_samples_fill_arrays(data, linesize, buf, buf_size,
+                                             sample_fmt, planar,
+                                             channel_layout, nb_channels)) < 0)
+        goto fail;
+
+    samplesref->audio->samples_nb = nb_samples;
+
+    /* make sure the buffer gets read permission or it's useless for output */
+    samplesref->perms = perms | AV_PERM_READ;
+
+    samples->refcount = 1;
+    samplesref->type = AVMEDIA_TYPE_AUDIO;
+    samplesref->format = sample_fmt;
+
+    memcpy(samples->data,        data,              sizeof(samples->data));
+    memcpy(samples->linesize,    linesize,          sizeof(samples->linesize));
+    memcpy(samplesref->data,     samples->data,     sizeof(samplesref->data));
+    memcpy(samplesref->linesize, samples->linesize, sizeof(samplesref->linesize));
+
+    return samplesref;
+
+fail:
+    if (samplesref && samplesref->audio)
+        av_free(samplesref->audio);
+    av_free(samplesref);
+    av_free(samples);
+    return NULL;
+}
+
 int avfilter_request_frame(AVFilterLink *link)
 {
     FF_DPRINTF_START(NULL, request_frame); ff_dprintf_link(NULL, link, 1);
diff --git a/libavfilter/avfilter.h b/libavfilter/avfilter.h
index a3c51e0..15d440a 100644
--- a/libavfilter/avfilter.h
+++ b/libavfilter/avfilter.h
@@ -686,6 +686,23 @@ AVFilterBufferRef *avfilter_get_audio_buffer(AVFilterLink *link, int perms,
                                              int64_t channel_layout, int planar);
 
 /**
+ * Create a buffer reference wrapped around an already allocated samples
+ * buffer.
+ *
+ * @param buf pointers to the samples buffer
+ * @param buf_size size in bytes of buf, must contain an integer number of samples
+ * @param perms the required access permissions
+ * @param sample_fmt     the format of each sample in the buffer to allocate
+ * @param planar         audio data layout - planar or packed
+ * @param channel_layout the channel layout of the buffer, <=0 if unknown
+ * @param nb_channels the number of channels, <= 0 if unknown
+ */
+AVFilterBufferRef *
+avfilter_get_audio_buffer_ref_from_buffer(uint8_t *buf, int buf_size, int perms,
+                                          enum AVSampleFormat sample_fmt, int planar,
+                                          int64_t channel_layout, int nb_channels);
+
+/**
  * Request an input frame from the filter at the other end of the link.
  *
  * @param link the input link
-- 
1.7.2.3




More information about the ffmpeg-devel mailing list