diff mbox series

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

Message ID b17a72426c522381cb34ff0c7cd1716458d4374d.1658446783.git.ffmpegagent@gmail.com
State New
Headers show
Series Add derive-device function which searches for existing devices in both directions | expand

Checks

Context Check Description
yinshiyou/make_loongarch64 success Make finished
yinshiyou/make_fate_loongarch64 success Make fate finished
andriy/make_x86 success Make finished
andriy/make_fate_x86 success Make fate finished

Commit Message

softworkz July 21, 2022, 11:39 p.m. UTC
From: softworkz <softworkz@hotmail.com>

This allows to create AVBufferRef from AVBuffer directly.

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

Patch

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.
  *