NvVideoDecoder.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /*
  2. * Copyright (c) 2016-2023, 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: Video Decode API</b>
  31. *
  32. */
  33. /**
  34. * @defgroup l4t_mm_nvvideodecoder_group Video Decoder
  35. * @ingroup l4t_mm_nvvideo_group
  36. *
  37. * Helper class that creates new V4L2
  38. * video decoders, and it sets decoder capture and output plane
  39. * formats.
  40. * @{
  41. */
  42. #ifndef __NV_VIDEO_DECODER_H__
  43. #define __NV_VIDEO_DECODER_H__
  44. #include "NvV4l2Element.h"
  45. /**
  46. * @brief Defines a helper class for V4L2 Video Decoder.
  47. *
  48. * The video decoder device node is `/dev/nvhost-nvdec`. The category name
  49. * for the decoder is \c "NVDEC".
  50. *
  51. * Refer to [V4L2 Video Decoder](group__V4L2Dec.html) for more information on the decoder.
  52. */
  53. class NvVideoDecoder:public NvV4l2Element
  54. {
  55. public:
  56. /**
  57. * Creates a new V4L2 Video Decoder object named \a name.
  58. *
  59. * This method internally calls \c v4l2_open on the decoder dev node
  60. * \c "/dev/nvhost-nvdec" and checks for \c V4L2_CAP_VIDEO_M2M_MPLANE
  61. * capability on the device. This method allows the caller to specify
  62. * additional flags with which the device should be opened.
  63. *
  64. * The device is opened in blocking mode, which can be modified by passing
  65. * the @a O_NONBLOCK flag to this method.
  66. *
  67. * @returns Reference to the newly created decoder object else \a NULL in
  68. * case of failure during initialization.
  69. */
  70. static NvVideoDecoder *createVideoDecoder(const char *name, int flags = 0);
  71. ~NvVideoDecoder();
  72. /**
  73. * Sets the format on the decoder output plane.
  74. *
  75. * Calls \c VIDIOC_S_FMT IOCTL internally on the capture plane.
  76. *
  77. * @param[in] pixfmt One of the raw V4L2 pixel formats.
  78. * @param[in] width Width of the output buffers in pixels.
  79. * @param[in] height Height of the output buffers in pixels.
  80. * @return 0 for success, -1 otherwise.
  81. */
  82. int setCapturePlaneFormat(uint32_t pixfmt, uint32_t width, uint32_t height);
  83. /**
  84. * Sets the format on the decoder output plane.
  85. *
  86. * Calls the \c VIDIOC_S_FMT IOCTL internally on the output plane.
  87. *
  88. * @param[in] pixfmt One of the coded V4L2 pixel formats.
  89. * @param[in] sizeimage Maximum size of the buffers on the output plane.
  90. containing encoded data in bytes.
  91. * @return 0 for success, -1 otherwise.
  92. */
  93. int setOutputPlaneFormat(uint32_t pixfmt, uint32_t sizeimage);
  94. /**
  95. * Informs the decoder that input buffers may not contain complete frames.
  96. * Deprecated interface, Use setFrameInputMode instead.
  97. *
  98. * Calls the VIDIOC_S_EXT_CTRLS IOCTL internally with Control ID
  99. * V4L2_CID_MPEG_VIDEO_DISABLE_COMPLETE_FRAME_INPUT.
  100. *
  101. * @return 0 for success, -1 otherwise.
  102. */
  103. int disableCompleteFrameInputBuffer();
  104. /**
  105. * Informs the decoder that input buffers may not contain complete frames.
  106. *
  107. * Calls the VIDIOC_S_EXT_CTRLS IOCTL internally with Control ID
  108. * V4L2_CID_MPEG_VIDEO_DISABLE_COMPLETE_FRAME_INPUT.
  109. *
  110. * @param[in] ctrl_value control value to disable
  111. * complete frame input buffer.
  112. * @return 0 for success, -1 otherwise.
  113. */
  114. int setFrameInputMode(unsigned int ctrl_value);
  115. /**
  116. * Enable slice level decoding for HEVC.
  117. *
  118. * Calls the VIDIOC_S_EXT_CTRLS IOCTL internally with Control ID
  119. * V4L2_CID_MPEG_VIDEO_DECODER_SLICE_INTERFACE.
  120. *
  121. * @param[in] ctrl_value control value to enable
  122. * slice level decoding.
  123. * @return 0 for success, -1 otherwise.
  124. */
  125. int setSliceMode(unsigned int ctrl_value);
  126. /**
  127. * Disables the display picture buffer.
  128. *
  129. * Calls the VIDIOC_S_EXT_CTRLS IOCTL internally with Control ID
  130. * V4L2_CID_MPEG_VIDEO_DISABLE_DPB. Must be called after setFormat on both
  131. * the planes and before requestBuffers on any of the planes.
  132. *
  133. * @return 0 for success, -1 otherwise.
  134. */
  135. int disableDPB();
  136. /**
  137. * Gets the minimum number of buffers to be requested on the decoder capture plane.
  138. *
  139. * Calls the VIDIOC_G_CTRL IOCTL internally with Control ID
  140. * V4L2_CID_MIN_BUFFERS_FOR_CAPTURE. It is valid after the first
  141. * V4L2_RESOLUTION_CHANGE_EVENT and may change after each subsequent
  142. * event.
  143. *
  144. * @param[out] num A reference to the integer to return the number of buffers.
  145. *
  146. * @return 0 for success, -1 otherwise.
  147. */
  148. int getMinimumCapturePlaneBuffers(int & num);
  149. /**
  150. * Sets the skip-frames parameter of the decoder.
  151. *
  152. * Calls the VIDIOC_S_EXT_CTRLS IOCTL internally with Control ID
  153. * V4L2_CID_MPEG_VIDEO_SKIP_FRAMES. Must be called after setFormat on both
  154. * the planes and before requestBuffers on any of the planes.
  155. *
  156. * @param[in] skip_frames Type of frames to skip decoding, one of
  157. * enum v4l2_skip_frames_type.
  158. *
  159. * @return 0 for success, -1 otherwise.
  160. */
  161. int setSkipFrames(enum v4l2_skip_frames_type skip_frames);
  162. /**
  163. * Sets the decoder for maximum performance.
  164. *
  165. * Calls the VIDIOC_S_EXT_CTRLS IOCTL internally with Control ID
  166. * V4L2_CID_MPEG_VIDEO_MAX_PERFORMANCE. Must be called after setFormat on both
  167. * the planes and before requestBuffers on any of the planes.
  168. *
  169. * @param[in] flag Integer variable to indicate whether max performance is to be
  170. * enabled/disabled.
  171. *
  172. * @return 0 for success, -1 otherwise.
  173. */
  174. int setMaxPerfMode(int flag);
  175. /**
  176. * Enables video decoder output metadata reporting.
  177. *
  178. * Calls the VIDIOC_S_EXT_CTRLS IOCTL internally with Control ID
  179. * V4L2_CID_MPEG_VIDEO_ERROR_REPORTING. Must be called after setFormat on
  180. * both the planes and before requestBuffers on any of the planes.
  181. *
  182. * @return 0 for success, -1 otherwise.
  183. */
  184. int enableMetadataReporting();
  185. int checkifMasteringDisplayDataPresent(v4l2_ctrl_video_displaydata &displaydata);
  186. int MasteringDisplayData(v4l2_ctrl_video_hdrmasteringdisplaydata *hdrmasteringdisplaydata);
  187. /**
  188. * Gets metadata for the decoded capture plane buffer.
  189. *
  190. * Calls the VIDIOC_G_EXT_CTRLS IOCTL internally with Control ID
  191. * V4L2_CID_MPEG_VIDEODEC_METADATA. Must be called for a buffer that has
  192. * been dequeued from the capture plane. The returned metadata corresponds
  193. * to the last dequeued buffer with index @a buffer_index.
  194. *
  195. * @param[in] buffer_index Index of the capture plane buffer whose metadata
  196. * is required.
  197. * @param[in,out] metadata Reference to the metadata structure
  198. * v4l2_ctrl_videodec_outputbuf_metadata to be filled.
  199. *
  200. * @return 0 for success, -1 otherwise.
  201. */
  202. int getMetadata(uint32_t buffer_index,
  203. v4l2_ctrl_videodec_outputbuf_metadata &metadata);
  204. /**
  205. * Gets metadata for the decoder output plane buffer.
  206. *
  207. * Calls the VIDIOC_G_EXT_CTRLS IOCTL internally with Control ID
  208. * V4L2_CID_MPEG_VIDEODEC_INPUT_METADATA. Must be called for a buffer that has
  209. * been dequeued from the output plane. The returned metadata corresponds
  210. * to the last dequeued buffer with index @a buffer_index.
  211. *
  212. * @param[in] buffer_index Index of the output plane buffer whose metadata
  213. * is required.
  214. * @param[in,out] input_metadata Reference to the metadata structure
  215. * v4l2_ctrl_videodec_inputbuf_metadata to be filled.
  216. *
  217. * @return 0 for success, -1 otherwise.
  218. */
  219. int getInputMetadata(uint32_t buffer_index,
  220. v4l2_ctrl_videodec_inputbuf_metadata &input_metadata);
  221. /**
  222. * Gets Sample Aspect Ratio (SAR) width and height for decoder.
  223. *
  224. * Calls the VIDIOC_G_EXT_CTRLS IOCTL internally with Control ID
  225. * V4L2_CID_MPEG_VIDEODEC_SAR_WIDTH and V4L2_CID_MPEG_VIDEODEC_SAR_HEIGHT.
  226. * Must be called after V4L2_EVENT_RESOLUTION_CHANGE is dequeued.
  227. *
  228. * @param[in,out] sar_width Reference to the SAR width to be filled.
  229. * @param[in,out] sar_height Reference to the SAR height to be filled.
  230. *
  231. * @return 0 for success, -1 otherwise.
  232. */
  233. int getSAR(uint32_t &sar_width, uint32_t &sar_height);
  234. /**
  235. * Issues Poll on the device which blocks until :
  236. * a) Either there is something to dequeue from capture or output plane or any events.
  237. * b) Poll was interrupted by a call to the device using V4L2_CID_SET_POLL_INTERRUPT
  238. * c) Application has already interrupted polling by V4L2_CID_SET_POLL_INTERRUPT
  239. */
  240. int DevicePoll(v4l2_ctrl_video_device_poll *devicepoll);
  241. /**
  242. * Sets the polling interrupt, now if the application calls Poll, the device should
  243. * not block, in other words polling is disabled.
  244. */
  245. int SetPollInterrupt();
  246. /**
  247. * Clears the polling interrupt, now if the application calls Poll, the device should
  248. * block until the event is triggered, in other words polling is enabled.
  249. */
  250. int ClearPollInterrupt();
  251. private:
  252. /**
  253. * Constructor used by #createVideoDecoder.
  254. */
  255. NvVideoDecoder(const char *name, int flags);
  256. static const NvElementProfiler::ProfilerField valid_fields =
  257. NvElementProfiler::PROFILER_FIELD_TOTAL_UNITS |
  258. NvElementProfiler::PROFILER_FIELD_FPS;
  259. };
  260. /** @} */
  261. #endif