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.

335 lines
7.7KB

  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 <stdatomic.h>
  19. #include <stdint.h>
  20. #include <string.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. atomic_init(&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. atomic_fetch_add_explicit(&buf->buffer->refcount, 1, memory_order_relaxed);
  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 (atomic_fetch_add_explicit(&b->refcount, -1, memory_order_acq_rel) == 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 atomic_load(&buf->buffer->refcount) == 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) || buf->data != buf->buffer->data) {
  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_init2(int size, void *opaque,
  155. AVBufferRef* (*alloc)(void *opaque, int size),
  156. void (*pool_free)(void *opaque))
  157. {
  158. AVBufferPool *pool = av_mallocz(sizeof(*pool));
  159. if (!pool)
  160. return NULL;
  161. ff_mutex_init(&pool->mutex, NULL);
  162. pool->size = size;
  163. pool->opaque = opaque;
  164. pool->alloc2 = alloc;
  165. pool->pool_free = pool_free;
  166. atomic_init(&pool->refcount, 1);
  167. return pool;
  168. }
  169. AVBufferPool *av_buffer_pool_init(int size, AVBufferRef* (*alloc)(int size))
  170. {
  171. AVBufferPool *pool = av_mallocz(sizeof(*pool));
  172. if (!pool)
  173. return NULL;
  174. ff_mutex_init(&pool->mutex, NULL);
  175. pool->size = size;
  176. pool->alloc = alloc ? alloc : av_buffer_alloc;
  177. atomic_init(&pool->refcount, 1);
  178. return pool;
  179. }
  180. /*
  181. * This function gets called when the pool has been uninited and
  182. * all the buffers returned to it.
  183. */
  184. static void buffer_pool_free(AVBufferPool *pool)
  185. {
  186. while (pool->pool) {
  187. BufferPoolEntry *buf = pool->pool;
  188. pool->pool = buf->next;
  189. buf->free(buf->opaque, buf->data);
  190. av_freep(&buf);
  191. }
  192. ff_mutex_destroy(&pool->mutex);
  193. if (pool->pool_free)
  194. pool->pool_free(pool->opaque);
  195. av_freep(&pool);
  196. }
  197. void av_buffer_pool_uninit(AVBufferPool **ppool)
  198. {
  199. AVBufferPool *pool;
  200. if (!ppool || !*ppool)
  201. return;
  202. pool = *ppool;
  203. *ppool = NULL;
  204. if (atomic_fetch_add_explicit(&pool->refcount, -1, memory_order_acq_rel) == 1)
  205. buffer_pool_free(pool);
  206. }
  207. static void pool_release_buffer(void *opaque, uint8_t *data)
  208. {
  209. BufferPoolEntry *buf = opaque;
  210. AVBufferPool *pool = buf->pool;
  211. ff_mutex_lock(&pool->mutex);
  212. buf->next = pool->pool;
  213. pool->pool = buf;
  214. ff_mutex_unlock(&pool->mutex);
  215. if (atomic_fetch_add_explicit(&pool->refcount, -1, memory_order_acq_rel) == 1)
  216. buffer_pool_free(pool);
  217. }
  218. /* allocate a new buffer and override its free() callback so that
  219. * it is returned to the pool on free */
  220. static AVBufferRef *pool_alloc_buffer(AVBufferPool *pool)
  221. {
  222. BufferPoolEntry *buf;
  223. AVBufferRef *ret;
  224. ret = pool->alloc2 ? pool->alloc2(pool->opaque, pool->size) :
  225. pool->alloc(pool->size);
  226. if (!ret)
  227. return NULL;
  228. buf = av_mallocz(sizeof(*buf));
  229. if (!buf) {
  230. av_buffer_unref(&ret);
  231. return NULL;
  232. }
  233. buf->data = ret->buffer->data;
  234. buf->opaque = ret->buffer->opaque;
  235. buf->free = ret->buffer->free;
  236. buf->pool = pool;
  237. ret->buffer->opaque = buf;
  238. ret->buffer->free = pool_release_buffer;
  239. return ret;
  240. }
  241. AVBufferRef *av_buffer_pool_get(AVBufferPool *pool)
  242. {
  243. AVBufferRef *ret;
  244. BufferPoolEntry *buf;
  245. ff_mutex_lock(&pool->mutex);
  246. buf = pool->pool;
  247. if (buf) {
  248. ret = av_buffer_create(buf->data, pool->size, pool_release_buffer,
  249. buf, 0);
  250. if (ret) {
  251. pool->pool = buf->next;
  252. buf->next = NULL;
  253. }
  254. } else {
  255. ret = pool_alloc_buffer(pool);
  256. }
  257. ff_mutex_unlock(&pool->mutex);
  258. if (ret)
  259. atomic_fetch_add_explicit(&pool->refcount, 1, memory_order_relaxed);
  260. return ret;
  261. }