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. 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. 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. }