NvVideoDecoder.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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. #include "NvVideoDecoder.h"
  29. #include "NvLogging.h"
  30. #include <cstring>
  31. #include <errno.h>
  32. #include <libv4l2.h>
  33. #define DECODER_DEV "/dev/nvhost-nvdec"
  34. #define CAT_NAME "NVDEC"
  35. #define CHECK_V4L2_RETURN(ret, str) \
  36. if (ret < 0) { \
  37. COMP_SYS_ERROR_MSG(str << ": failed"); \
  38. return -1; \
  39. } else { \
  40. COMP_DEBUG_MSG(str << ": success"); \
  41. return 0; \
  42. }
  43. #define RETURN_ERROR_IF_FORMATS_SET() \
  44. if (output_plane_pixfmt != 0) { \
  45. COMP_ERROR_MSG("Should be called before setting plane formats") \
  46. return -1; \
  47. }
  48. #define RETURN_ERROR_IF_BUFFERS_REQUESTED() \
  49. if (output_plane.getNumBuffers() != 0 && capture_plane.getNumBuffers() != 0) { \
  50. COMP_ERROR_MSG("Should be called before requesting buffers on either plane") \
  51. return -1; \
  52. }
  53. #define RETURN_ERROR_IF_FORMATS_NOT_SET() \
  54. if (output_plane_pixfmt == 0) { \
  55. COMP_ERROR_MSG("Should be called after setting plane formats") \
  56. return -1; \
  57. }
  58. using namespace std;
  59. NvVideoDecoder::NvVideoDecoder(const char *name, int flags)
  60. :NvV4l2Element(name, DECODER_DEV, flags, valid_fields)
  61. {
  62. }
  63. NvVideoDecoder *
  64. NvVideoDecoder::createVideoDecoder(const char *name, int flags)
  65. {
  66. NvVideoDecoder *dec = new NvVideoDecoder(name, flags);
  67. if (dec->isInError())
  68. {
  69. delete dec;
  70. return NULL;
  71. }
  72. return dec;
  73. }
  74. NvVideoDecoder::~NvVideoDecoder()
  75. {
  76. }
  77. int
  78. NvVideoDecoder::setCapturePlaneFormat(uint32_t pixfmt, uint32_t width,
  79. uint32_t height)
  80. {
  81. struct v4l2_format format;
  82. uint32_t num_bufferplanes;
  83. NvBuffer::NvBufferPlaneFormat planefmts[MAX_PLANES];
  84. if (! ((pixfmt == V4L2_PIX_FMT_NV12M) || (pixfmt == V4L2_PIX_FMT_P010M) || (pixfmt == V4L2_PIX_FMT_YUV422M) ||
  85. (pixfmt == V4L2_PIX_FMT_NV24M) || (pixfmt == V4L2_PIX_FMT_NV24_10LE)))
  86. {
  87. COMP_ERROR_MSG("Only NV12M, P010M, YUV422M, NV24M and NV24_10LE is supported");
  88. return -1;
  89. }
  90. capture_plane_pixfmt = pixfmt;
  91. NvBuffer::fill_buffer_plane_format(&num_bufferplanes, planefmts, width,
  92. height, pixfmt);
  93. capture_plane.setBufferPlaneFormat(num_bufferplanes, planefmts);
  94. memset(&format, 0, sizeof(struct v4l2_format));
  95. format.type = capture_plane.getBufType();
  96. format.fmt.pix_mp.width = width;
  97. format.fmt.pix_mp.height = height;
  98. format.fmt.pix_mp.pixelformat = pixfmt;
  99. format.fmt.pix_mp.num_planes = num_bufferplanes;
  100. return capture_plane.setFormat(format);
  101. }
  102. int
  103. NvVideoDecoder::setOutputPlaneFormat(uint32_t pixfmt, uint32_t sizeimage)
  104. {
  105. struct v4l2_format format;
  106. memset(&format, 0, sizeof(struct v4l2_format));
  107. format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
  108. switch (pixfmt)
  109. {
  110. case V4L2_PIX_FMT_H264:
  111. case V4L2_PIX_FMT_H265:
  112. case V4L2_PIX_FMT_VP8:
  113. case V4L2_PIX_FMT_VP9:
  114. case V4L2_PIX_FMT_AV1:
  115. case V4L2_PIX_FMT_MPEG2:
  116. case V4L2_PIX_FMT_MPEG4:
  117. case V4L2_PIX_FMT_MJPEG:
  118. output_plane_pixfmt = pixfmt;
  119. break;
  120. default:
  121. COMP_ERROR_MSG("Unsupported pixel format for decoder output plane "
  122. << pixfmt);
  123. return -1;
  124. }
  125. format.fmt.pix_mp.pixelformat = pixfmt;
  126. format.fmt.pix_mp.num_planes = 1;
  127. format.fmt.pix_mp.plane_fmt[0].sizeimage = sizeimage;
  128. return output_plane.setFormat(format);
  129. }
  130. int
  131. NvVideoDecoder::disableDPB()
  132. {
  133. struct v4l2_ext_control control;
  134. struct v4l2_ext_controls ctrls;
  135. RETURN_ERROR_IF_FORMATS_NOT_SET();
  136. RETURN_ERROR_IF_BUFFERS_REQUESTED();
  137. memset(&control, 0, sizeof(control));
  138. memset(&ctrls, 0, sizeof(ctrls));
  139. ctrls.count = 1;
  140. ctrls.controls = &control;
  141. control.id = V4L2_CID_MPEG_VIDEO_DISABLE_DPB;
  142. CHECK_V4L2_RETURN(setExtControls(ctrls),
  143. "Disabling decoder DPB");
  144. }
  145. int
  146. NvVideoDecoder::disableCompleteFrameInputBuffer()
  147. {
  148. struct v4l2_ext_control control;
  149. struct v4l2_ext_controls ctrls;
  150. RETURN_ERROR_IF_FORMATS_NOT_SET();
  151. memset(&control, 0, sizeof(control));
  152. memset(&ctrls, 0, sizeof(ctrls));
  153. ctrls.count = 1;
  154. ctrls.controls = &control;
  155. control.id = V4L2_CID_MPEG_VIDEO_DISABLE_COMPLETE_FRAME_INPUT;
  156. control.value = 1;
  157. CHECK_V4L2_RETURN(setExtControls(ctrls),
  158. "Disabling decoder complete frame input buffer");
  159. }
  160. int
  161. NvVideoDecoder::setFrameInputMode(unsigned int ctrl_value)
  162. {
  163. struct v4l2_ext_control control;
  164. struct v4l2_ext_controls ctrls;
  165. RETURN_ERROR_IF_FORMATS_NOT_SET();
  166. memset(&control, 0, sizeof(control));
  167. memset(&ctrls, 0, sizeof(ctrls));
  168. ctrls.count = 1;
  169. ctrls.controls = &control;
  170. control.id = V4L2_CID_MPEG_VIDEO_DISABLE_COMPLETE_FRAME_INPUT;
  171. control.value = ctrl_value;
  172. CHECK_V4L2_RETURN(setExtControls(ctrls),
  173. "Setting decoder frame input mode to " << ctrl_value);
  174. }
  175. int
  176. NvVideoDecoder::setSliceMode(unsigned int ctrl_value)
  177. {
  178. struct v4l2_ext_control control;
  179. struct v4l2_ext_controls ctrls;
  180. RETURN_ERROR_IF_FORMATS_NOT_SET();
  181. memset(&control, 0, sizeof(control));
  182. memset(&ctrls, 0, sizeof(ctrls));
  183. ctrls.count = 1;
  184. ctrls.controls = &control;
  185. control.id = V4L2_CID_MPEG_VIDEO_DECODER_SLICE_INTERFACE;
  186. control.value = ctrl_value;
  187. CHECK_V4L2_RETURN(setExtControls(ctrls),
  188. "Setting decoder slice mode to " << ctrl_value);
  189. }
  190. int
  191. NvVideoDecoder::getMinimumCapturePlaneBuffers(int &num)
  192. {
  193. CHECK_V4L2_RETURN(getControl(V4L2_CID_MIN_BUFFERS_FOR_CAPTURE, num),
  194. "Getting decoder minimum capture plane buffers (" << num << ")");
  195. }
  196. int
  197. NvVideoDecoder::setSkipFrames(enum v4l2_skip_frames_type skip_frames)
  198. {
  199. struct v4l2_ext_control control;
  200. struct v4l2_ext_controls ctrls;
  201. RETURN_ERROR_IF_FORMATS_NOT_SET();
  202. RETURN_ERROR_IF_BUFFERS_REQUESTED();
  203. memset(&control, 0, sizeof(control));
  204. memset(&ctrls, 0, sizeof(ctrls));
  205. ctrls.count = 1;
  206. ctrls.controls = &control;
  207. control.id = V4L2_CID_MPEG_VIDEO_SKIP_FRAMES;
  208. control.value = skip_frames;
  209. CHECK_V4L2_RETURN(setExtControls(ctrls),
  210. "Setting decoder skip frames to " << skip_frames);
  211. }
  212. int
  213. NvVideoDecoder::setMaxPerfMode(int flag)
  214. {
  215. struct v4l2_ext_control control;
  216. struct v4l2_ext_controls ctrls;
  217. RETURN_ERROR_IF_FORMATS_NOT_SET();
  218. RETURN_ERROR_IF_BUFFERS_REQUESTED();
  219. memset(&control, 0, sizeof(control));
  220. memset(&ctrls, 0, sizeof(ctrls));
  221. ctrls.count = 1;
  222. ctrls.controls = &control;
  223. control.id = V4L2_CID_MPEG_VIDEO_MAX_PERFORMANCE;
  224. control.value = flag;
  225. CHECK_V4L2_RETURN(setExtControls(ctrls),
  226. "Enabling Maximum Performance ");
  227. }
  228. int
  229. NvVideoDecoder::enableMetadataReporting()
  230. {
  231. struct v4l2_ext_control control;
  232. struct v4l2_ext_controls ctrls;
  233. RETURN_ERROR_IF_FORMATS_NOT_SET();
  234. RETURN_ERROR_IF_BUFFERS_REQUESTED();
  235. memset(&control, 0, sizeof(control));
  236. memset(&ctrls, 0, sizeof(ctrls));
  237. ctrls.count = 1;
  238. ctrls.controls = &control;
  239. control.id = V4L2_CID_MPEG_VIDEO_ERROR_REPORTING;
  240. CHECK_V4L2_RETURN(setExtControls(ctrls),
  241. "Enabling decoder output metadata reporting");
  242. }
  243. int
  244. NvVideoDecoder::getMetadata(uint32_t buffer_index,
  245. v4l2_ctrl_videodec_outputbuf_metadata &dec_metadata)
  246. {
  247. v4l2_ctrl_video_metadata metadata;
  248. struct v4l2_ext_control control;
  249. struct v4l2_ext_controls ctrls;
  250. ctrls.count = 1;
  251. ctrls.controls = &control;
  252. metadata.buffer_index = buffer_index;
  253. metadata.VideoDecMetadata = &dec_metadata;
  254. control.id = V4L2_CID_MPEG_VIDEODEC_METADATA;
  255. control.string = (char *)&metadata;
  256. CHECK_V4L2_RETURN(getExtControls(ctrls),
  257. "Getting decoder output metadata for buffer " << buffer_index);
  258. }
  259. int
  260. NvVideoDecoder::getInputMetadata(uint32_t buffer_index,
  261. v4l2_ctrl_videodec_inputbuf_metadata &dec_input_metadata)
  262. {
  263. v4l2_ctrl_video_metadata metadata;
  264. struct v4l2_ext_control control;
  265. struct v4l2_ext_controls ctrls;
  266. ctrls.count = 1;
  267. ctrls.controls = &control;
  268. metadata.buffer_index = buffer_index;
  269. metadata.VideoDecHeaderErrorMetadata = &dec_input_metadata;
  270. control.id = V4L2_CID_MPEG_VIDEODEC_INPUT_METADATA;
  271. control.string = (char *)&metadata;
  272. CHECK_V4L2_RETURN(getExtControls(ctrls),
  273. "Getting decoder input metadata for buffer " << buffer_index);
  274. }
  275. int
  276. NvVideoDecoder::getSAR(uint32_t &sar_width, uint32_t &sar_height)
  277. {
  278. struct v4l2_ext_control controls[2];
  279. struct v4l2_ext_controls ctrls;
  280. ctrls.count = 2;
  281. ctrls.controls = controls;
  282. controls[0].id = V4L2_CID_MPEG_VIDEODEC_SAR_WIDTH;
  283. controls[0].string = (char *)&sar_width;
  284. controls[1].id = V4L2_CID_MPEG_VIDEODEC_SAR_HEIGHT;
  285. controls[1].string = (char *)&sar_height;
  286. CHECK_V4L2_RETURN(getExtControls(ctrls),
  287. "Getting decoder SAR width and height");
  288. }
  289. int
  290. NvVideoDecoder::checkifMasteringDisplayDataPresent(v4l2_ctrl_video_displaydata
  291. &displaydata)
  292. {
  293. struct v4l2_ext_control control;
  294. struct v4l2_ext_controls ctrls;
  295. ctrls.count = 1;
  296. ctrls.controls = &control;
  297. control.id = V4L2_CID_VIDEODEC_DISPLAYDATA_PRESENT;
  298. control.string = (char *)&displaydata;
  299. CHECK_V4L2_RETURN(getExtControls(ctrls),
  300. "Getting decoder output displaydata for buffer ");
  301. }
  302. int
  303. NvVideoDecoder::MasteringDisplayData(v4l2_ctrl_video_hdrmasteringdisplaydata
  304. *hdrmasteringdisplaydata)
  305. {
  306. struct v4l2_ext_control control;
  307. struct v4l2_ext_controls ctrls;
  308. ctrls.count = 1;
  309. ctrls.controls = &control;
  310. control.id = V4L2_CID_VIDEODEC_HDR_MASTERING_DISPLAY_DATA;
  311. control.string = (char *)hdrmasteringdisplaydata;
  312. CHECK_V4L2_RETURN(getExtControls(ctrls),
  313. "Getting decoder output hdrdisplaydata for buffer ");
  314. }
  315. int
  316. NvVideoDecoder::DevicePoll(v4l2_ctrl_video_device_poll *devicepoll)
  317. {
  318. struct v4l2_ext_control control;
  319. struct v4l2_ext_controls ctrls;
  320. RETURN_ERROR_IF_FORMATS_NOT_SET();
  321. memset(&control, 0, sizeof(control));
  322. memset(&ctrls, 0, sizeof(ctrls));
  323. ctrls.count = 1;
  324. ctrls.controls = &control;
  325. control.id = V4L2_CID_MPEG_VIDEO_DEVICE_POLL;
  326. control.string = (char *)devicepoll;
  327. CHECK_V4L2_RETURN(setExtControls(ctrls),
  328. "Done calling video device poll ");
  329. }
  330. int
  331. NvVideoDecoder::SetPollInterrupt()
  332. {
  333. struct v4l2_ext_control control;
  334. struct v4l2_ext_controls ctrls;
  335. RETURN_ERROR_IF_FORMATS_NOT_SET();
  336. memset(&control, 0, sizeof(control));
  337. memset(&ctrls, 0, sizeof(ctrls));
  338. ctrls.count = 1;
  339. ctrls.controls = &control;
  340. control.id = V4L2_CID_MPEG_SET_POLL_INTERRUPT;
  341. control.value = 1;
  342. CHECK_V4L2_RETURN(setExtControls(ctrls),
  343. "Setting decoder poll interrupt to 1 ");
  344. }
  345. int
  346. NvVideoDecoder::ClearPollInterrupt()
  347. {
  348. struct v4l2_ext_control control;
  349. struct v4l2_ext_controls ctrls;
  350. RETURN_ERROR_IF_FORMATS_NOT_SET();
  351. memset(&control, 0, sizeof(control));
  352. memset(&ctrls, 0, sizeof(ctrls));
  353. ctrls.count = 1;
  354. ctrls.controls = &control;
  355. control.id = V4L2_CID_MPEG_SET_POLL_INTERRUPT;
  356. control.value = 0;
  357. CHECK_V4L2_RETURN(setExtControls(ctrls),
  358. "Setting decoder poll interrupt to 0 ");
  359. }