[FFmpeg-devel] [PATCH v2 1/4] avutil/buffer: add av_ref_from_buffer() function

softworkz ffmpegagent at gmail.com
Sat May 21 17:07:39 EEST 2022


From: softworkz <softworkz at hotmail.com>

This allows to create AVBufferRef from AVBuffer directly.

Signed-off-by: softworkz <softworkz at hotmail.com>
---
 libavutil/buffer.c | 16 ++++++++++++++++
 libavutil/buffer.h |  8 ++++++++
 2 files changed, 24 insertions(+)

diff --git a/libavutil/buffer.c b/libavutil/buffer.c
index 54590be566..f9df0ad6ea 100644
--- a/libavutil/buffer.c
+++ b/libavutil/buffer.c
@@ -100,6 +100,22 @@ AVBufferRef *av_buffer_allocz(size_t size)
     return ret;
 }
 
+AVBufferRef *av_ref_from_buffer(AVBuffer *buf)
+{
+    AVBufferRef *ref = av_mallocz(sizeof(*ref));
+
+    if (!ref)
+        return NULL;
+
+    ref->buffer = buf;
+    ref->data   = buf->data;
+    ref->size   = buf->size;
+
+    atomic_fetch_add_explicit(&buf->refcount, 1, memory_order_relaxed);
+
+    return ref;
+}
+
 AVBufferRef *av_buffer_ref(const AVBufferRef *buf)
 {
     AVBufferRef *ret = av_mallocz(sizeof(*ret));
diff --git a/libavutil/buffer.h b/libavutil/buffer.h
index e1ef5b7f07..491734b9ca 100644
--- a/libavutil/buffer.h
+++ b/libavutil/buffer.h
@@ -139,6 +139,14 @@ AVBufferRef *av_buffer_create(uint8_t *data, size_t size,
  */
 void av_buffer_default_free(void *opaque, uint8_t *data);
 
+/**
+ * Create a new reference to an AVBuffer.
+ *
+ * @return a new AVBufferRef referring to the same AVBuffer as buf or NULL on
+ * failure.
+ */
+AVBufferRef *av_ref_from_buffer(AVBuffer *buf);
+
 /**
  * Create a new reference to an AVBuffer.
  *
-- 
ffmpeg-codebot



More information about the ffmpeg-devel mailing list