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.

287 lines
10KB

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