[FFmpeg-devel] [PATCH] lavu/buffer: add release function

Lukasz Marek lukasz.m.luki at gmail.com
Sun Feb 23 23:19:23 CET 2014


new function allows to unref buffer and obtain its data.

Signed-off-by: Lukasz Marek <lukasz.m.luki at gmail.com>
---
 libavutil/buffer.c | 26 ++++++++++++++++++++++++++
 libavutil/buffer.h | 12 ++++++++++++
 2 files changed, 38 insertions(+)

diff --git a/libavutil/buffer.c b/libavutil/buffer.c
index e9bf54b..a68b0be 100644
--- a/libavutil/buffer.c
+++ b/libavutil/buffer.c
@@ -117,6 +117,32 @@ void av_buffer_unref(AVBufferRef **buf)
     }
 }
 
+int av_buffer_release(AVBufferRef **buf, uint8_t **data)
+{
+    AVBuffer *b;
+
+    if (!buf || !*buf) {
+        if (data)
+            *data = NULL;
+        return 0;
+    }
+    b = (*buf)->buffer;
+    av_freep(buf);
+
+    if (!avpriv_atomic_int_add_and_fetch(&b->refcount, -1)) {
+        if (data)
+            *data = b->data;
+        else
+            b->free(b->opaque, b->data);
+        av_freep(&b);
+    } else if (data) {
+        *data = av_memdup(b->data, b->size);
+        if (!*data)
+            return AVERROR(ENOMEM);
+    }
+    return 0;
+}
+
 int av_buffer_is_writable(const AVBufferRef *buf)
 {
     if (buf->buffer->flags & AV_BUFFER_FLAG_READONLY)
diff --git a/libavutil/buffer.h b/libavutil/buffer.h
index b4399fd..8cf2cbf 100644
--- a/libavutil/buffer.h
+++ b/libavutil/buffer.h
@@ -155,6 +155,18 @@ AVBufferRef *av_buffer_ref(AVBufferRef *buf);
 void av_buffer_unref(AVBufferRef **buf);
 
 /**
+ * Free a given reference and pass underlaying data to user provided pointer.
+ * If there is more than one reference then data is copied.
+ *
+ * @param buf  the reference to be released. The pointer is set to NULL on return.
+ * @param data pointer to be passed with underlaying data.
+ * @return 0 on success, a negative AVERROR on failure.
+ *
+ * @note on error buffer is properly released and *data is set to NULL.
+ */
+int av_buffer_release(AVBufferRef **buf, uint8_t **data);
+
+/**
  * @return 1 if the caller may write to the data referred to by buf (which is
  * true if and only if buf is the only reference to the underlying AVBuffer).
  * Return 0 otherwise.
-- 
1.8.3.2



More information about the ffmpeg-devel mailing list