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.

369 lines
8.4KB

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