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.

338 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 <stdint.h>
  19. #include <string.h>
  20. #include "atomic.h"
  21. #include "buffer_internal.h"
  22. #include "common.h"
  23. #include "mem.h"
  24. AVBufferRef *av_buffer_create(uint8_t *data, int size,
  25. void (*free)(void *opaque, uint8_t *data),
  26. void *opaque, int flags)
  27. {
  28. AVBufferRef *ref = NULL;
  29. AVBuffer *buf = NULL;
  30. buf = av_mallocz(sizeof(*buf));
  31. if (!buf)
  32. return NULL;
  33. buf->data = data;
  34. buf->size = size;
  35. buf->free = free ? free : av_buffer_default_free;
  36. buf->opaque = opaque;
  37. buf->refcount = 1;
  38. if (flags & AV_BUFFER_FLAG_READONLY)
  39. buf->flags |= BUFFER_FLAG_READONLY;
  40. ref = av_mallocz(sizeof(*ref));
  41. if (!ref) {
  42. av_freep(&buf);
  43. return NULL;
  44. }
  45. ref->buffer = buf;
  46. ref->data = data;
  47. ref->size = size;
  48. return ref;
  49. }
  50. void av_buffer_default_free(void *opaque, uint8_t *data)
  51. {
  52. av_free(data);
  53. }
  54. AVBufferRef *av_buffer_alloc(int size)
  55. {
  56. AVBufferRef *ret = NULL;
  57. uint8_t *data = NULL;
  58. data = av_malloc(size);
  59. if (!data)
  60. return NULL;
  61. ret = av_buffer_create(data, size, av_buffer_default_free, NULL, 0);
  62. if (!ret)
  63. av_freep(&data);
  64. return ret;
  65. }
  66. AVBufferRef *av_buffer_allocz(int size)
  67. {
  68. AVBufferRef *ret = av_buffer_alloc(size);
  69. if (!ret)
  70. return NULL;
  71. memset(ret->data, 0, size);
  72. return ret;
  73. }
  74. AVBufferRef *av_buffer_ref(AVBufferRef *buf)
  75. {
  76. AVBufferRef *ret = av_mallocz(sizeof(*ret));
  77. if (!ret)
  78. return NULL;
  79. *ret = *buf;
  80. avpriv_atomic_int_add_and_fetch(&buf->buffer->refcount, 1);
  81. return ret;
  82. }
  83. void av_buffer_unref(AVBufferRef **buf)
  84. {
  85. AVBuffer *b;
  86. if (!buf || !*buf)
  87. return;
  88. b = (*buf)->buffer;
  89. av_freep(buf);
  90. if (!avpriv_atomic_int_add_and_fetch(&b->refcount, -1)) {
  91. b->free(b->opaque, b->data);
  92. av_freep(&b);
  93. }
  94. }
  95. int av_buffer_is_writable(const AVBufferRef *buf)
  96. {
  97. if (buf->buffer->flags & AV_BUFFER_FLAG_READONLY)
  98. return 0;
  99. return avpriv_atomic_int_add_and_fetch(&buf->buffer->refcount, 0) == 1;
  100. }
  101. int av_buffer_make_writable(AVBufferRef **pbuf)
  102. {
  103. AVBufferRef *newbuf, *buf = *pbuf;
  104. if (av_buffer_is_writable(buf))
  105. return 0;
  106. newbuf = av_buffer_alloc(buf->size);
  107. if (!newbuf)
  108. return AVERROR(ENOMEM);
  109. memcpy(newbuf->data, buf->data, buf->size);
  110. av_buffer_unref(pbuf);
  111. *pbuf = newbuf;
  112. return 0;
  113. }
  114. int av_buffer_realloc(AVBufferRef **pbuf, int size)
  115. {
  116. AVBufferRef *buf = *pbuf;
  117. uint8_t *tmp;
  118. if (!buf) {
  119. /* allocate a new buffer with av_realloc(), so it will be reallocatable
  120. * later */
  121. uint8_t *data = av_realloc(NULL, size);
  122. if (!data)
  123. return AVERROR(ENOMEM);
  124. buf = av_buffer_create(data, size, av_buffer_default_free, NULL, 0);
  125. if (!buf) {
  126. av_freep(&data);
  127. return AVERROR(ENOMEM);
  128. }
  129. buf->buffer->flags |= BUFFER_FLAG_REALLOCATABLE;
  130. *pbuf = buf;
  131. return 0;
  132. } else if (buf->size == size)
  133. return 0;
  134. if (!(buf->buffer->flags & BUFFER_FLAG_REALLOCATABLE) ||
  135. !av_buffer_is_writable(buf)) {
  136. /* cannot realloc, allocate a new reallocable buffer and copy data */
  137. AVBufferRef *new = NULL;
  138. av_buffer_realloc(&new, size);
  139. if (!new)
  140. return AVERROR(ENOMEM);
  141. memcpy(new->data, buf->data, FFMIN(size, buf->size));
  142. av_buffer_unref(pbuf);
  143. *pbuf = new;
  144. return 0;
  145. }
  146. tmp = av_realloc(buf->buffer->data, size);
  147. if (!tmp)
  148. return AVERROR(ENOMEM);
  149. buf->buffer->data = buf->data = tmp;
  150. buf->buffer->size = buf->size = size;
  151. return 0;
  152. }
  153. AVBufferPool *av_buffer_pool_init(int size, AVBufferRef* (*alloc)(int size))
  154. {
  155. AVBufferPool *pool = av_mallocz(sizeof(*pool));
  156. if (!pool)
  157. return NULL;
  158. pool->size = size;
  159. pool->alloc = alloc ? alloc : av_buffer_alloc;
  160. avpriv_atomic_int_set(&pool->refcount, 1);
  161. return pool;
  162. }
  163. /*
  164. * This function gets called when the pool has been uninited and
  165. * all the buffers returned to it.
  166. */
  167. static void buffer_pool_free(AVBufferPool *pool)
  168. {
  169. while (pool->pool) {
  170. BufferPoolEntry *buf = pool->pool;
  171. pool->pool = buf->next;
  172. buf->free(buf->opaque, buf->data);
  173. av_freep(&buf);
  174. }
  175. av_freep(&pool);
  176. }
  177. void av_buffer_pool_uninit(AVBufferPool **ppool)
  178. {
  179. AVBufferPool *pool;
  180. if (!ppool || !*ppool)
  181. return;
  182. pool = *ppool;
  183. *ppool = NULL;
  184. if (!avpriv_atomic_int_add_and_fetch(&pool->refcount, -1))
  185. buffer_pool_free(pool);
  186. }
  187. /* remove the whole buffer list from the pool and return it */
  188. static BufferPoolEntry *get_pool(AVBufferPool *pool)
  189. {
  190. BufferPoolEntry *cur = NULL, *last = NULL;
  191. do {
  192. FFSWAP(BufferPoolEntry*, cur, last);
  193. cur = avpriv_atomic_ptr_cas((void * volatile *)&pool->pool, last, NULL);
  194. if (!cur)
  195. return NULL;
  196. } while (cur != last);
  197. return cur;
  198. }
  199. static void add_to_pool(BufferPoolEntry *buf)
  200. {
  201. AVBufferPool *pool;
  202. BufferPoolEntry *cur, *end = buf;
  203. if (!buf)
  204. return;
  205. pool = buf->pool;
  206. while (end->next)
  207. end = end->next;
  208. while ((cur = avpriv_atomic_ptr_cas((void * volatile *)&pool->pool, NULL, buf))) {
  209. /* pool is not empty, retrieve it and append it to our list */
  210. cur = get_pool(pool);
  211. end->next = cur;
  212. while (end->next)
  213. end = end->next;
  214. }
  215. }
  216. static void pool_release_buffer(void *opaque, uint8_t *data)
  217. {
  218. BufferPoolEntry *buf = opaque;
  219. AVBufferPool *pool = buf->pool;
  220. add_to_pool(buf);
  221. if (!avpriv_atomic_int_add_and_fetch(&pool->refcount, -1))
  222. buffer_pool_free(pool);
  223. }
  224. /* allocate a new buffer and override its free() callback so that
  225. * it is returned to the pool on free */
  226. static AVBufferRef *pool_alloc_buffer(AVBufferPool *pool)
  227. {
  228. BufferPoolEntry *buf;
  229. AVBufferRef *ret;
  230. ret = pool->alloc(pool->size);
  231. if (!ret)
  232. return NULL;
  233. buf = av_mallocz(sizeof(*buf));
  234. if (!buf) {
  235. av_buffer_unref(&ret);
  236. return NULL;
  237. }
  238. buf->data = ret->buffer->data;
  239. buf->opaque = ret->buffer->opaque;
  240. buf->free = ret->buffer->free;
  241. buf->pool = pool;
  242. ret->buffer->opaque = buf;
  243. ret->buffer->free = pool_release_buffer;
  244. avpriv_atomic_int_add_and_fetch(&pool->refcount, 1);
  245. return ret;
  246. }
  247. AVBufferRef *av_buffer_pool_get(AVBufferPool *pool)
  248. {
  249. AVBufferRef *ret;
  250. BufferPoolEntry *buf;
  251. /* check whether the pool is empty */
  252. buf = get_pool(pool);
  253. if (!buf)
  254. return pool_alloc_buffer(pool);
  255. /* keep the first entry, return the rest of the list to the pool */
  256. add_to_pool(buf->next);
  257. buf->next = NULL;
  258. ret = av_buffer_create(buf->data, pool->size, pool_release_buffer,
  259. buf, 0);
  260. if (!ret) {
  261. add_to_pool(buf);
  262. return NULL;
  263. }
  264. avpriv_atomic_int_add_and_fetch(&pool->refcount, 1);
  265. return ret;
  266. }