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.

366 lines
8.3KB

  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->pool_free = pool_free;
  181. atomic_init(&pool->refcount, 1);
  182. return pool;
  183. }
  184. AVBufferPool *av_buffer_pool_init(int size, AVBufferRef* (*alloc)(int size))
  185. {
  186. AVBufferPool *pool = av_mallocz(sizeof(*pool));
  187. if (!pool)
  188. return NULL;
  189. ff_mutex_init(&pool->mutex, NULL);
  190. pool->size = size;
  191. pool->alloc = alloc ? alloc : av_buffer_alloc;
  192. atomic_init(&pool->refcount, 1);
  193. return pool;
  194. }
  195. /*
  196. * This function gets called when the pool has been uninited and
  197. * all the buffers returned to it.
  198. */
  199. static void buffer_pool_free(AVBufferPool *pool)
  200. {
  201. while (pool->pool) {
  202. BufferPoolEntry *buf = pool->pool;
  203. pool->pool = buf->next;
  204. buf->free(buf->opaque, buf->data);
  205. av_freep(&buf);
  206. }
  207. ff_mutex_destroy(&pool->mutex);
  208. if (pool->pool_free)
  209. pool->pool_free(pool->opaque);
  210. av_freep(&pool);
  211. }
  212. void av_buffer_pool_uninit(AVBufferPool **ppool)
  213. {
  214. AVBufferPool *pool;
  215. if (!ppool || !*ppool)
  216. return;
  217. pool = *ppool;
  218. *ppool = NULL;
  219. if (atomic_fetch_sub_explicit(&pool->refcount, 1, memory_order_acq_rel) == 1)
  220. buffer_pool_free(pool);
  221. }
  222. static void pool_release_buffer(void *opaque, uint8_t *data)
  223. {
  224. BufferPoolEntry *buf = opaque;
  225. AVBufferPool *pool = buf->pool;
  226. if(CONFIG_MEMORY_POISONING)
  227. memset(buf->data, FF_MEMORY_POISON, pool->size);
  228. ff_mutex_lock(&pool->mutex);
  229. buf->next = pool->pool;
  230. pool->pool = buf;
  231. ff_mutex_unlock(&pool->mutex);
  232. if (atomic_fetch_sub_explicit(&pool->refcount, 1, memory_order_acq_rel) == 1)
  233. buffer_pool_free(pool);
  234. }
  235. /* allocate a new buffer and override its free() callback so that
  236. * it is returned to the pool on free */
  237. static AVBufferRef *pool_alloc_buffer(AVBufferPool *pool)
  238. {
  239. BufferPoolEntry *buf;
  240. AVBufferRef *ret;
  241. ret = pool->alloc2 ? pool->alloc2(pool->opaque, pool->size) :
  242. pool->alloc(pool->size);
  243. if (!ret)
  244. return NULL;
  245. buf = av_mallocz(sizeof(*buf));
  246. if (!buf) {
  247. av_buffer_unref(&ret);
  248. return NULL;
  249. }
  250. buf->data = ret->buffer->data;
  251. buf->opaque = ret->buffer->opaque;
  252. buf->free = ret->buffer->free;
  253. buf->pool = pool;
  254. ret->buffer->opaque = buf;
  255. ret->buffer->free = pool_release_buffer;
  256. return ret;
  257. }
  258. AVBufferRef *av_buffer_pool_get(AVBufferPool *pool)
  259. {
  260. AVBufferRef *ret;
  261. BufferPoolEntry *buf;
  262. ff_mutex_lock(&pool->mutex);
  263. buf = pool->pool;
  264. if (buf) {
  265. ret = av_buffer_create(buf->data, pool->size, pool_release_buffer,
  266. buf, 0);
  267. if (ret) {
  268. pool->pool = buf->next;
  269. buf->next = NULL;
  270. }
  271. } else {
  272. ret = pool_alloc_buffer(pool);
  273. }
  274. ff_mutex_unlock(&pool->mutex);
  275. if (ret)
  276. atomic_fetch_add_explicit(&pool->refcount, 1, memory_order_relaxed);
  277. return ret;
  278. }
  279. void *av_buffer_pool_buffer_get_opaque(AVBufferRef *ref)
  280. {
  281. BufferPoolEntry *buf = ref->buffer->opaque;
  282. av_assert0(buf);
  283. return buf->opaque;
  284. }