annotate ffmpeg/libavutil/buffer.c @ 13:844d341cf643 tip

Back up before ISMIR
author Yading Song <yading.song@eecs.qmul.ac.uk>
date Thu, 31 Oct 2013 13:17:06 +0000
parents f445c3017523
children
rev   line source
yading@11 1 /*
yading@11 2 * This file is part of FFmpeg.
yading@11 3 *
yading@11 4 * FFmpeg is free software; you can redistribute it and/or
yading@11 5 * modify it under the terms of the GNU Lesser General Public
yading@11 6 * License as published by the Free Software Foundation; either
yading@11 7 * version 2.1 of the License, or (at your option) any later version.
yading@11 8 *
yading@11 9 * FFmpeg is distributed in the hope that it will be useful,
yading@11 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
yading@11 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
yading@11 12 * Lesser General Public License for more details.
yading@11 13 *
yading@11 14 * You should have received a copy of the GNU Lesser General Public
yading@11 15 * License along with FFmpeg; if not, write to the Free Software
yading@11 16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
yading@11 17 */
yading@11 18
yading@11 19 #include <stdint.h>
yading@11 20 #include <string.h>
yading@11 21
yading@11 22 #include "atomic.h"
yading@11 23 #include "buffer_internal.h"
yading@11 24 #include "common.h"
yading@11 25 #include "mem.h"
yading@11 26
yading@11 27 AVBufferRef *av_buffer_create(uint8_t *data, int size,
yading@11 28 void (*free)(void *opaque, uint8_t *data),
yading@11 29 void *opaque, int flags)
yading@11 30 {
yading@11 31 AVBufferRef *ref = NULL;
yading@11 32 AVBuffer *buf = NULL;
yading@11 33
yading@11 34 buf = av_mallocz(sizeof(*buf));
yading@11 35 if (!buf)
yading@11 36 return NULL;
yading@11 37
yading@11 38 buf->data = data;
yading@11 39 buf->size = size;
yading@11 40 buf->free = free ? free : av_buffer_default_free;
yading@11 41 buf->opaque = opaque;
yading@11 42 buf->refcount = 1;
yading@11 43
yading@11 44 if (flags & AV_BUFFER_FLAG_READONLY)
yading@11 45 buf->flags |= BUFFER_FLAG_READONLY;
yading@11 46
yading@11 47 ref = av_mallocz(sizeof(*ref));
yading@11 48 if (!ref) {
yading@11 49 av_freep(&buf);
yading@11 50 return NULL;
yading@11 51 }
yading@11 52
yading@11 53 ref->buffer = buf;
yading@11 54 ref->data = data;
yading@11 55 ref->size = size;
yading@11 56
yading@11 57 return ref;
yading@11 58 }
yading@11 59
yading@11 60 void av_buffer_default_free(void *opaque, uint8_t *data)
yading@11 61 {
yading@11 62 av_free(data);
yading@11 63 }
yading@11 64
yading@11 65 AVBufferRef *av_buffer_alloc(int size)
yading@11 66 {
yading@11 67 AVBufferRef *ret = NULL;
yading@11 68 uint8_t *data = NULL;
yading@11 69
yading@11 70 data = av_malloc(size);
yading@11 71 if (!data)
yading@11 72 return NULL;
yading@11 73
yading@11 74 ret = av_buffer_create(data, size, av_buffer_default_free, NULL, 0);
yading@11 75 if (!ret)
yading@11 76 av_freep(&data);
yading@11 77
yading@11 78 return ret;
yading@11 79 }
yading@11 80
yading@11 81 AVBufferRef *av_buffer_allocz(int size)
yading@11 82 {
yading@11 83 AVBufferRef *ret = av_buffer_alloc(size);
yading@11 84 if (!ret)
yading@11 85 return NULL;
yading@11 86
yading@11 87 memset(ret->data, 0, size);
yading@11 88 return ret;
yading@11 89 }
yading@11 90
yading@11 91 AVBufferRef *av_buffer_ref(AVBufferRef *buf)
yading@11 92 {
yading@11 93 AVBufferRef *ret = av_mallocz(sizeof(*ret));
yading@11 94
yading@11 95 if (!ret)
yading@11 96 return NULL;
yading@11 97
yading@11 98 *ret = *buf;
yading@11 99
yading@11 100 avpriv_atomic_int_add_and_fetch(&buf->buffer->refcount, 1);
yading@11 101
yading@11 102 return ret;
yading@11 103 }
yading@11 104
yading@11 105 void av_buffer_unref(AVBufferRef **buf)
yading@11 106 {
yading@11 107 AVBuffer *b;
yading@11 108
yading@11 109 if (!buf || !*buf)
yading@11 110 return;
yading@11 111 b = (*buf)->buffer;
yading@11 112 av_freep(buf);
yading@11 113
yading@11 114 if (!avpriv_atomic_int_add_and_fetch(&b->refcount, -1)) {
yading@11 115 b->free(b->opaque, b->data);
yading@11 116 av_freep(&b);
yading@11 117 }
yading@11 118 }
yading@11 119
yading@11 120 int av_buffer_is_writable(const AVBufferRef *buf)
yading@11 121 {
yading@11 122 if (buf->buffer->flags & AV_BUFFER_FLAG_READONLY)
yading@11 123 return 0;
yading@11 124
yading@11 125 return avpriv_atomic_int_get(&buf->buffer->refcount) == 1;
yading@11 126 }
yading@11 127
yading@11 128 void *av_buffer_get_opaque(const AVBufferRef *buf)
yading@11 129 {
yading@11 130 return buf->buffer->opaque;
yading@11 131 }
yading@11 132
yading@11 133 int av_buffer_get_ref_count(const AVBufferRef *buf)
yading@11 134 {
yading@11 135 return buf->buffer->refcount;
yading@11 136 }
yading@11 137
yading@11 138 int av_buffer_make_writable(AVBufferRef **pbuf)
yading@11 139 {
yading@11 140 AVBufferRef *newbuf, *buf = *pbuf;
yading@11 141
yading@11 142 if (av_buffer_is_writable(buf))
yading@11 143 return 0;
yading@11 144
yading@11 145 newbuf = av_buffer_alloc(buf->size);
yading@11 146 if (!newbuf)
yading@11 147 return AVERROR(ENOMEM);
yading@11 148
yading@11 149 memcpy(newbuf->data, buf->data, buf->size);
yading@11 150 av_buffer_unref(pbuf);
yading@11 151 *pbuf = newbuf;
yading@11 152
yading@11 153 return 0;
yading@11 154 }
yading@11 155
yading@11 156 int av_buffer_realloc(AVBufferRef **pbuf, int size)
yading@11 157 {
yading@11 158 AVBufferRef *buf = *pbuf;
yading@11 159 uint8_t *tmp;
yading@11 160
yading@11 161 if (!buf) {
yading@11 162 /* allocate a new buffer with av_realloc(), so it will be reallocatable
yading@11 163 * later */
yading@11 164 uint8_t *data = av_realloc(NULL, size);
yading@11 165 if (!data)
yading@11 166 return AVERROR(ENOMEM);
yading@11 167
yading@11 168 buf = av_buffer_create(data, size, av_buffer_default_free, NULL, 0);
yading@11 169 if (!buf) {
yading@11 170 av_freep(&data);
yading@11 171 return AVERROR(ENOMEM);
yading@11 172 }
yading@11 173
yading@11 174 buf->buffer->flags |= BUFFER_FLAG_REALLOCATABLE;
yading@11 175 *pbuf = buf;
yading@11 176
yading@11 177 return 0;
yading@11 178 } else if (buf->size == size)
yading@11 179 return 0;
yading@11 180
yading@11 181 if (!(buf->buffer->flags & BUFFER_FLAG_REALLOCATABLE) ||
yading@11 182 !av_buffer_is_writable(buf)) {
yading@11 183 /* cannot realloc, allocate a new reallocable buffer and copy data */
yading@11 184 AVBufferRef *new = NULL;
yading@11 185
yading@11 186 av_buffer_realloc(&new, size);
yading@11 187 if (!new)
yading@11 188 return AVERROR(ENOMEM);
yading@11 189
yading@11 190 memcpy(new->data, buf->data, FFMIN(size, buf->size));
yading@11 191
yading@11 192 av_buffer_unref(pbuf);
yading@11 193 *pbuf = new;
yading@11 194 return 0;
yading@11 195 }
yading@11 196
yading@11 197 tmp = av_realloc(buf->buffer->data, size);
yading@11 198 if (!tmp)
yading@11 199 return AVERROR(ENOMEM);
yading@11 200
yading@11 201 buf->buffer->data = buf->data = tmp;
yading@11 202 buf->buffer->size = buf->size = size;
yading@11 203 return 0;
yading@11 204 }
yading@11 205
yading@11 206 AVBufferPool *av_buffer_pool_init(int size, AVBufferRef* (*alloc)(int size))
yading@11 207 {
yading@11 208 AVBufferPool *pool = av_mallocz(sizeof(*pool));
yading@11 209 if (!pool)
yading@11 210 return NULL;
yading@11 211
yading@11 212 pool->size = size;
yading@11 213 pool->alloc = alloc ? alloc : av_buffer_alloc;
yading@11 214
yading@11 215 avpriv_atomic_int_set(&pool->refcount, 1);
yading@11 216
yading@11 217 return pool;
yading@11 218 }
yading@11 219
yading@11 220 /*
yading@11 221 * This function gets called when the pool has been uninited and
yading@11 222 * all the buffers returned to it.
yading@11 223 */
yading@11 224 static void buffer_pool_free(AVBufferPool *pool)
yading@11 225 {
yading@11 226 while (pool->pool) {
yading@11 227 BufferPoolEntry *buf = pool->pool;
yading@11 228 pool->pool = buf->next;
yading@11 229
yading@11 230 buf->free(buf->opaque, buf->data);
yading@11 231 av_freep(&buf);
yading@11 232 }
yading@11 233 av_freep(&pool);
yading@11 234 }
yading@11 235
yading@11 236 void av_buffer_pool_uninit(AVBufferPool **ppool)
yading@11 237 {
yading@11 238 AVBufferPool *pool;
yading@11 239
yading@11 240 if (!ppool || !*ppool)
yading@11 241 return;
yading@11 242 pool = *ppool;
yading@11 243 *ppool = NULL;
yading@11 244
yading@11 245 if (!avpriv_atomic_int_add_and_fetch(&pool->refcount, -1))
yading@11 246 buffer_pool_free(pool);
yading@11 247 }
yading@11 248
yading@11 249 /* remove the whole buffer list from the pool and return it */
yading@11 250 static BufferPoolEntry *get_pool(AVBufferPool *pool)
yading@11 251 {
yading@11 252 BufferPoolEntry *cur = *(void * volatile *)&pool->pool, *last = NULL;
yading@11 253
yading@11 254 while (cur != last) {
yading@11 255 last = cur;
yading@11 256 cur = avpriv_atomic_ptr_cas((void * volatile *)&pool->pool, last, NULL);
yading@11 257 if (!cur)
yading@11 258 return NULL;
yading@11 259 }
yading@11 260
yading@11 261 return cur;
yading@11 262 }
yading@11 263
yading@11 264 static void add_to_pool(BufferPoolEntry *buf)
yading@11 265 {
yading@11 266 AVBufferPool *pool;
yading@11 267 BufferPoolEntry *cur, *end = buf;
yading@11 268
yading@11 269 if (!buf)
yading@11 270 return;
yading@11 271 pool = buf->pool;
yading@11 272
yading@11 273 while (end->next)
yading@11 274 end = end->next;
yading@11 275
yading@11 276 while (avpriv_atomic_ptr_cas((void * volatile *)&pool->pool, NULL, buf)) {
yading@11 277 /* pool is not empty, retrieve it and append it to our list */
yading@11 278 cur = get_pool(pool);
yading@11 279 end->next = cur;
yading@11 280 while (end->next)
yading@11 281 end = end->next;
yading@11 282 }
yading@11 283 }
yading@11 284
yading@11 285 static void pool_release_buffer(void *opaque, uint8_t *data)
yading@11 286 {
yading@11 287 BufferPoolEntry *buf = opaque;
yading@11 288 AVBufferPool *pool = buf->pool;
yading@11 289
yading@11 290 if(CONFIG_MEMORY_POISONING)
yading@11 291 memset(buf->data, 0x2a, pool->size);
yading@11 292
yading@11 293 add_to_pool(buf);
yading@11 294 if (!avpriv_atomic_int_add_and_fetch(&pool->refcount, -1))
yading@11 295 buffer_pool_free(pool);
yading@11 296 }
yading@11 297
yading@11 298 /* allocate a new buffer and override its free() callback so that
yading@11 299 * it is returned to the pool on free */
yading@11 300 static AVBufferRef *pool_alloc_buffer(AVBufferPool *pool)
yading@11 301 {
yading@11 302 BufferPoolEntry *buf;
yading@11 303 AVBufferRef *ret;
yading@11 304
yading@11 305 ret = pool->alloc(pool->size);
yading@11 306 if (!ret)
yading@11 307 return NULL;
yading@11 308
yading@11 309 buf = av_mallocz(sizeof(*buf));
yading@11 310 if (!buf) {
yading@11 311 av_buffer_unref(&ret);
yading@11 312 return NULL;
yading@11 313 }
yading@11 314
yading@11 315 buf->data = ret->buffer->data;
yading@11 316 buf->opaque = ret->buffer->opaque;
yading@11 317 buf->free = ret->buffer->free;
yading@11 318 buf->pool = pool;
yading@11 319
yading@11 320 ret->buffer->opaque = buf;
yading@11 321 ret->buffer->free = pool_release_buffer;
yading@11 322
yading@11 323 avpriv_atomic_int_add_and_fetch(&pool->refcount, 1);
yading@11 324 avpriv_atomic_int_add_and_fetch(&pool->nb_allocated, 1);
yading@11 325
yading@11 326 return ret;
yading@11 327 }
yading@11 328
yading@11 329 AVBufferRef *av_buffer_pool_get(AVBufferPool *pool)
yading@11 330 {
yading@11 331 AVBufferRef *ret;
yading@11 332 BufferPoolEntry *buf;
yading@11 333
yading@11 334 /* check whether the pool is empty */
yading@11 335 buf = get_pool(pool);
yading@11 336 if (!buf && pool->refcount <= pool->nb_allocated) {
yading@11 337 av_log(NULL, AV_LOG_DEBUG, "Pool race dectected, spining to avoid overallocation and eventual OOM\n");
yading@11 338 while (!buf && avpriv_atomic_int_get(&pool->refcount) <= avpriv_atomic_int_get(&pool->nb_allocated))
yading@11 339 buf = get_pool(pool);
yading@11 340 }
yading@11 341
yading@11 342 if (!buf)
yading@11 343 return pool_alloc_buffer(pool);
yading@11 344
yading@11 345 /* keep the first entry, return the rest of the list to the pool */
yading@11 346 add_to_pool(buf->next);
yading@11 347 buf->next = NULL;
yading@11 348
yading@11 349 ret = av_buffer_create(buf->data, pool->size, pool_release_buffer,
yading@11 350 buf, 0);
yading@11 351 if (!ret) {
yading@11 352 add_to_pool(buf);
yading@11 353 return NULL;
yading@11 354 }
yading@11 355 avpriv_atomic_int_add_and_fetch(&pool->refcount, 1);
yading@11 356
yading@11 357 return ret;
yading@11 358 }