[008/250] lavu: add a new API for reference-counted data buffers.

Message ID 1354401337-7312-9-git-send-email-anton@khirnov.net
State RFC
Headers show

Commit Message

Anton Khirnov Dec. 1, 2012, 10:31 p.m.
---
 libavutil/Makefile          |    2 +
 libavutil/buffer.c          |  174 +++++++++++++++++++++++++++++++++++++++++++
 libavutil/buffer.h          |  134 +++++++++++++++++++++++++++++++++
 libavutil/buffer_internal.h |   46 ++++++++++++
 4 files changed, 356 insertions(+)
 create mode 100644 libavutil/buffer.c
 create mode 100644 libavutil/buffer.h
 create mode 100644 libavutil/buffer_internal.h

Comments

Luca Barbato Dec. 2, 2012, 7:21 p.m. | #1
On 12/01/2012 11:31 PM, Anton Khirnov wrote:
> ---

> +AVBufferRef *av_buffer_make_writable(AVBufferRef **pbuf)

I wonder if it can't be fold in a av_buffer_ref variant. The rest looks
ok. (bikesched on av_foo vs avfoo namespaces might be resolved and we
could go and make a huge-unifomation patchset for libav10)

lu

Patch

diff --git a/libavutil/Makefile b/libavutil/Makefile
index 48a0e16..f9e3794 100644
--- a/libavutil/Makefile
+++ b/libavutil/Makefile
@@ -10,6 +10,7 @@  HEADERS = adler32.h                                                     \
           avutil.h                                                      \
           base64.h                                                      \
           blowfish.h                                                    \
+          buffer.h                                                      \
           bswap.h                                                       \
           channel_layout.h                                              \
           common.h                                                      \
@@ -57,6 +58,7 @@  OBJS = adler32.o                                                        \
        avstring.o                                                       \
        base64.o                                                         \
        blowfish.o                                                       \
+       buffer.o                                                         \
        channel_layout.o                                                 \
        cpu.o                                                            \
        crc.o                                                            \
