位置:libavutil/buffer.h
    AV系列结构体之AVBuffer、AVBufferRef、AVBufferPool - 图1

    AVBuffer采用引用计数的数据Buffer的API。

    有两个核心对象这个API——AVBuffer和AVBufferRef。

    • AVBuffer代表数据缓冲区本身,它是私有的,不能直接被调用者调用。
    • 我们可以通过AVBufferRef,调用者须要检查两个AVBuffer指针是否指向两个不同的引用在同一数据buffer
    • AVBufferRef 代表一个单个引用指向AVBuffer,调用者可以直接调它。

    有两个功能函数提供创建一个AVBuffer对象在一个单引用中,av_buffer_alloc()分配置一个新buffer空间。 av_buffer_create()负责包装已存在数组的AVBuffer对象。从一个已存在引用,其他的引用将创建通过av_buffer_ref()方法。
    使用av_buffer_unref(),交释放这个引用(包含数据引用及计数引用)。
    在已经存在的AVBuffer引用中,FFmpeg认为AVBuffer是可写入相关数据的。(不会被标识只读),av_buffer_is_writable() 函数提供是否可write的功能,如果不能将自动创建一个新的可写的buffer。

    1. typedef struct AVBuffer AVBuffer;
    2. /**
    3. * A reference to a data buffer.
    4. *
    5. * The size of this struct is not a part of the public ABI and it is not meant
    6. * to be allocated directly.
    7. */
    8. typedef struct AVBufferRef {
    9. AVBuffer *buffer;
    10. /**
    11. * The data buffer. It is considered writable if and only if
    12. * this is the only reference to the buffer, in which case
    13. * av_buffer_is_writable() returns 1.
    14. */
    15. uint8_t *data;
    16. /**
    17. * Size of data in bytes.
    18. */
    19. int size;
    20. } AVBufferRef;
    21. /**
    22. * Allocate an AVBuffer of the given size using av_malloc().
    23. *
    24. * @return an AVBufferRef of given size or NULL when out of memory
    25. */
    26. AVBufferRef *av_buffer_alloc(int size);
    27. /**
    28. * Same as av_buffer_alloc(), except the returned buffer will be initialized
    29. * to zero.
    30. */
    31. AVBufferRef *av_buffer_allocz(int size);
    32. /**
    33. * Always treat the buffer as read-only, even when it has only one
    34. * reference.
    35. */
    36. #define AV_BUFFER_FLAG_READONLY (1 << 0)
    37. /**
    38. * Create an AVBuffer from an existing array.
    39. *
    40. * If this function is successful, data is owned by the AVBuffer. The caller may
    41. * only access data through the returned AVBufferRef and references derived from
    42. * it.
    43. * If this function fails, data is left untouched.
    44. * @param data data array
    45. * @param size size of data in bytes
    46. * @param free a callback for freeing this buffer's data
    47. * @param opaque parameter to be got for processing or passed to free
    48. * @param flags a combination of AV_BUFFER_FLAG_*
    49. *
    50. * @return an AVBufferRef referring to data on success, NULL on failure.
    51. */
    52. AVBufferRef *av_buffer_create(uint8_t *data, int size,
    53. void (*free)(void *opaque, uint8_t *data),
    54. void *opaque, int flags);
    55. /**
    56. * Default free callback, which calls av_free() on the buffer data.
    57. * This function is meant to be passed to av_buffer_create(), not called
    58. * directly.
    59. */
    60. void av_buffer_default_free(void *opaque, uint8_t *data);
    61. /**
    62. * Create a new reference to an AVBuffer.
    63. *
    64. * @return a new AVBufferRef referring to the same AVBuffer as buf or NULL on
    65. * failure.
    66. */
    67. AVBufferRef *av_buffer_ref(AVBufferRef *buf);
    68. /**
    69. * Free a given reference and automatically free the buffer if there are no more
    70. * references to it.
    71. *
    72. * @param buf the reference to be freed. The pointer is set to NULL on return.
    73. */
    74. void av_buffer_unref(AVBufferRef **buf);
    75. /**
    76. * @return 1 if the caller may write to the data referred to by buf (which is
    77. * true if and only if buf is the only reference to the underlying AVBuffer).
    78. * Return 0 otherwise.
    79. * A positive answer is valid until av_buffer_ref() is called on buf.
    80. */
    81. int av_buffer_is_writable(const AVBufferRef *buf);
    82. /**
    83. * @return the opaque parameter set by av_buffer_create.
    84. */
    85. void *av_buffer_get_opaque(const AVBufferRef *buf);
    86. int av_buffer_get_ref_count(const AVBufferRef *buf);
    87. /**
    88. * Create a writable reference from a given buffer reference, avoiding data copy
    89. * if possible.
    90. *
    91. * @param buf buffer reference to make writable. On success, buf is either left
    92. * untouched, or it is unreferenced and a new writable AVBufferRef is
    93. * written in its place. On failure, buf is left untouched.
    94. * @return 0 on success, a negative AVERROR on failure.
    95. */
    96. int av_buffer_make_writable(AVBufferRef **buf);
    97. /**
    98. * Reallocate a given buffer.
    99. *
    100. * @param buf a buffer reference to reallocate. On success, buf will be
    101. * unreferenced and a new reference with the required size will be
    102. * written in its place. On failure buf will be left untouched. *buf
    103. * may be NULL, then a new buffer is allocated.
    104. * @param size required new buffer size.
    105. * @return 0 on success, a negative AVERROR on failure.
    106. *
    107. * @note the buffer is actually reallocated with av_realloc() only if it was
    108. * initially allocated through av_buffer_realloc(NULL) and there is only one
    109. * reference to it (i.e. the one passed to this function). In all other cases
    110. * a new buffer is allocated and the data is copied.
    111. */
    112. int av_buffer_realloc(AVBufferRef **buf, int size);
    113. /**
    114. * @}
    115. */
    116. typedef struct AVBufferPool AVBufferPool;
    117. /**
    118. * Allocate and initialize a buffer pool.
    119. *
    120. * @param size size of each buffer in this pool
    121. * @param alloc a function that will be used to allocate new buffers when the
    122. * pool is empty. May be NULL, then the default allocator will be used
    123. * (av_buffer_alloc()).
    124. * @return newly created buffer pool on success, NULL on error.
    125. */
    126. AVBufferPool *av_buffer_pool_init(int size, AVBufferRef* (*alloc)(int size));
    127. /**
    128. * Allocate and initialize a buffer pool with a more complex allocator.
    129. *
    130. * @param size size of each buffer in this pool
    131. * @param opaque arbitrary user data used by the allocator
    132. * @param alloc a function that will be used to allocate new buffers when the
    133. * pool is empty.
    134. * @param pool_free a function that will be called immediately before the pool
    135. * is freed. I.e. after av_buffer_pool_can_uninit() is called
    136. * by the pool and all the frames are returned to the pool and
    137. * freed. It is intended to uninitialize the user opaque data.
    138. * @return newly created buffer pool on success, NULL on error.
    139. */
    140. AVBufferPool *av_buffer_pool_init2(int size, void *opaque,
    141. AVBufferRef* (*alloc)(void *opaque, int size),
    142. void (*pool_free)(void *opaque));
    143. /**
    144. * Mark the pool as being available for freeing. It will actually be freed only
    145. * once all the allocated buffers associated with the pool are released. Thus it
    146. * is safe to call this function while some of the allocated buffers are still
    147. * in use.
    148. *
    149. * @param pool pointer to the pool to be freed. It will be set to NULL.
    150. */
    151. void av_buffer_pool_uninit(AVBufferPool **pool);
    152. /**
    153. * Allocate a new AVBuffer, reusing an old buffer from the pool when available.
    154. * This function may be called simultaneously from multiple threads.
    155. *
    156. * @return a reference to the new buffer on success, NULL on error.
    157. */
    158. AVBufferRef *av_buffer_pool_get(AVBufferPool *pool);
    159. /**
    160. * @}
    161. */
    162. #endif /* AVUTIL_BUFFER_H */

    AVBufferPool 是用来管理大buffer时的API

    调用者必须使用av_buffer_pool_init()创建一个buffer池,无论什么时候需要,通过av_buffer_pool_get(),得到新buffer的引用,和av_buffer_alloc()非常像,当不被引用时,会重新归还给pool,而不用去像AVBuffer那样释放。当调用者不再分配新的buffer时,av_buffer_pool_uninit() 必须调用,表明这个pool将自动释放了(因为这个pool也是一个对象)。分配和释放buffer,是线程安全操作,只要保证没有其他默认alloc的callback在使用。这个结构体是私有的,不能直接被外部使用,外部可以用av_buffer_pool_init(),及av_buffer_pool_uninit()函数。


    如约智惠.png