NvBuffer.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /*
  2. * Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions, and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of NVIDIA CORPORATION nor the names of its
  13. * contributors may be used to endorse or promote products derived
  14. * from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  19. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  20. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  21. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  22. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  23. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  24. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  26. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. /**
  29. * @file
  30. * <b>NVIDIA Multimedia API: Buffer API</b>
  31. *
  32. * @b Description: This file declares the NvBuffer APIs.
  33. */
  34. #ifndef __NV_BUFFER_H__
  35. #define __NV_BUFFER_H__
  36. #include <linux/videodev2.h>
  37. #include <pthread.h>
  38. #include <stdint.h>
  39. #include "v4l2_nv_extensions.h"
  40. /**
  41. *
  42. * @defgroup l4t_mm_nvbuffer_group Buffer API
  43. *
  44. * The @c %NvBuffer API provides buffer functionality, including reference
  45. * count functionality and convenience methods.
  46. * @ingroup aa_framework_api_group
  47. * @{
  48. */
  49. /**
  50. * Specifies the maximum number of planes a buffer can contain.
  51. */
  52. #define MAX_PLANES 3
  53. /**
  54. * @brief Class representing a buffer.
  55. *
  56. * The NvBuffer class is modeled on the basis of the @c v4l2_buffer
  57. * structure. The buffer has @c buf_type @c v4l2_buf_type, @c
  58. * memory_type @c v4l2_memory, and an index. It contains an
  59. * NvBufferPlane array similar to the array of @c v4l2_plane
  60. * structures in @c v4l2_buffer.m.planes. It also contains a
  61. * corresponding NvBufferPlaneFormat array that describes the
  62. * format of each of the planes.
  63. *
  64. * Even though @c %NvBuffer closely resembles v4l2 structures, it can
  65. * be easily used with other non-v4l2 components. @c %NvBuffer
  66. * contains data pointers, buffer length, file descriptor (FD) of
  67. * buffer planes, buffer format (height, width, stride, etc.), and
  68. * other members that are required by such components.
  69. *
  70. * This class also provides buffer reference count functionality. This
  71. * is useful when the same buffer is being used by multiple elements.
  72. *
  73. * In the case of a V4L2 MMAP, this class provides convenience methods
  74. * for mapping or unmapping the contents of the buffer to or from
  75. * memory, allocating or deallocating software memory depending on its
  76. * format.
  77. */
  78. class NvBuffer
  79. {
  80. public:
  81. /**
  82. * Holds the buffer plane format.
  83. */
  84. typedef struct
  85. {
  86. uint32_t width; /**< Holds the width of the plane in pixels. */
  87. uint32_t height; /**< Holds the height of the plane in pixels. */
  88. uint32_t bytesperpixel; /**< Holds the bytes used to represent one
  89. pixel in the plane. */
  90. uint32_t stride; /**< Holds the stride of the plane in bytes. */
  91. uint32_t sizeimage; /**< Holds the size of the plane in bytes. */
  92. } NvBufferPlaneFormat;
  93. /**
  94. * Holds the buffer plane parameters.
  95. */
  96. typedef struct
  97. {
  98. NvBufferPlaneFormat fmt; /**< Holds the format of the plane. */
  99. unsigned char *data; /**< Holds a pointer to the plane memory. */
  100. uint32_t bytesused; /**< Holds the number of valid bytes in the plane. */
  101. int fd; /**< Holds the file descriptor (FD) of the plane of the
  102. exported buffer, in the case of V4L2 MMAP buffers. */
  103. uint32_t mem_offset; /**< Holds the offset of the first valid byte
  104. from the data pointer. */
  105. uint32_t length; /**< Holds the size of the buffer in bytes. */
  106. } NvBufferPlane;
  107. /**
  108. * Creates a new NvBuffer object.
  109. *
  110. * This convenience method for V4L2 elements creates a new buffer
  111. * with the planes array memset to zero and the refcount
  112. * initialized to zero.
  113. *
  114. * @param[in] buf_type Type of buffer, enumerated as @c
  115. * v4l2_buf_type.
  116. * @param[in] memory_type @c %NvBuffer memory, enumerated as an
  117. * @c v4l2_memory enum.
  118. * @param[in] n_planes Number of planes in the buffer.
  119. * @param[in] fmt Specifies a pointer to the array of buffer plane formats.
  120. * Should contain at least @a n_planes elements.
  121. * @param[in] index Index of the buffer in the plane.
  122. */
  123. NvBuffer(enum v4l2_buf_type buf_type, enum v4l2_memory memory_type,
  124. uint32_t n_planes, NvBufferPlaneFormat *fmt, uint32_t index);
  125. /**
  126. * Creates a new NvBuffer for raw pixel formats.
  127. *
  128. * This convenience method for V4L2 elements is an @c %NvBuffer
  129. * constructor for raw pixel formats only. It requires width,
  130. * height, and pixel format to be specified.
  131. *
  132. * The planes array is memset to zero and the refcount is
  133. * initialized to zero.
  134. *
  135. * @attention The memory must be allocated by the application
  136. * by calling NvBuffer::allocateMemory.
  137. *
  138. * @param[in] pixfmt Pixel format of the buffer.
  139. * @param[in] width Width of the buffer in pixels.
  140. * @param[in] height Height of the buffer in pixels.
  141. * @param[in] index Index/ID of the buffer.
  142. */
  143. NvBuffer(uint32_t pixfmt, uint32_t width, uint32_t height, uint32_t index);
  144. /**
  145. * Creates a new NvBuffer object for non-raw pixel formats.
  146. *
  147. * This convenience method for V4L2 elements is an @c %NvBuffer
  148. * constructor for non raw pixel formats. It requires size of the
  149. * buffer to be supplied.
  150. *
  151. * The planes array is memset to zero and refcount initialized to
  152. * zero.
  153. *
  154. * @attention The memory needs to be allocated by the application
  155. * by calling NvBuffer::allocateMemory.
  156. *
  157. * @param[in] size Size of the buffer in bytes.
  158. * @param[in] index Index/ID of the buffer.
  159. */
  160. NvBuffer(uint32_t size, uint32_t index);
  161. /**
  162. * Destroys an NvBuffer object.
  163. *
  164. * This method cleans up class instances, unmapping any mapped
  165. * planes.
  166. */
  167. ~NvBuffer();
  168. /**
  169. * Maps the contents of the buffer to memory.
  170. *
  171. * This method maps the file descriptor (FD) of the planes to
  172. * a data pointer of @c planes. (MMAP buffers only.)
  173. *
  174. * @return 0 on success, -1 otherwise.
  175. */
  176. int map();
  177. /**
  178. * Unmaps the contents of the buffer from memory. (MMAP buffers only.)
  179. *
  180. */
  181. void unmap();
  182. /**
  183. * Allocates software memory for the buffer.
  184. *
  185. * @warning This method works only for @c V4L2_MEMORY_USERPTR memory.
  186. *
  187. * This method allocates memory on the basis of the buffer format:
  188. * @a height, @a width, @a bytesperpixel, and @a sizeimage.
  189. *
  190. * @return 0 for success, -1 otherwise.
  191. */
  192. int allocateMemory();
  193. /**
  194. * Deallocates buffer memory.
  195. *
  196. * @warning This method works only for @c V4L2_MEMORY_USERPTR memory and if
  197. * the memory was previously allocated using NvBuffer::allocateMemory.
  198. */
  199. void deallocateMemory();
  200. /**
  201. * Increases the reference count of the buffer.
  202. *
  203. * This method is thread safe.
  204. *
  205. * @return Reference count of the buffer after the operation.
  206. */
  207. int ref();
  208. /**
  209. * Decreases the reference count of the buffer.
  210. *
  211. * This thread-safe method decreases the buffer reference count if the
  212. * buffer reference count is above 0.
  213. *
  214. * @return Reference count of the buffer after the operation.
  215. */
  216. int unref();
  217. const enum v4l2_buf_type buf_type; /**< Type of the buffer. */
  218. const enum v4l2_memory memory_type; /**< Type of memory associated
  219. with the buffer. */
  220. const uint32_t index; /**< Holds the buffer index. */
  221. uint32_t n_planes; /**< Holds the number of planes in the buffer. */
  222. NvBufferPlane planes[MAX_PLANES]; /**< Holds the data pointer, plane file
  223. descriptor (FD), plane format, etc. */
  224. /**
  225. * Fills the NvBuffer::NvBufferPlaneFormat array.
  226. *
  227. * This convenience method populates the
  228. * @c %NvBuffer::NvBufferPlaneFormat array on the basis of @a width,
  229. * @a height and pixel format (@a raw_pixfmt). It also returns the number of planes
  230. * required for the pixel format in @a num_planes.
  231. *
  232. *
  233. * @param[out] num_planes The number of planes. Must not be NULL.
  234. * @param[in,out] planefmts Array of %NvBuffer::NvBufferPlaneFormat to
  235. * fill. Must be at least \a num_planes in length. For best
  236. * results, pass an array of length #MAX_PLANES.
  237. * @param[in] width Width of the buffer in pixels.
  238. * @param[in] height Height of the buffer in pixels.
  239. * @param[in] raw_pixfmt Raw V4L2 pixel formats.
  240. * @return 0 for success, -1 for an unsupported pixel format.
  241. */
  242. static int fill_buffer_plane_format(uint32_t *num_planes,
  243. NvBuffer::NvBufferPlaneFormat *planefmts,
  244. uint32_t width, uint32_t height, uint32_t raw_pixfmt);
  245. private:
  246. uint32_t ref_count; /**< Holds the reference count of the buffer. */
  247. pthread_mutex_t ref_lock; /**< Mutex to synchronize increment/
  248. decrement operations of @c ref_count. */
  249. bool mapped; /**< Indicates if the buffer is mapped to
  250. memory. */
  251. bool allocated; /**< Indicates if the buffer is allocated
  252. memory. */
  253. NvBuffer *shared_buffer; /**< If this is a DMABUF buffer, @c shared_buffer
  254. points to the MMAP @c NvBuffer whose FD was
  255. sent when this buffer was queued. */
  256. /**
  257. * Disallows copy constructor.
  258. */
  259. NvBuffer(const NvBuffer& that);
  260. /**
  261. * Disallows assignment.
  262. */
  263. void operator=(NvBuffer const&);
  264. friend class NvV4l2ElementPlane;
  265. };
  266. /** @} */
  267. #endif