Browse code

Merge commit '8e401dbe90cc77b1f3067a917d9fa48cefa3fcdb'

* commit '8e401dbe90cc77b1f3067a917d9fa48cefa3fcdb':
lavu: add a new API for reference-counted data buffers.

Conflicts:
libavutil/Makefile

Merged-by: Michael Niedermayer <michaelni@gmx.at>

Michael Niedermayer authored on 2013/03/09 00:01:00
Showing 4 changed files
... ...
@@ -14,6 +14,7 @@ HEADERS = adler32.h                                                     \
14 14
           blowfish.h                                                    \
15 15
           bprint.h                                                      \
16 16
           bswap.h                                                       \
17
+          buffer.h                                                      \
17 18
           channel_layout.h                                              \
18 19
           common.h                                                      \
19 20
           cpu.h                                                         \
... ...
@@ -65,6 +66,7 @@ OBJS = adler32.o                                                        \
65 65
        base64.o                                                         \
66 66
        blowfish.o                                                       \
67 67
        bprint.o                                                         \
68
+       buffer.o                                                         \
68 69
        channel_layout.o                                                 \
69 70
        cpu.o                                                            \
70 71
        crc.o                                                            \
71 72
new file mode 100644
... ...
@@ -0,0 +1,194 @@
0
+/*
1
+ * This file is part of FFmpeg.
2
+ *
3
+ * FFmpeg is free software; you can redistribute it and/or
4
+ * modify it under the terms of the GNU Lesser General Public
5
+ * License as published by the Free Software Foundation; either
6
+ * version 2.1 of the License, or (at your option) any later version.
7
+ *
8
+ * FFmpeg is distributed in the hope that it will be useful,
9
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11
+ * Lesser General Public License for more details.
12
+ *
13
+ * You should have received a copy of the GNU Lesser General Public
14
+ * License along with FFmpeg; if not, write to the Free Software
15
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
+ */
17
+
18
+#include <stdint.h>
19
+#include <string.h>
20
+
21
+#include "atomic.h"
22
+#include "buffer_internal.h"
23
+#include "common.h"
24
+#include "mem.h"
25
+
26
+AVBufferRef *av_buffer_create(uint8_t *data, int size,
27
+                              void (*free)(void *opaque, uint8_t *data),
28
+                              void *opaque, int flags)
29
+{
30
+    AVBufferRef *ref = NULL;
31
+    AVBuffer    *buf = NULL;
32
+
33
+    buf = av_mallocz(sizeof(*buf));
34
+    if (!buf)
35
+        return NULL;
36
+
37
+    buf->data     = data;
38
+    buf->size     = size;
39
+    buf->free     = free ? free : av_buffer_default_free;
40
+    buf->opaque   = opaque;
41
+    buf->refcount = 1;
42
+
43
+    if (flags & AV_BUFFER_FLAG_READONLY)
44
+        buf->flags |= BUFFER_FLAG_READONLY;
45
+
46
+    ref = av_mallocz(sizeof(*ref));
47
+    if (!ref) {
48
+        av_freep(&buf);
49
+        return NULL;
50
+    }
51
+
52
+    ref->buffer = buf;
53
+    ref->data   = data;
54
+    ref->size   = size;
55
+
56
+    return ref;
57
+}
58
+
59
+void av_buffer_default_free(void *opaque, uint8_t *data)
60
+{
61
+    av_free(data);
62
+}
63
+
64
+AVBufferRef *av_buffer_alloc(int size)
65
+{
66
+    AVBufferRef *ret = NULL;
67
+    uint8_t    *data = NULL;
68
+
69
+    data = av_malloc(size);
70
+    if (!data)
71
+        return NULL;
72
+
73
+    ret = av_buffer_create(data, size, av_buffer_default_free, NULL, 0);
74
+    if (!ret)
75
+        av_freep(&data);
76
+
77
+    return ret;
78
+}
79
+
80
+AVBufferRef *av_buffer_allocz(int size)
81
+{
82
+    AVBufferRef *ret = av_buffer_alloc(size);
83
+    if (!ret)
84
+        return NULL;
85
+
86
+    memset(ret->data, 0, size);
87
+    return ret;
88
+}
89
+
90
+AVBufferRef *av_buffer_ref(AVBufferRef *buf)
91
+{
92
+    AVBufferRef *ret = av_mallocz(sizeof(*ret));
93
+
94
+    if (!ret)
95
+        return NULL;
96
+
97
+    *ret = *buf;
98
+
99
+    avpriv_atomic_int_add_and_fetch(&buf->buffer->refcount, 1);
100
+
101
+    return ret;
102
+}
103
+
104
+void av_buffer_unref(AVBufferRef **buf)
105
+{
106
+    AVBuffer *b;
107
+
108
+    if (!buf || !*buf)
109
+        return;
110
+    b = (*buf)->buffer;
111
+    av_freep(buf);
112
+
113
+    if (!avpriv_atomic_int_add_and_fetch(&b->refcount, -1)) {
114
+        b->free(b->opaque, b->data);
115
+        av_freep(&b);
116
+    }
117
+}
118
+
119
+int av_buffer_is_writable(const AVBufferRef *buf)
120
+{
121
+    if (buf->buffer->flags & AV_BUFFER_FLAG_READONLY)
122
+        return 0;
123
+
124
+    return avpriv_atomic_int_add_and_fetch(&buf->buffer->refcount, 0) == 1;
125
+}
126
+
127
+int av_buffer_make_writable(AVBufferRef **pbuf)
128
+{
129
+    AVBufferRef *newbuf, *buf = *pbuf;
130
+
131
+    if (av_buffer_is_writable(buf))
132
+        return 0;
133
+
134
+    newbuf = av_buffer_alloc(buf->size);
135
+    if (!newbuf)
136
+        return AVERROR(ENOMEM);
137
+
138
+    memcpy(newbuf->data, buf->data, buf->size);
139
+    av_buffer_unref(pbuf);
140
+    *pbuf = newbuf;
141
+
142
+    return 0;
143
+}
144
+
145
+int av_buffer_realloc(AVBufferRef **pbuf, int size)
146
+{
147
+    AVBufferRef *buf = *pbuf;
148
+    uint8_t *tmp;
149
+
150
+    if (!buf) {
151
+        /* allocate a new buffer with av_realloc(), so it will be reallocatable
152
+         * later */
153
+        uint8_t *data = av_realloc(NULL, size);
154
+        if (!data)
155
+            return AVERROR(ENOMEM);
156
+
157
+        buf = av_buffer_create(data, size, av_buffer_default_free, NULL, 0);
158
+        if (!buf) {
159
+            av_freep(&data);
160
+            return AVERROR(ENOMEM);
161
+        }
162
+
163
+        buf->buffer->flags |= BUFFER_FLAG_REALLOCATABLE;
164
+        *pbuf = buf;
165
+
166
+        return 0;
167
+    } else if (buf->size == size)
168
+        return 0;
169
+
170
+    if (!(buf->buffer->flags & BUFFER_FLAG_REALLOCATABLE) ||
171
+        !av_buffer_is_writable(buf)) {
172
+        /* cannot realloc, allocate a new reallocable buffer and copy data */
173
+        AVBufferRef *new = NULL;
174
+
175
+        av_buffer_realloc(&new, size);
176
+        if (!new)
177
+            return AVERROR(ENOMEM);
178
+
179
+        memcpy(new->data, buf->data, FFMIN(size, buf->size));
180
+
181
+        av_buffer_unref(pbuf);
182
+        *pbuf = new;
183
+        return 0;
184
+    }
185
+
186
+    tmp = av_realloc(buf->buffer->data, size);
187
+    if (!tmp)
188
+        return AVERROR(ENOMEM);
189
+
190
+    buf->buffer->data = buf->data = tmp;
191
+    buf->buffer->size = buf->size = size;
192
+    return 0;
193
+}
0 194
new file mode 100644
... ...
@@ -0,0 +1,197 @@
0
+/*
1
+ * This file is part of FFmpeg.
2
+ *
3
+ * FFmpeg is free software; you can redistribute it and/or
4
+ * modify it under the terms of the GNU Lesser General Public
5
+ * License as published by the Free Software Foundation; either
6
+ * version 2.1 of the License, or (at your option) any later version.
7
+ *
8
+ * FFmpeg is distributed in the hope that it will be useful,
9
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11
+ * Lesser General Public License for more details.
12
+ *
13
+ * You should have received a copy of the GNU Lesser General Public
14
+ * License along with FFmpeg; if not, write to the Free Software
15
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
+ */
17
+
18
+/**
19
+ * @file
20
+ * @ingroup lavu_buffer
21
+ * refcounted data buffer API
22
+ */
23
+
24
+#ifndef AVUTIL_BUFFER_H
25
+#define AVUTIL_BUFFER_H
26
+
27
+#include <stdint.h>
28
+
29
+/**
30
+ * @defgroup lavu_buffer AVBuffer
31
+ * @ingroup lavu_data
32
+ *
33
+ * @{
34
+ * AVBuffer is an API for reference-counted data buffers.
35
+ *
36
+ * There are two core objects in this API -- AVBuffer and AVBufferRef. AVBuffer
37
+ * represents the data buffer itself; it is opaque and not meant to be accessed
38
+ * by the caller directly, but only through AVBufferRef. However, the caller may
39
+ * e.g. compare two AVBuffer pointers to check whether two different references
40
+ * are describing the same data buffer. AVBufferRef represents a single
41
+ * reference to an AVBuffer and it is the object that may be manipulated by the
42
+ * caller directly.
43
+ *
44
+ * There are two functions provided for creating a new AVBuffer with a single
45
+ * reference -- av_buffer_alloc() to just allocate a new buffer, and
46
+ * av_buffer_create() to wrap an existing array in an AVBuffer. From an existing
47
+ * reference, additional references may be created with av_buffer_ref().
48
+ * Use av_buffer_unref() to free a reference (this will automatically free the
49
+ * data once all the references are freed).
50
+ *
51
+ * The convention throughout this API and the rest of FFmpeg is such that the
52
+ * buffer is considered writable if there exists only one reference to it (and
53
+ * it has not been marked as read-only). The av_buffer_is_writable() function is
54
+ * provided to check whether this is true and av_buffer_make_writable() will
55
+ * automatically create a new writable buffer when necessary.
56
+ * Of course nothing prevents the calling code from violating this convention,
57
+ * however that is safe only when all the existing references are under its
58
+ * control.
59
+ *
60
+ * @note Referencing and unreferencing the buffers is thread-safe and thus
61
+ * may be done from multiple threads simultaneously without any need for
62
+ * additional locking.
63
+ *
64
+ * @note Two different references to the same buffer can point to different
65
+ * parts of the buffer (i.e. their AVBufferRef.data will not be equal).
66
+ */
67
+
68
+/**
69
+ * A reference counted buffer type. It is opaque and is meant to be used through
70
+ * references (AVBufferRef).
71
+ */
72
+typedef struct AVBuffer AVBuffer;
73
+
74
+/**
75
+ * A reference to a data buffer.
76
+ *
77
+ * The size of this struct is not a part of the public ABI and it is not meant
78
+ * to be allocated directly.
79
+ */
80
+typedef struct AVBufferRef {
81
+    AVBuffer *buffer;
82
+
83
+    /**
84
+     * The data buffer. It is considered writable if and only if
85
+     * this is the only reference to the buffer, in which case
86
+     * av_buffer_is_writable() returns 1.
87
+     */
88
+    uint8_t *data;
89
+    /**
90
+     * Size of data in bytes.
91
+     */
92
+    int      size;
93
+} AVBufferRef;
94
+
95
+/**
96
+ * Allocate an AVBuffer of the given size using av_malloc().
97
+ *
98
+ * @return an AVBufferRef of given size or NULL when out of memory
99
+ */
100
+AVBufferRef *av_buffer_alloc(int size);
101
+
102
+/**
103
+ * Same as av_buffer_alloc(), except the returned buffer will be initialized
104
+ * to zero.
105
+ */
106
+AVBufferRef *av_buffer_allocz(int size);
107
+
108
+/**
109
+ * Always treat the buffer as read-only, even when it has only one
110
+ * reference.
111
+ */
112
+#define AV_BUFFER_FLAG_READONLY (1 << 0)
113
+
114
+/**
115
+ * Create an AVBuffer from an existing array.
116
+ *
117
+ * If this function is successful, data is owned by the AVBuffer. The caller may
118
+ * only access data through the returned AVBufferRef and references derived from
119
+ * it.
120
+ * If this function fails, data is left untouched.
121
+ * @param data   data array
122
+ * @param size   size of data in bytes
123
+ * @param free   a callback for freeing data
124
+ * @param opaque parameter to be passed to free
125
+ * @param flags  a combination of AV_BUFFER_FLAG_*
126
+ *
127
+ * @return an AVBufferRef referring to data on success, NULL on failure.
128
+ */
129
+AVBufferRef *av_buffer_create(uint8_t *data, int size,
130
+                              void (*free)(void *opaque, uint8_t *data),
131
+                              void *opaque, int flags);
132
+
133
+/**
134
+ * Default free callback, which calls av_free() on the buffer data.
135
+ * This function is meant to be passed to av_buffer_create(), not called
136
+ * directly.
137
+ */
138
+void av_buffer_default_free(void *opaque, uint8_t *data);
139
+
140
+/**
141
+ * Create a new reference to an AVBuffer.
142
+ *
143
+ * @return a new AVBufferRef referring to the same AVBuffer as buf or NULL on
144
+ * failure.
145
+ */
146
+AVBufferRef *av_buffer_ref(AVBufferRef *buf);
147
+
148
+/**
149
+ * Free a given reference and automatically free the buffer if there are no more
150
+ * references to it.
151
+ *
152
+ * @param buf the reference to be freed. The pointer is set to NULL on return.
153
+ */
154
+void av_buffer_unref(AVBufferRef **buf);
155
+
156
+/**
157
+ * @return 1 if the caller may write to the data referred to by buf (which is
158
+ * true if and only if buf is the only reference to the underlying AVBuffer).
159
+ * Return 0 otherwise.
160
+ * A positive answer is valid until av_buffer_ref() is called on buf.
161
+ */
162
+int av_buffer_is_writable(const AVBufferRef *buf);
163
+
164
+/**
165
+ * Create a writable reference from a given buffer reference, avoiding data copy
166
+ * if possible.
167
+ *
168
+ * @param buf buffer reference to make writable. On success, buf is either left
169
+ *            untouched, or it is unreferenced and a new writable AVBufferRef is
170
+ *            written in its place. On failure, buf is left untouched.
171
+ * @return 0 on success, a negative AVERROR on failure.
172
+ */
173
+int av_buffer_make_writable(AVBufferRef **buf);
174
+
175
+/**
176
+ * Reallocate a given buffer.
177
+ *
178
+ * @param buf  a buffer reference to reallocate. On success, buf will be
179
+ *             unreferenced and a new reference with the required size will be
180
+ *             written in its place. On failure buf will be left untouched. *buf
181
+ *             may be NULL, then a new buffer is allocated.
182
+ * @param size required new buffer size.
183
+ * @return 0 on success, a negative AVERROR on failure.
184
+ *
185
+ * @note the buffer is actually reallocated with av_realloc() only if it was
186
+ * initially allocated through av_buffer_realloc(NULL) and there is only one
187
+ * reference to it (i.e. the one passed to this function). In all other cases
188
+ * a new buffer is allocated and the data is copied.
189
+ */
190
+int av_buffer_realloc(AVBufferRef **buf, int size);
191
+
192
+/**
193
+ * @}
194
+ */
195
+
196
+#endif /* AVUTIL_BUFFER_H */
0 197
new file mode 100644
... ...
@@ -0,0 +1,60 @@
0
+/*
1
+ * This file is part of FFmpeg.
2
+ *
3
+ * FFmpeg is free software; you can redistribute it and/or
4
+ * modify it under the terms of the GNU Lesser General Public
5
+ * License as published by the Free Software Foundation; either
6
+ * version 2.1 of the License, or (at your option) any later version.
7
+ *
8
+ * FFmpeg is distributed in the hope that it will be useful,
9
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11
+ * Lesser General Public License for more details.
12
+ *
13
+ * You should have received a copy of the GNU Lesser General Public
14
+ * License along with FFmpeg; if not, write to the Free Software
15
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
+ */
17
+
18
+#ifndef AVUTIL_BUFFER_INTERNAL_H
19
+#define AVUTIL_BUFFER_INTERNAL_H
20
+
21
+#include <stdint.h>
22
+
23
+#include "buffer.h"
24
+
25
+/**
26
+ * The buffer is always treated as read-only.
27
+ */
28
+#define BUFFER_FLAG_READONLY      (1 << 0)
29
+/**
30
+ * The buffer was av_realloc()ed, so it is reallocatable.
31
+ */
32
+#define BUFFER_FLAG_REALLOCATABLE (1 << 1)
33
+
34
+struct AVBuffer {
35
+    uint8_t *data; /**< data described by this buffer */
36
+    int      size; /**< size of data in bytes */
37
+
38
+    /**
39
+     *  number of existing AVBufferRef instances referring to this buffer
40
+     */
41
+    volatile int refcount;
42
+
43
+    /**
44
+     * a callback for freeing the data
45
+     */
46
+    void (*free)(void *opaque, uint8_t *data);
47
+
48
+    /**
49
+     * an opaque pointer, to be used by the freeing callback
50
+     */
51
+    void *opaque;
52
+
53
+    /**
54
+     * A combination of BUFFER_FLAG_*
55
+     */
56
+    int flags;
57
+};
58
+
59
+#endif /* AVUTIL_BUFFER_INTERNAL_H */