2
0

buffer.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. /*
  2. * This file is part of FFmpeg.
  3. *
  4. * FFmpeg 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. * FFmpeg 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 FFmpeg; 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. static void buffer_replace(AVBufferRef **dst, AVBufferRef **src)
  85. {
  86. AVBuffer *b;
  87. b = (*dst)->buffer;
  88. if (src) {
  89. **dst = **src;
  90. av_freep(src);
  91. } else
  92. av_freep(dst);
  93. if (atomic_fetch_add_explicit(&b->refcount, -1, memory_order_acq_rel) == 1) {
  94. b->free(b->opaque, b->data);
  95. av_freep(&b);
  96. }
  97. }
  98. void av_buffer_unref(AVBufferRef **buf)
  99. {
  100. if (!buf || !*buf)
  101. return;
  102. buffer_replace(buf, NULL);
  103. }
  104. int av_buffer_is_writable(const AVBufferRef *buf)
  105. {
  106. if (buf->buffer->flags & AV_BUFFER_FLAG_READONLY)
  107. return 0;
  108. return atomic_load(&buf->buffer->refcount) == 1;
  109. }
  110. void *av_buffer_get_opaque(const AVBufferRef *buf)
  111. {
  112. return buf->buffer->opaque;
  113. }
  114. int av_buffer_get_ref_count(const AVBufferRef *buf)
  115. {
  116. return atomic_load(&buf->buffer->refcount);
  117. }
  118. int av_buffer_make_writable(AVBufferRef **pbuf)
  119. {
  120. AVBufferRef *newbuf, *buf = *pbuf;
  121. if (av_buffer_is_writable(buf))
  122. return 0;
  123. newbuf = av_buffer_alloc(buf->size);
  124. if (!newbuf)
  125. return AVERROR(ENOMEM);
  126. memcpy(newbuf->data, buf->data, buf->size);
  127. buffer_replace(pbuf, &newbuf);
  128. return 0;
  129. }
  130. int av_buffer_realloc(AVBufferRef **pbuf, int size)
  131. {
  132. AVBufferRef *buf = *pbuf;
  133. uint8_t *tmp;
  134. if (!buf) {
  135. /* allocate a new buffer with av_realloc(), so it will be reallocatable
  136. * later */
  137. uint8_t *data = av_realloc(NULL, size);
  138. if (!data)
  139. return AVERROR(ENOMEM);
  140. buf = av_buffer_create(data, size, av_buffer_default_free, NULL, 0);
  141. if (!buf) {
  142. av_freep(&data);
  143. return AVERROR(ENOMEM);
  144. }
  145. buf->buffer->flags |= BUFFER_FLAG_REALLOCATABLE;
  146. *pbuf = buf;
  147. return 0;
  148. } else if (buf->size == size)
  149. return 0;
  150. if (!(buf->buffer->flags & BUFFER_FLAG_REALLOCATABLE) ||
  151. !av_buffer_is_writable(buf) || buf->data != buf->buffer->data) {
  152. /* cannot realloc, allocate a new reallocable buffer and copy data */
  153. AVBufferRef *new = NULL;
  154. av_buffer_realloc(&new, size);
  155. if (!new)
  156. return AVERROR(ENOMEM);
  157. memcpy(new->data, buf->data, FFMIN(size, buf->size));
  158. buffer_replace(pbuf, &new);
  159. return 0;
  160. }
  161. tmp = av_realloc(buf->buffer->data, size);
  162. if (!tmp)
  163. return AVERROR(ENOMEM);
  164. buf->buffer->data = buf->data = tmp;
  165. buf->buffer->size = buf->size = size;
  166. return 0;
  167. }
  168. AVBufferPool *av_buffer_pool_init2(int size, void *opaque,
  169. AVBufferRef* (*alloc)(void *opaque, int size),
  170. void (*pool_free)(void *opaque))
  171. {
  172. AVBufferPool *pool = av_mallocz(sizeof(*pool));
  173. if (!pool)
  174. return NULL;
  175. ff_mutex_init(&pool->mutex, NULL);
  176. pool->size = size;
  177. pool->opaque = opaque;
  178. pool->alloc2 = alloc;
  179. pool->pool_free = pool_free;
  180. atomic_init(&pool->refcount, 1);
  181. return pool;
  182. }
  183. AVBufferPool *av_buffer_pool_init(int size, AVBufferRef* (*alloc)(int size))
  184. {
  185. AVBufferPool *pool = av_mallocz(sizeof(*pool));
  186. if (!pool)
  187. return NULL;
  188. ff_mutex_init(&pool->mutex, NULL);
  189. pool->size = size;
  190. pool->alloc = alloc ? alloc : av_buffer_alloc;
  191. atomic_init(&pool->refcount, 1);
  192. return pool;
  193. }
  194. /*
  195. * This function gets called when the pool has been uninited and
  196. * all the buffers returned to it.
  197. */
  198. static void buffer_pool_free(AVBufferPool *pool)
  199. {
  200. while (pool->pool) {
  201. BufferPoolEntry *buf = pool->pool;
  202. pool->pool = buf->next;
  203. buf->free(buf->opaque, buf->data);
  204. av_freep(&buf);
  205. }
  206. ff_mutex_destroy(&pool->mutex);
  207. if (pool->pool_free)
  208. pool->pool_free(pool->opaque);
  209. av_freep(&pool);
  210. }
  211. void av_buffer_pool_uninit(AVBufferPool **ppool)
  212. {
  213. AVBufferPool *pool;
  214. if (!ppool || !*ppool)
  215. return;
  216. pool = *ppool;
  217. *ppool = NULL;
  218. if (atomic_fetch_add_explicit(&pool->refcount, -1, memory_order_acq_rel) == 1)
  219. buffer_pool_free(pool);
  220. }
  221. static void pool_release_buffer(void *opaque, uint8_t *data)
  222. {
  223. BufferPoolEntry *buf = opaque;
  224. AVBufferPool *pool = buf->pool;
  225. if(CONFIG_MEMORY_POISONING)
  226. memset(buf->data, FF_MEMORY_POISON, pool->size);
  227. ff_mutex_lock(&pool->mutex);
  228. buf->next = pool->pool;
  229. pool->pool = buf;
  230. ff_mutex_unlock(&pool->mutex);
  231. if (atomic_fetch_add_explicit(&pool->refcount, -1, memory_order_acq_rel) == 1)
  232. buffer_pool_free(pool);
  233. }
  234. /* allocate a new buffer and override its free() callback so that
  235. * it is returned to the pool on free */
  236. static AVBufferRef *pool_alloc_buffer(AVBufferPool *pool)
  237. {
  238. BufferPoolEntry *buf;
  239. AVBufferRef *ret;
  240. ret = pool->alloc2 ? pool->alloc2(pool->opaque, pool->size) :
  241. pool->alloc(pool->size);
  242. if (!ret)
  243. return NULL;
  244. buf = av_mallocz(sizeof(*buf));
  245. if (!buf) {
  246. av_buffer_unref(&ret);
  247. return NULL;
  248. }
  249. buf->data = ret->buffer->data;
  250. buf->opaque = ret->buffer->opaque;
  251. buf->free = ret->buffer->free;
  252. buf->pool = pool;
  253. ret->buffer->opaque = buf;
  254. ret->buffer->free = pool_release_buffer;
  255. return ret;
  256. }
  257. AVBufferRef *av_buffer_pool_get(AVBufferPool *pool)
  258. {
  259. AVBufferRef *ret;
  260. BufferPoolEntry *buf;
  261. ff_mutex_lock(&pool->mutex);
  262. buf = pool->pool;
  263. if (buf) {
  264. ret = av_buffer_create(buf->data, pool->size, pool_release_buffer,
  265. buf, 0);
  266. if (ret) {
  267. pool->pool = buf->next;
  268. buf->next = NULL;
  269. }
  270. } else {
  271. ret = pool_alloc_buffer(pool);
  272. }
  273. ff_mutex_unlock(&pool->mutex);
  274. if (ret)
  275. atomic_fetch_add_explicit(&pool->refcount, 1, memory_order_relaxed);
  276. return ret;
  277. }