You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

309 lines
7.0KB

  1. /*
  2. * This file is part of Libav.
  3. *
  4. * Libav is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * Libav is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with Libav; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include <stdint.h>
  19. #include <string.h>
  20. #include "atomic.h"
  21. #include "buffer_internal.h"
  22. #include "common.h"
  23. #include "mem.h"
  24. #include "thread.h"
  25. AVBufferRef *av_buffer_create(uint8_t *data, int size,
  26. void (*free)(void *opaque, uint8_t *data),
  27. void *opaque, int flags)
  28. {
  29. AVBufferRef *ref = NULL;
  30. AVBuffer *buf = NULL;
  31. buf = av_mallocz(sizeof(*buf));
  32. if (!buf)
  33. return NULL;
  34. buf->data = data;
  35. buf->size = size;
  36. buf->free = free ? free : av_buffer_default_free;
  37. buf->opaque = opaque;
  38. buf->refcount = 1;
  39. if (flags & AV_BUFFER_FLAG_READONLY)
  40. buf->flags |= BUFFER_FLAG_READONLY;
  41. ref = av_mallocz(sizeof(*ref));
  42. if (!ref) {
  43. av_freep(&buf);
  44. return NULL;
  45. }
  46. ref->buffer = buf;
  47. ref->data = data;
  48. ref->size = size;
  49. return ref;
  50. }
  51. void av_buffer_default_free(void *opaque, uint8_t *data)
  52. {
  53. av_free(data);
  54. }
  55. AVBufferRef *av_buffer_alloc(int size)
  56. {
  57. AVBufferRef *ret = NULL;
  58. uint8_t *data = NULL;
  59. data = av_malloc(size);
  60. if (!data)
  61. return NULL;
  62. ret = av_buffer_create(data, size, av_buffer_default_free, NULL, 0);
  63. if (!ret)
  64. av_freep(&data);
  65. return ret;
  66. }
  67. AVBufferRef *av_buffer_allocz(int size)
  68. {
  69. AVBufferRef *ret = av_buffer_alloc(size);
  70. if (!ret)
  71. return NULL;
  72. memset(ret->data, 0, size);
  73. return ret;
  74. }
  75. AVBufferRef *av_buffer_ref(AVBufferRef *buf)
  76. {
  77. AVBufferRef *ret = av_mallocz(sizeof(*ret));
  78. if (!ret)
  79. return NULL;
  80. *ret = *buf;
  81. avpriv_atomic_int_add_and_fetch(&buf->buffer->refcount, 1);
  82. return ret;
  83. }
  84. void av_buffer_unref(AVBufferRef **buf)
  85. {
  86. AVBuffer *b;
  87. if (!buf || !*buf)
  88. return;
  89. b = (*buf)->buffer;
  90. av_freep(buf);
  91. if (!avpriv_atomic_int_add_and_fetch(&b->refcount, -1)) {
  92. b->free(b->opaque, b->data);
  93. av_freep(&b);
  94. }
  95. }
  96. int av_buffer_is_writable(const AVBufferRef *buf)
  97. {
  98. if (buf->buffer->flags & AV_BUFFER_FLAG_READONLY)
  99. return 0;
  100. return avpriv_atomic_int_add_and_fetch(&buf->buffer->refcount, 0) == 1;
  101. }
  102. int av_buffer_make_writable(AVBufferRef **pbuf)
  103. {
  104. AVBufferRef *newbuf, *buf = *pbuf;
  105. if (av_buffer_is_writable(buf))
  106. return 0;
  107. newbuf = av_buffer_alloc(buf->size);
  108. if (!newbuf)
  109. return AVERROR(ENOMEM);
  110. memcpy(newbuf->data, buf->data, buf->size);
  111. av_buffer_unref(pbuf);
  112. *pbuf = newbuf;
  113. return 0;
  114. }
  115. int av_buffer_realloc(AVBufferRef **pbuf, int size)
  116. {
  117. AVBufferRef *buf = *pbuf;
  118. uint8_t *tmp;
  119. if (!buf) {
  120. /* allocate a new buffer with av_realloc(), so it will be reallocatable
  121. * later */
  122. uint8_t *data = av_realloc(NULL, size);
  123. if (!data)
  124. return AVERROR(ENOMEM);
  125. buf = av_buffer_create(data, size, av_buffer_default_free, NULL, 0);
  126. if (!buf) {
  127. av_freep(&data);
  128. return AVERROR(ENOMEM);
  129. }
  130. buf->buffer->flags |= BUFFER_FLAG_REALLOCATABLE;
  131. *pbuf = buf;
  132. return 0;
  133. } else if (buf->size == size)
  134. return 0;
  135. if (!(buf->buffer->flags & BUFFER_FLAG_REALLOCATABLE) ||
  136. !av_buffer_is_writable(buf)) {
  137. /* cannot realloc, allocate a new reallocable buffer and copy data */
  138. AVBufferRef *new = NULL;
  139. av_buffer_realloc(&new, size);
  140. if (!new)
  141. return AVERROR(ENOMEM);
  142. memcpy(new->data, buf->data, FFMIN(size, buf->size));
  143. av_buffer_unref(pbuf);
  144. *pbuf = new;
  145. return 0;
  146. }
  147. tmp = av_realloc(buf->buffer->data, size);
  148. if (!tmp)
  149. return AVERROR(ENOMEM);
  150. buf->buffer->data = buf->data = tmp;
  151. buf->buffer->size = buf->size = size;
  152. return 0;
  153. }
  154. AVBufferPool *av_buffer_pool_init(int size, AVBufferRef* (*alloc)(int size))
  155. {
  156. AVBufferPool *pool = av_mallocz(sizeof(*pool));
  157. if (!pool)
  158. return NULL;
  159. ff_mutex_init(&pool->mutex, NULL);
  160. pool->size = size;
  161. pool->alloc = alloc ? alloc : av_buffer_alloc;
  162. avpriv_atomic_int_set(&pool->refcount, 1);
  163. return pool;
  164. }
  165. /*
  166. * This function gets called when the pool has been uninited and
  167. * all the buffers returned to it.
  168. */
  169. static void buffer_pool_free(AVBufferPool *pool)
  170. {
  171. while (pool->pool) {
  172. BufferPoolEntry *buf = pool->pool;
  173. pool->pool = buf->next;
  174. buf->free(buf->opaque, buf->data);
  175. av_freep(&buf);
  176. }
  177. ff_mutex_destroy(&pool->mutex);
  178. av_freep(&pool);
  179. }
  180. void av_buffer_pool_uninit(AVBufferPool **ppool)
  181. {
  182. AVBufferPool *pool;
  183. if (!ppool || !*ppool)
  184. return;
  185. pool = *ppool;
  186. *ppool = NULL;
  187. if (!avpriv_atomic_int_add_and_fetch(&pool->refcount, -1))
  188. buffer_pool_free(pool);
  189. }
  190. static void pool_release_buffer(void *opaque, uint8_t *data)
  191. {
  192. BufferPoolEntry *buf = opaque;
  193. AVBufferPool *pool = buf->pool;
  194. ff_mutex_lock(&pool->mutex);
  195. buf->next = pool->pool;
  196. pool->pool = buf;
  197. ff_mutex_unlock(&pool->mutex);
  198. if (!avpriv_atomic_int_add_and_fetch(&pool->refcount, -1))
  199. buffer_pool_free(pool);
  200. }
  201. /* allocate a new buffer and override its free() callback so that
  202. * it is returned to the pool on free */
  203. static AVBufferRef *pool_alloc_buffer(AVBufferPool *pool)
  204. {
  205. BufferPoolEntry *buf;
  206. AVBufferRef *ret;
  207. ret = pool->alloc(pool->size);
  208. if (!ret)
  209. return NULL;
  210. buf = av_mallocz(sizeof(*buf));
  211. if (!buf) {
  212. av_buffer_unref(&ret);
  213. return NULL;
  214. }
  215. buf->data = ret->buffer->data;
  216. buf->opaque = ret->buffer->opaque;
  217. buf->free = ret->buffer->free;
  218. buf->pool = pool;
  219. ret->buffer->opaque = buf;
  220. ret->buffer->free = pool_release_buffer;
  221. return ret;
  222. }
  223. AVBufferRef *av_buffer_pool_get(AVBufferPool *pool)
  224. {
  225. AVBufferRef *ret;
  226. BufferPoolEntry *buf;
  227. ff_mutex_lock(&pool->mutex);
  228. buf = pool->pool;
  229. if (buf) {
  230. ret = av_buffer_create(buf->data, pool->size, pool_release_buffer,
  231. buf, 0);
  232. if (ret) {
  233. pool->pool = buf->next;
  234. buf->next = NULL;
  235. }
  236. } else {
  237. ret = pool_alloc_buffer(pool);
  238. }
  239. ff_mutex_unlock(&pool->mutex);
  240. if (ret)
  241. avpriv_atomic_int_add_and_fetch(&pool->refcount, 1);
  242. return ret;
  243. }