buffer.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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. /**
  19. * @file
  20. * @ingroup lavu_buffer
  21. * refcounted data buffer API
  22. */
  23. #ifndef AVUTIL_BUFFER_H
  24. #define AVUTIL_BUFFER_H
  25. #include <stddef.h>
  26. #include <stdint.h>
  27. #include "version.h"
  28. /**
  29. * @defgroup lavu_buffer AVBuffer
  30. * @ingroup lavu_data
  31. *
  32. * @{
  33. * AVBuffer is an API for reference-counted data buffers.
  34. *
  35. * There are two core objects in this API -- AVBuffer and AVBufferRef. AVBuffer
  36. * represents the data buffer itself; it is opaque and not meant to be accessed
  37. * by the caller directly, but only through AVBufferRef. However, the caller may
  38. * e.g. compare two AVBuffer pointers to check whether two different references
  39. * are describing the same data buffer. AVBufferRef represents a single
  40. * reference to an AVBuffer and it is the object that may be manipulated by the
  41. * caller directly.
  42. *
  43. * There are two functions provided for creating a new AVBuffer with a single
  44. * reference -- av_buffer_alloc() to just allocate a new buffer, and
  45. * av_buffer_create() to wrap an existing array in an AVBuffer. From an existing
  46. * reference, additional references may be created with av_buffer_ref().
  47. * Use av_buffer_unref() to free a reference (this will automatically free the
  48. * data once all the references are freed).
  49. *
  50. * The convention throughout this API and the rest of FFmpeg is such that the
  51. * buffer is considered writable if there exists only one reference to it (and
  52. * it has not been marked as read-only). The av_buffer_is_writable() function is
  53. * provided to check whether this is true and av_buffer_make_writable() will
  54. * automatically create a new writable buffer when necessary.
  55. * Of course nothing prevents the calling code from violating this convention,
  56. * however that is safe only when all the existing references are under its
  57. * control.
  58. *
  59. * @note Referencing and unreferencing the buffers is thread-safe and thus
  60. * may be done from multiple threads simultaneously without any need for
  61. * additional locking.
  62. *
  63. * @note Two different references to the same buffer can point to different
  64. * parts of the buffer (i.e. their AVBufferRef.data will not be equal).
  65. */
  66. /**
  67. * A reference counted buffer type. It is opaque and is meant to be used through
  68. * references (AVBufferRef).
  69. */
  70. typedef struct AVBuffer AVBuffer;
  71. /**
  72. * A reference to a data buffer.
  73. *
  74. * The size of this struct is not a part of the public ABI and it is not meant
  75. * to be allocated directly.
  76. */
  77. typedef struct AVBufferRef {
  78. AVBuffer *buffer;
  79. /**
  80. * The data buffer. It is considered writable if and only if
  81. * this is the only reference to the buffer, in which case
  82. * av_buffer_is_writable() returns 1.
  83. */
  84. uint8_t *data;
  85. /**
  86. * Size of data in bytes.
  87. */
  88. #if FF_API_BUFFER_SIZE_T
  89. int size;
  90. #else
  91. size_t size;
  92. #endif
  93. } AVBufferRef;
  94. /**
  95. * Allocate an AVBuffer of the given size using av_malloc().
  96. *
  97. * @return an AVBufferRef of given size or NULL when out of memory
  98. */
  99. #if FF_API_BUFFER_SIZE_T
  100. AVBufferRef *av_buffer_alloc(int size);
  101. #else
  102. AVBufferRef *av_buffer_alloc(size_t size);
  103. #endif
  104. /**
  105. * Same as av_buffer_alloc(), except the returned buffer will be initialized
  106. * to zero.
  107. */
  108. #if FF_API_BUFFER_SIZE_T
  109. AVBufferRef *av_buffer_allocz(int size);
  110. #else
  111. AVBufferRef *av_buffer_allocz(size_t size);
  112. #endif
  113. /**
  114. * Always treat the buffer as read-only, even when it has only one
  115. * reference.
  116. */
  117. #define AV_BUFFER_FLAG_READONLY (1 << 0)
  118. /**
  119. * Create an AVBuffer from an existing array.
  120. *
  121. * If this function is successful, data is owned by the AVBuffer. The caller may
  122. * only access data through the returned AVBufferRef and references derived from
  123. * it.
  124. * If this function fails, data is left untouched.
  125. * @param data data array
  126. * @param size size of data in bytes
  127. * @param free a callback for freeing this buffer's data
  128. * @param opaque parameter to be got for processing or passed to free
  129. * @param flags a combination of AV_BUFFER_FLAG_*
  130. *
  131. * @return an AVBufferRef referring to data on success, NULL on failure.
  132. */
  133. #if FF_API_BUFFER_SIZE_T
  134. AVBufferRef *av_buffer_create(uint8_t *data, int size,
  135. #else
  136. AVBufferRef *av_buffer_create(uint8_t *data, size_t size,
  137. #endif
  138. void (*free)(void *opaque, uint8_t *data),
  139. void *opaque, int flags);
  140. /**
  141. * Default free callback, which calls av_free() on the buffer data.
  142. * This function is meant to be passed to av_buffer_create(), not called
  143. * directly.
  144. */
  145. void av_buffer_default_free(void *opaque, uint8_t *data);
  146. /**
  147. * Create a new reference to an AVBuffer.
  148. *
  149. * @return a new AVBufferRef referring to the same AVBuffer as buf or NULL on
  150. * failure.
  151. */
  152. AVBufferRef *av_buffer_ref(AVBufferRef *buf);
  153. /**
  154. * Free a given reference and automatically free the buffer if there are no more
  155. * references to it.
  156. *
  157. * @param buf the reference to be freed. The pointer is set to NULL on return.
  158. */
  159. void av_buffer_unref(AVBufferRef **buf);
  160. /**
  161. * @return 1 if the caller may write to the data referred to by buf (which is
  162. * true if and only if buf is the only reference to the underlying AVBuffer).
  163. * Return 0 otherwise.
  164. * A positive answer is valid until av_buffer_ref() is called on buf.
  165. */
  166. int av_buffer_is_writable(const AVBufferRef *buf);
  167. /**
  168. * @return the opaque parameter set by av_buffer_create.
  169. */
  170. void *av_buffer_get_opaque(const AVBufferRef *buf);
  171. int av_buffer_get_ref_count(const AVBufferRef *buf);
  172. /**
  173. * Create a writable reference from a given buffer reference, avoiding data copy
  174. * if possible.
  175. *
  176. * @param buf buffer reference to make writable. On success, buf is either left
  177. * untouched, or it is unreferenced and a new writable AVBufferRef is
  178. * written in its place. On failure, buf is left untouched.
  179. * @return 0 on success, a negative AVERROR on failure.
  180. */
  181. int av_buffer_make_writable(AVBufferRef **buf);
  182. /**
  183. * Reallocate a given buffer.
  184. *
  185. * @param buf a buffer reference to reallocate. On success, buf will be
  186. * unreferenced and a new reference with the required size will be
  187. * written in its place. On failure buf will be left untouched. *buf
  188. * may be NULL, then a new buffer is allocated.
  189. * @param size required new buffer size.
  190. * @return 0 on success, a negative AVERROR on failure.
  191. *
  192. * @note the buffer is actually reallocated with av_realloc() only if it was
  193. * initially allocated through av_buffer_realloc(NULL) and there is only one
  194. * reference to it (i.e. the one passed to this function). In all other cases
  195. * a new buffer is allocated and the data is copied.
  196. */
  197. #if FF_API_BUFFER_SIZE_T
  198. int av_buffer_realloc(AVBufferRef **buf, int size);
  199. #else
  200. int av_buffer_realloc(AVBufferRef **buf, size_t size);
  201. #endif
  202. /**
  203. * Ensure dst refers to the same data as src.
  204. *
  205. * When *dst is already equivalent to src, do nothing. Otherwise unreference dst
  206. * and replace it with a new reference to src.
  207. *
  208. * @param dst Pointer to either a valid buffer reference or NULL. On success,
  209. * this will point to a buffer reference equivalent to src. On
  210. * failure, dst will be left untouched.
  211. * @param src A buffer reference to replace dst with. May be NULL, then this
  212. * function is equivalent to av_buffer_unref(dst).
  213. * @return 0 on success
  214. * AVERROR(ENOMEM) on memory allocation failure.
  215. */
  216. int av_buffer_replace(AVBufferRef **dst, AVBufferRef *src);
  217. /**
  218. * @}
  219. */
  220. /**
  221. * @defgroup lavu_bufferpool AVBufferPool
  222. * @ingroup lavu_data
  223. *
  224. * @{
  225. * AVBufferPool is an API for a lock-free thread-safe pool of AVBuffers.
  226. *
  227. * Frequently allocating and freeing large buffers may be slow. AVBufferPool is
  228. * meant to solve this in cases when the caller needs a set of buffers of the
  229. * same size (the most obvious use case being buffers for raw video or audio
  230. * frames).
  231. *
  232. * At the beginning, the user must call av_buffer_pool_init() to create the
  233. * buffer pool. Then whenever a buffer is needed, call av_buffer_pool_get() to
  234. * get a reference to a new buffer, similar to av_buffer_alloc(). This new
  235. * reference works in all aspects the same way as the one created by
  236. * av_buffer_alloc(). However, when the last reference to this buffer is
  237. * unreferenced, it is returned to the pool instead of being freed and will be
  238. * reused for subsequent av_buffer_pool_get() calls.
  239. *
  240. * When the caller is done with the pool and no longer needs to allocate any new
  241. * buffers, av_buffer_pool_uninit() must be called to mark the pool as freeable.
  242. * Once all the buffers are released, it will automatically be freed.
  243. *
  244. * Allocating and releasing buffers with this API is thread-safe as long as
  245. * either the default alloc callback is used, or the user-supplied one is
  246. * thread-safe.
  247. */
  248. /**
  249. * The buffer pool. This structure is opaque and not meant to be accessed
  250. * directly. It is allocated with av_buffer_pool_init() and freed with
  251. * av_buffer_pool_uninit().
  252. */
  253. typedef struct AVBufferPool AVBufferPool;
  254. /**
  255. * Allocate and initialize a buffer pool.
  256. *
  257. * @param size size of each buffer in this pool
  258. * @param alloc a function that will be used to allocate new buffers when the
  259. * pool is empty. May be NULL, then the default allocator will be used
  260. * (av_buffer_alloc()).
  261. * @return newly created buffer pool on success, NULL on error.
  262. */
  263. #if FF_API_BUFFER_SIZE_T
  264. AVBufferPool *av_buffer_pool_init(int size, AVBufferRef* (*alloc)(int size));
  265. #else
  266. AVBufferPool *av_buffer_pool_init(size_t size, AVBufferRef* (*alloc)(size_t size));
  267. #endif
  268. /**
  269. * Allocate and initialize a buffer pool with a more complex allocator.
  270. *
  271. * @param size size of each buffer in this pool
  272. * @param opaque arbitrary user data used by the allocator
  273. * @param alloc a function that will be used to allocate new buffers when the
  274. * pool is empty. May be NULL, then the default allocator will be
  275. * used (av_buffer_alloc()).
  276. * @param pool_free a function that will be called immediately before the pool
  277. * is freed. I.e. after av_buffer_pool_uninit() is called
  278. * by the caller and all the frames are returned to the pool
  279. * and freed. It is intended to uninitialize the user opaque
  280. * data. May be NULL.
  281. * @return newly created buffer pool on success, NULL on error.
  282. */
  283. #if FF_API_BUFFER_SIZE_T
  284. AVBufferPool *av_buffer_pool_init2(int size, void *opaque,
  285. AVBufferRef* (*alloc)(void *opaque, int size),
  286. #else
  287. AVBufferPool *av_buffer_pool_init2(size_t size, void *opaque,
  288. AVBufferRef* (*alloc)(void *opaque, size_t size),
  289. #endif
  290. void (*pool_free)(void *opaque));
  291. /**
  292. * Mark the pool as being available for freeing. It will actually be freed only
  293. * once all the allocated buffers associated with the pool are released. Thus it
  294. * is safe to call this function while some of the allocated buffers are still
  295. * in use.
  296. *
  297. * @param pool pointer to the pool to be freed. It will be set to NULL.
  298. */
  299. void av_buffer_pool_uninit(AVBufferPool **pool);
  300. /**
  301. * Allocate a new AVBuffer, reusing an old buffer from the pool when available.
  302. * This function may be called simultaneously from multiple threads.
  303. *
  304. * @return a reference to the new buffer on success, NULL on error.
  305. */
  306. AVBufferRef *av_buffer_pool_get(AVBufferPool *pool);
  307. /**
  308. * Query the original opaque parameter of an allocated buffer in the pool.
  309. *
  310. * @param ref a buffer reference to a buffer returned by av_buffer_pool_get.
  311. * @return the opaque parameter set by the buffer allocator function of the
  312. * buffer pool.
  313. *
  314. * @note the opaque parameter of ref is used by the buffer pool implementation,
  315. * therefore you have to use this function to access the original opaque
  316. * parameter of an allocated buffer.
  317. */
  318. void *av_buffer_pool_buffer_get_opaque(AVBufferRef *ref);
  319. /**
  320. * @}
  321. */
  322. #endif /* AVUTIL_BUFFER_H */