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.

395 lines
8.9KB

  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. 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(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_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, int 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(int size, void *opaque,
  191. AVBufferRef* (*alloc)(void *opaque, int 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(int size, AVBufferRef* (*alloc)(int 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. /*
  218. * This function gets called when the pool has been uninited and
  219. * all the buffers returned to it.
  220. */
  221. static void buffer_pool_free(AVBufferPool *pool)
  222. {
  223. while (pool->pool) {
  224. BufferPoolEntry *buf = pool->pool;
  225. pool->pool = buf->next;
  226. buf->free(buf->opaque, buf->data);
  227. av_freep(&buf);
  228. }
  229. ff_mutex_destroy(&pool->mutex);
  230. if (pool->pool_free)
  231. pool->pool_free(pool->opaque);
  232. av_freep(&pool);
  233. }
  234. void av_buffer_pool_uninit(AVBufferPool **ppool)
  235. {
  236. AVBufferPool *pool;
  237. if (!ppool || !*ppool)
  238. return;
  239. pool = *ppool;
  240. *ppool = NULL;
  241. if (atomic_fetch_sub_explicit(&pool->refcount, 1, memory_order_acq_rel) == 1)
  242. buffer_pool_free(pool);
  243. }
  244. static void pool_release_buffer(void *opaque, uint8_t *data)
  245. {
  246. BufferPoolEntry *buf = opaque;
  247. AVBufferPool *pool = buf->pool;
  248. if(CONFIG_MEMORY_POISONING)
  249. memset(buf->data, FF_MEMORY_POISON, pool->size);
  250. ff_mutex_lock(&pool->mutex);
  251. buf->next = pool->pool;
  252. pool->pool = buf;
  253. ff_mutex_unlock(&pool->mutex);
  254. if (atomic_fetch_sub_explicit(&pool->refcount, 1, memory_order_acq_rel) == 1)
  255. buffer_pool_free(pool);
  256. }
  257. /* allocate a new buffer and override its free() callback so that
  258. * it is returned to the pool on free */
  259. static AVBufferRef *pool_alloc_buffer(AVBufferPool *pool)
  260. {
  261. BufferPoolEntry *buf;
  262. AVBufferRef *ret;
  263. av_assert0(pool->alloc || pool->alloc2);
  264. ret = pool->alloc2 ? pool->alloc2(pool->opaque, pool->size) :
  265. pool->alloc(pool->size);
  266. if (!ret)
  267. return NULL;
  268. buf = av_mallocz(sizeof(*buf));
  269. if (!buf) {
  270. av_buffer_unref(&ret);
  271. return NULL;
  272. }
  273. buf->data = ret->buffer->data;
  274. buf->opaque = ret->buffer->opaque;
  275. buf->free = ret->buffer->free;
  276. buf->pool = pool;
  277. ret->buffer->opaque = buf;
  278. ret->buffer->free = pool_release_buffer;
  279. return ret;
  280. }
  281. AVBufferRef *av_buffer_pool_get(AVBufferPool *pool)
  282. {
  283. AVBufferRef *ret;
  284. BufferPoolEntry *buf;
  285. ff_mutex_lock(&pool->mutex);
  286. buf = pool->pool;
  287. if (buf) {
  288. ret = av_buffer_create(buf->data, pool->size, pool_release_buffer,
  289. buf, 0);
  290. if (ret) {
  291. pool->pool = buf->next;
  292. buf->next = NULL;
  293. }
  294. } else {
  295. ret = pool_alloc_buffer(pool);
  296. }
  297. ff_mutex_unlock(&pool->mutex);
  298. if (ret)
  299. atomic_fetch_add_explicit(&pool->refcount, 1, memory_order_relaxed);
  300. return ret;
  301. }
  302. void *av_buffer_pool_buffer_get_opaque(AVBufferRef *ref)
  303. {
  304. BufferPoolEntry *buf = ref->buffer->opaque;
  305. av_assert0(buf);
  306. return buf->opaque;
  307. }