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.

404 lines
9.1KB

  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, buffer_size_t 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. buf->flags = flags;
  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(buffer_size_t 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(buffer_size_t 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_sub_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, buffer_size_t size)
  131. {
  132. AVBufferRef *buf = *pbuf;
  133. uint8_t *tmp;
  134. int ret;
  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_internal |= BUFFER_FLAG_REALLOCATABLE;
  147. *pbuf = buf;
  148. return 0;
  149. } else if (buf->size == size)
  150. return 0;
  151. if (!(buf->buffer->flags_internal & 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. ret = av_buffer_realloc(&new, size);
  156. if (ret < 0)
  157. return ret;
  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. int av_buffer_replace(AVBufferRef **pdst, AVBufferRef *src)
  170. {
  171. AVBufferRef *dst = *pdst;
  172. AVBufferRef *tmp;
  173. if (!src) {
  174. av_buffer_unref(pdst);
  175. return 0;
  176. }
  177. if (dst && dst->buffer == src->buffer) {
  178. /* make sure the data pointers match */
  179. dst->data = src->data;
  180. dst->size = src->size;
  181. return 0;
  182. }
  183. tmp = av_buffer_ref(src);
  184. if (!tmp)
  185. return AVERROR(ENOMEM);
  186. av_buffer_unref(pdst);
  187. *pdst = tmp;
  188. return 0;
  189. }
  190. AVBufferPool *av_buffer_pool_init2(buffer_size_t size, void *opaque,
  191. AVBufferRef* (*alloc)(void *opaque, buffer_size_t size),
  192. void (*pool_free)(void *opaque))
  193. {
  194. AVBufferPool *pool = av_mallocz(sizeof(*pool));
  195. if (!pool)
  196. return NULL;
  197. ff_mutex_init(&pool->mutex, NULL);
  198. pool->size = size;
  199. pool->opaque = opaque;
  200. pool->alloc2 = alloc;
  201. pool->alloc = av_buffer_alloc; // fallback
  202. pool->pool_free = pool_free;
  203. atomic_init(&pool->refcount, 1);
  204. return pool;
  205. }
  206. AVBufferPool *av_buffer_pool_init(buffer_size_t size, AVBufferRef* (*alloc)(buffer_size_t size))
  207. {
  208. AVBufferPool *pool = av_mallocz(sizeof(*pool));
  209. if (!pool)
  210. return NULL;
  211. ff_mutex_init(&pool->mutex, NULL);
  212. pool->size = size;
  213. pool->alloc = alloc ? alloc : av_buffer_alloc;
  214. atomic_init(&pool->refcount, 1);
  215. return pool;
  216. }
  217. static void buffer_pool_flush(AVBufferPool *pool)
  218. {
  219. while (pool->pool) {
  220. BufferPoolEntry *buf = pool->pool;
  221. pool->pool = buf->next;
  222. buf->free(buf->opaque, buf->data);
  223. av_freep(&buf);
  224. }
  225. }
  226. /*
  227. * This function gets called when the pool has been uninited and
  228. * all the buffers returned to it.
  229. */
  230. static void buffer_pool_free(AVBufferPool *pool)
  231. {
  232. buffer_pool_flush(pool);
  233. ff_mutex_destroy(&pool->mutex);
  234. if (pool->pool_free)
  235. pool->pool_free(pool->opaque);
  236. av_freep(&pool);
  237. }
  238. void av_buffer_pool_uninit(AVBufferPool **ppool)
  239. {
  240. AVBufferPool *pool;
  241. if (!ppool || !*ppool)
  242. return;
  243. pool = *ppool;
  244. *ppool = NULL;
  245. ff_mutex_lock(&pool->mutex);
  246. buffer_pool_flush(pool);
  247. ff_mutex_unlock(&pool->mutex);
  248. if (atomic_fetch_sub_explicit(&pool->refcount, 1, memory_order_acq_rel) == 1)
  249. buffer_pool_free(pool);
  250. }
  251. static void pool_release_buffer(void *opaque, uint8_t *data)
  252. {
  253. BufferPoolEntry *buf = opaque;
  254. AVBufferPool *pool = buf->pool;
  255. if(CONFIG_MEMORY_POISONING)
  256. memset(buf->data, FF_MEMORY_POISON, pool->size);
  257. ff_mutex_lock(&pool->mutex);
  258. buf->next = pool->pool;
  259. pool->pool = buf;
  260. ff_mutex_unlock(&pool->mutex);
  261. if (atomic_fetch_sub_explicit(&pool->refcount, 1, memory_order_acq_rel) == 1)
  262. buffer_pool_free(pool);
  263. }
  264. /* allocate a new buffer and override its free() callback so that
  265. * it is returned to the pool on free */
  266. static AVBufferRef *pool_alloc_buffer(AVBufferPool *pool)
  267. {
  268. BufferPoolEntry *buf;
  269. AVBufferRef *ret;
  270. av_assert0(pool->alloc || pool->alloc2);
  271. ret = pool->alloc2 ? pool->alloc2(pool->opaque, pool->size) :
  272. pool->alloc(pool->size);
  273. if (!ret)
  274. return NULL;
  275. buf = av_mallocz(sizeof(*buf));
  276. if (!buf) {
  277. av_buffer_unref(&ret);
  278. return NULL;
  279. }
  280. buf->data = ret->buffer->data;
  281. buf->opaque = ret->buffer->opaque;
  282. buf->free = ret->buffer->free;
  283. buf->pool = pool;
  284. ret->buffer->opaque = buf;
  285. ret->buffer->free = pool_release_buffer;
  286. return ret;
  287. }
  288. AVBufferRef *av_buffer_pool_get(AVBufferPool *pool)
  289. {
  290. AVBufferRef *ret;
  291. BufferPoolEntry *buf;
  292. ff_mutex_lock(&pool->mutex);
  293. buf = pool->pool;
  294. if (buf) {
  295. ret = av_buffer_create(buf->data, pool->size, pool_release_buffer,
  296. buf, 0);
  297. if (ret) {
  298. pool->pool = buf->next;
  299. buf->next = NULL;
  300. }
  301. } else {
  302. ret = pool_alloc_buffer(pool);
  303. }
  304. ff_mutex_unlock(&pool->mutex);
  305. if (ret)
  306. atomic_fetch_add_explicit(&pool->refcount, 1, memory_order_relaxed);
  307. return ret;
  308. }
  309. void *av_buffer_pool_buffer_get_opaque(AVBufferRef *ref)
  310. {
  311. BufferPoolEntry *buf = ref->buffer->opaque;
  312. av_assert0(buf);
  313. return buf->opaque;
  314. }