diff --git a/libavutil/buffer.c b/libavutil/buffer.c
new file mode 100644
index 0000000..394a84e
--- /dev/null
+++ b/libavutil/buffer.c
@@ -0,0 +1,174 @@ 
+/*
+ * This file is part of Libav.
+ *
+ * Libav is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * Libav is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with Libav; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include <stdint.h>
+#include <string.h>
+
+#include "buffer_internal.h"
+#include "common.h"
+#include "mem.h"
+
+AVBufferRef *av_buffer_create(uint8_t *data, int size,
+                              void (*free)(void *opaque, uint8_t *data),
+                              void *opaque)
+{
+    AVBufferRef *ref = NULL;
+    AVBuffer    *buf = NULL;
+
+    buf = av_mallocz(sizeof(*buf));
+    if (!buf)
+        return NULL;
+
+    buf->data     = data;
+    buf->size     = size;
+    buf->free     = free;
+    buf->opaque   = opaque;
+    buf->refcount = 1;
+
+    ref = av_mallocz(sizeof(*ref));
+    if (!ref)
+        goto fail;
+
+    ref->buffer = buf;
+    ref->data   = data;
+    ref->size   = size;
+
+    return ref;
+
+fail:
+    av_freep(&buf);
+    return NULL;
+}
+
+void av_buffer_default_free(void *opaque, uint8_t *data)
+{
+    av_free(data);
+}
+
+AVBufferRef *av_buffer_alloc(int size)
+{
+    AVBufferRef *ret = NULL;
+    uint8_t    *data = NULL;
+
+    data = av_malloc(size);
+    if (!data)
+        return NULL;
+
+    ret = av_buffer_create(data, size, av_buffer_default_free, NULL);
+    if (!ret)
+        av_freep(&data);
+
+    return ret;
+}
+
+AVBufferRef *av_buffer_ref(const AVBufferRef *buf)
+{
+    AVBufferRef *ret = av_mallocz(sizeof(*ret));
+
+    if (!ret)
+        return NULL;
+
+    *ret = *buf;
+
+    __sync_add_and_fetch(&buf->buffer->refcount, 1);
+
+    return ret;
+}
+
+void av_buffer_unref(AVBufferRef **buf)
+{
+    AVBuffer *b;
+
+    if (!buf || !*buf)
+        return;
+    b = (*buf)->buffer;
+    av_freep(buf);
+
+    if (!__sync_sub_and_fetch(&b->refcount, 1)) {
+        b->free(b->opaque, b->data);
+        av_freep(&b);
+        return;
+    }
+}
+
+int av_buffer_is_writable(const AVBufferRef *buf)
+{
+    return buf->buffer->refcount == 1;
+}
+
+AVBufferRef *av_buffer_make_writable(AVBufferRef **pbuf)
+{
+    AVBufferRef *buf = *pbuf;
+    int writable = av_buffer_is_writable(buf);
+    uint8_t *data = NULL;
+    int size = buf->size;
+
+    if (writable)
+        return buf;
+
+    data = av_malloc(size);
+    if (!data) {
+        av_buffer_unref(pbuf);
+        return NULL;
+    }
+
+    memcpy(data, buf->data, size);
+    av_buffer_unref(pbuf);
+
+    *pbuf = av_buffer_create(data, size, av_buffer_default_free, NULL);
+    if (!*pbuf)
+        av_freep(&data);
+    return *pbuf;
+}
+
+AVBufferRef *av_buffer_realloc(AVBufferRef **pbuf, int size)
+{
+    AVBufferRef *buf = *pbuf;
+    uint8_t *tmp;
+
+    if (!buf) {
+        *pbuf = av_buffer_alloc(size);
+        return *pbuf;
+    } else if (buf->size == size)
+        return buf;
+
+    if (buf->buffer->free != av_buffer_default_free || !av_buffer_is_writable(buf)) {
+        AVBufferRef *new = av_buffer_alloc(size);
+
+        if (!new)
+            goto fail;
+
+        memcpy(new->data, buf->data, FFMIN(size, buf->size));
+
+        av_buffer_unref(pbuf);
+        *pbuf = new;
+        return new;
+    }
+
+    tmp = av_realloc(buf->buffer->data, size);
+    if (!tmp)
+        goto fail;
+
+    buf->buffer->data = buf->data = tmp;
+    buf->buffer->size = buf->size = size;
+    return buf;
+fail:
+    av_buffer_unref(pbuf);
+    return NULL;
+
+}
diff --git a/libavutil/buffer.h b/libavutil/buffer.h
new file mode 100644
index 0000000..5160d90
--- /dev/null
+++ b/libavutil/buffer.h
@@ -0,0 +1,134 @@ 
+/*
+ * This file is part of Libav.
+ *
+ * Libav is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * Libav is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with Libav; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+/**
+ * @file
+ * refcounted data buffer API
+ */
+
+#ifndef AVUTIL_BUFFER_H
+#define AVUTIL_BUFFER_H
+
+#include <stdint.h>
+
+/**
+ * A reference counted buffer type. It is opaque and is meant to be used through
+ * references (AVBufferRef).
+ */
+typedef struct AVBuffer AVBuffer;
+
+/**
+ * A reference to a data buffer.
+ *
+ * The size of this struct is not a part of the public ABI and it is not meant
+ * to be allocated directly. Use av_buffer_alloc() / av_buffer_create() to get
+ * an AVBufferRef.
+ */
+typedef struct AVBufferRef {
+    AVBuffer *buffer;
+
+    /**
+     * The data buffer. It is considered writable if and only if
+     * this is the only reference to buffer, in which case
+     * av_buffer_is_writable() returns 1.
+     */
+    uint8_t *data;
+    /**
+     * Size of data in bytes.
+     */
+    int      size;
+} AVBufferRef;
+
+/**
+ * Allocate an AVBuffer of the given size using av_malloc().
+ *
+ * @return an AVBufferRef of given size or NULL when out of memory
+ */
+AVBufferRef *av_buffer_alloc(int size);
+
+/**
+ * Create an AVBuffer from an existing array.
+ *
+ * @param data data array.
+ * If this function is successful, data is owned by the AVBuffer. The caller may
+ * only access data through the returned AVBufferRef and references derived from
+ * it.
+ * If this function fails, data is not touched in any way.
+ * @param size size of data in bytes
+ * @param free a callback for freeing data
+ * @param opaque parameter to be passed to free
+ * @return an AVBufferRef referring to data on success, NULL on failure.
+ */
+AVBufferRef *av_buffer_create(uint8_t *data, int size,
+                              void (*free)(void *opaque, uint8_t *data),
+                              void *opaque);
+
+/**
+ * Default free callback, which calls av_free() on the buffer data.
+ * This function is meant to be passed to av_buffer_create(), do not call it
+ * directly.
+ */
+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_buffer_ref(const AVBufferRef *buf);
+
+/**
+ * Free a given reference and automatically free the buffer if there are no more
+ * references to it.
+ *
+ * @param buf buffer reference to be freed. The pointer is set to NULL on return
+ */
+void av_buffer_unref(AVBufferRef **buf);
+
+/**
+ * @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.
+ * A positive answer is valid until av_buffer_ref() is called on buf.
+ */
+int av_buffer_is_writable(const AVBufferRef *buf);
+
+/**
+ * Create a writable reference from a given buffer reference, avoiding data copy
+ * if possible.
+ *
+ * @param buf buffer reference to make writable. On success, buf is either left
+ * untouched, or it is unreferenced and a new writable AVBufferRef is written in
+ * its place. On failure, buf is unreferenced and set to NULL.
+ * @return a writable buffer reference (equal to *pbuf) or NULL.
+ */
+AVBufferRef *av_buffer_make_writable(AVBufferRef **buf);
+
+/**
+ * Reallocate a given buffer.
+ *
+ * @param pbuf a buffer reference to reallocate. On success, pbuf will be
+ * unreferenced and a new reference with the required size will be written in
+ * its place. On failure pbuf will be unreferenced and set to NULL.
+ * @param size required new buffer size.
+ * @return a new buffer reference (equal to *pbuf) or NULL.
+ */
+AVBufferRef *av_buffer_realloc(AVBufferRef **pbuf, int size);
+
+#endif /* AVUTIL_BUFFER_H */
diff --git a/libavutil/buffer_internal.h b/libavutil/buffer_internal.h
new file mode 100644
index 0000000..ae866ee
--- /dev/null
+++ b/libavutil/buffer_internal.h
@@ -0,0 +1,46 @@ 
+/*
+ * This file is part of Libav.
+ *
+ * Libav is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * Libav is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with Libav; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#ifndef AVUTIL_BUFFER_INTERNAL_H
+#define AVUTIL_BUFFER_INTERNAL_H
+
+#include <stdint.h>
+
+#include "buffer.h"
+
+struct AVBuffer {
+    uint8_t *data; /**< data described by this buffer */
+    int      size; /**< size of data in bytes */
+
+    /**
+     *  number of existing AVBufferRef instances referring to this buffer
+     */
+    int refcount;
+
+    /**
+     * a callback for freeing the data
+     */
+    void (*free)(void *opaque, uint8_t *data);
+
+    /**
+     * an opaque pointer, to be used by the freeing callback
+     */
+    void *opaque;
+};
+
+#endif /* AVUTIL_BUFFER_INTERNAL_H */