NvV4l2Element.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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: V4L2 Helper Class</b>
  31. *
  32. * @b Description: This file declares a helper class for V4L2-based components.
  33. */
  34. /**
  35. * @defgroup l4t_mm_nvv4l2element_group V4L2 Element Class
  36. * @ingroup l4t_mm_nvelement_group
  37. *
  38. * Helper class that provides common functionality for V4L2-based components,
  39. * such as encoder and decoder. Objects instantiated from this class create
  40. * new V4L2 elements, subscribes to V4L2 events, dequeues an event from
  41. * an element, and sets/gets control values.
  42. * @{
  43. */
  44. #ifndef __NV_V4L2_ELEMENT_H__
  45. #define __NV_V4L2_ELEMENT_H__
  46. #include "NvElement.h"
  47. #include "NvV4l2ElementPlane.h"
  48. #include "v4l2_nv_extensions.h"
  49. /**
  50. * Defines a helper class for V4L2 based components.
  51. *
  52. * This derived class provides common functionality for V4L2 components. V4L2-based
  53. * components such as encoder/decoder extend from this class.
  54. *
  55. * This class is modeled on V4L2 M2M devices. It includes the file descriptor (FD) of the device
  56. * opened using %v4l2_open, two planes (NvV4l2ElementPlane), output plane,
  57. * capture plane, and other helper methods, such as setting/getting controls,
  58. * subscribing/dequeueing events, etc.
  59. */
  60. class NvV4l2Element:public NvElement
  61. {
  62. public:
  63. virtual ~NvV4l2Element();
  64. /**
  65. * Subscribes to an V4L2 event.
  66. *
  67. * Calls \c VIDIOC_SUBSCRIBE_EVENT IOCTL internally.
  68. *
  69. * @param[in] type Type of the event.
  70. * @param[in] id ID of the event source.
  71. * @param[in] flags Event flags.
  72. * @return 0 for success, -1 otherwise.
  73. */
  74. int subscribeEvent(uint32_t type, uint32_t id, uint32_t flags);
  75. /**
  76. * Dequeues an event from the element.
  77. *
  78. * Calls \c VIDIOC_DQEVENT IOCTL internally. The caller can specify the maximum time
  79. * to wait for dequeuing the event. The call blocks until an event is
  80. * dequeued successfully or timeout is reached.
  81. *
  82. * @param[in,out] event A reference to the \c v4l2_event structure to fill.
  83. * @param[in] max_wait_ms Specifies the max wait time for dequeuing an event,
  84. * in milliseconds.
  85. * @return 0 for success, -1 otherwise.
  86. */
  87. int dqEvent(struct v4l2_event &event, uint32_t max_wait_ms);
  88. /**
  89. * Sets the value of a control.
  90. *
  91. * Calls \c VIDIOC_S_CTRL IOCTL internally.
  92. *
  93. * @param[in] id ID of the control to be set.
  94. * @param[in] value Value to be set on the control.
  95. * @return 0 for success, -1 otherwise.
  96. */
  97. int setControl(uint32_t id, int32_t value);
  98. /**
  99. * Gets the value of a control.
  100. *
  101. * Calls \c VIDIOC_G_CTRL IOCTL internally.
  102. *
  103. * @param[in] id ID of the control to get.
  104. * @param[out] value A reference to the variable into which the control value
  105. is read.
  106. * @return 0 for success, -1 otherwise.
  107. */
  108. int getControl(uint32_t id, int32_t &value);
  109. /**
  110. * Sets the value of several controls.
  111. *
  112. * Calls \c VIDIOC_S_EXT_CTRLS IOCTL internally.
  113. *
  114. * @param[in] ctl A pointer to the controls to set.
  115. * @return 0 for success, -1 otherwise.
  116. */
  117. int setExtControls(struct v4l2_ext_controls &ctl);
  118. /**
  119. * Gets the value of several controls.
  120. *
  121. * Calls \c VIDIOC_G_EXT_CTRLS IOCTL internally.
  122. *
  123. * @param[in,out] ctl A pointer to the controls to get.
  124. * @return 0 for success, -1 otherwise.
  125. */
  126. int getExtControls(struct v4l2_ext_controls &ctl);
  127. virtual int isInError();
  128. /**
  129. * Sets the output plane.
  130. */
  131. NvV4l2ElementPlane output_plane; /**< Output plane of the element */
  132. /**
  133. * Sets the capture plane.
  134. */
  135. NvV4l2ElementPlane capture_plane; /**< Capture plane of the element */
  136. /**
  137. *
  138. * Terminates processing of queued buffers immediately. All the buffers are
  139. * returned to the application.
  140. *
  141. * Calls VIDIOC_STREAMOFF IOCTL on both of the planes internally.
  142. *
  143. * @return 0 for success, -1 otherwise.
  144. */
  145. int abort();
  146. /**
  147. * Waits until the element processes all the output plane buffers.
  148. *
  149. * Objects extending @c V4l2Element must implement this because the idle
  150. * condition is component-specific.
  151. *
  152. * @param[in] max_wait_ms Max time to wait in milliseconds.
  153. * @return 0 for success, -1 otherwise.
  154. */
  155. virtual int waitForIdle(uint32_t max_wait_ms);
  156. void *app_data; /**< A pointer to the application-specific data. */
  157. /**
  158. * Enables profiling for the V4l2Element.
  159. *
  160. * Must be called before setting either plane formats.
  161. */
  162. void enableProfiling();
  163. protected:
  164. int fd; /**< Specifies the FD of the device opened using \c v4l2_open. */
  165. uint32_t output_plane_pixfmt; /**< Pixel format of output plane buffers */
  166. uint32_t capture_plane_pixfmt; /**< Pixel format of capture plane buffers */
  167. /**
  168. * Creates a new V4l2Element named \a name.
  169. *
  170. * This constructor calls v4l2_open on the \a dev_node. It sets an error if
  171. * v4l2_open fails.
  172. *
  173. * This function also checks if the device supports V4L2_CAP_VIDEO_M2M_MPLANE
  174. * capability.
  175. *
  176. * @param[in] comp_name A pointer to the unique name to identity the element instance.
  177. * @param[in] dev_node A pointer to /dev/ * node of the device.
  178. * @param[in] flags Flags with which to open the device.
  179. * @param[in] fields Profiler fields that are valid for the element.
  180. */
  181. NvV4l2Element(const char *comp_name, const char *dev_node, int flags, NvElementProfiler::ProfilerField fields);
  182. };
  183. /** @} */
  184. #endif