NvVideoEncoder.h 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775
  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 Encode API</b>
  31. *
  32. */
  33. /**
  34. * @defgroup l4t_mm_nvvideoencoder_group Video Encoder
  35. * @ingroup l4t_mm_nvvideo_group
  36. *
  37. * Helper class that creates new V4L2
  38. * video encoders, and it sets encoder capture and output plane
  39. * formats.
  40. * @{
  41. */
  42. #ifndef __NV_VIDEO_ENCODER_H__
  43. #define __NV_VIDEO_ENCODER_H__
  44. #include "NvV4l2Element.h"
  45. /**
  46. * @brief Defines a helper class for V4L2 Video Encoder.
  47. *
  48. * The video encoder device node is \c "/dev/nvhost-msenc". The category name
  49. * for the encoder is \c "NVENC".
  50. *
  51. * Refer to [V4L2 Video Encoder](group__V4L2Enc.html) for more information on the encoder.
  52. */
  53. class NvVideoEncoder:public NvV4l2Element
  54. {
  55. public:
  56. /**
  57. * Creates a new V4L2 Video Encoder object named \a name.
  58. *
  59. * This method internally calls \c v4l2_open on the encoder dev node
  60. * \c "/dev/nvhost-msenc" 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 must 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 encoder object, else NULL in
  68. * case of failure during initialization.
  69. */
  70. static NvVideoEncoder *createVideoEncoder(const char *name, int flags = 0);
  71. ~NvVideoEncoder();
  72. /**
  73. * Sets the format on the encoder output plane.
  74. *
  75. * Calls \c VIDIOC_S_FMT IOCTL internally on the output plane.
  76. *
  77. * @pre Applications must set the capture plane format using #setCapturePlaneFormat before calling this method.
  78. *
  79. * @param[in] pixfmt One of the raw V4L2 pixel formats.
  80. * @param[in] width Width of the input buffers in pixels.
  81. * @param[in] height Height of the input buffers in pixels.
  82. * @return 0 for success, -1 otherwise.
  83. */
  84. int setOutputPlaneFormat(uint32_t pixfmt, uint32_t width, uint32_t height);
  85. /**
  86. * Sets the format on the converter capture plane.
  87. *
  88. * Calls \c VIDIOC_S_FMT IOCTL internally on the capture plane.
  89. *
  90. * @param[in] pixfmt One of the coded V4L2 pixel formats.
  91. * @param[in] width Width of the input buffers in pixels.
  92. * @param[in] height Height of the input buffers in pixels.
  93. * @param[in] sizeimage Maximum size of the encoded buffers on the capture.
  94. * plane in bytes
  95. * @return 0 for success, -1 otherwise.
  96. */
  97. int setCapturePlaneFormat(uint32_t pixfmt, uint32_t width,
  98. uint32_t height, uint32_t sizeimage);
  99. /**
  100. * Sets the encode framerate.
  101. *
  102. * Calls the VIDIOC_S_PARM IOCTL on the encoder capture plane. Can be
  103. * called any time after setFormat on both the planes.
  104. *
  105. * @param[in] framerate_num Numerator part of the framerate fraction.
  106. * @param[in] framerate_den Denominator part of the framerate fraction.
  107. *
  108. * @return 0 for success, -1 otherwise.
  109. */
  110. int setFrameRate(uint32_t framerate_num, uint32_t framerate_den);
  111. /**
  112. * Sets the encoder bitrate.
  113. *
  114. * Calls the VIDIOC_S_EXT_CTRLS IOCTL internally with Control ID
  115. * \c V4L2_CID_MPEG_VIDEO_BITRATE. Can be called any time after setFormat on
  116. * both the planes.
  117. *
  118. * @param[in] bitrate Bitrate of the encoded stream, in bits per second.
  119. *
  120. * @return 0 for success, -1 otherwise.
  121. */
  122. int setBitrate(uint32_t bitrate);
  123. /**
  124. * Sets the encoder peak bitrate.
  125. *
  126. * Calls the VIDIOC_S_EXT_CTRLS IOCTL internally with Control ID
  127. * \c V4L2_CID_MPEG_VIDEO_BITRATE_PEAK. Can be called any time after setFormat on
  128. * both the planes. Takes effect in VBR mode
  129. *
  130. * @param[in] peak_bitrate Peak Bitrate of the encoded stream, in bits per second.
  131. *
  132. * @return 0 for success, -1 otherwise.
  133. */
  134. int setPeakBitrate(uint32_t peak_bitrate);
  135. /**
  136. * Sets the encoder profile.
  137. *
  138. * Calls the VIDIOC_S_EXT_CTRLS IOCTL internally with Control ID
  139. * \c V4L2_CID_MPEG_VIDEO_H264_PROFILE or #V4L2_CID_MPEG_VIDEO_H265_PROFILE,
  140. * depending on the encoder type. Must be called after setFormat on both
  141. * the planes and before \c requestBuffers on any of the planes.
  142. *
  143. * @param[in] profile Profile to be used for encoding.
  144. *
  145. * @return 0 for success, -1 otherwise.
  146. */
  147. int setProfile(uint32_t profile);
  148. /**
  149. * Sets the encoder command.
  150. *
  151. * Calls the VIDIOC_ENCODER_CMD internally with encoder commands.
  152. *
  153. * @return 0 for succes, -1 otherwise.
  154. */
  155. int setEncoderCommand(int cmd, int flags);
  156. /**
  157. * Set the encoder level.
  158. *
  159. * Calls the VIDIOC_S_EXT_CTRLS IOCTL internally with Control ID
  160. * \c V4L2_CID_MPEG_VIDEO_H264_LEVEL or #V4L2_CID_MPEG_VIDEOENC_H265_LEVEL,
  161. * depending on encoder type. Must be called after setFormat on both
  162. * the planes and before \c requestBuffers on any of the planes.
  163. *
  164. * @param[in] level Level to be used for encoding
  165. *
  166. * @return 0 for success, -1 otherwise.
  167. */
  168. int setLevel(uint32_t level);
  169. /**
  170. * Sets the encoder for maximum performance.
  171. *
  172. * Calls the VIDIOC_S_EXT_CTRLS IOCTL internally with Control ID
  173. * V4L2_CID_MPEG_VIDEO_MAX_PERFORMANCE. Must be called after setFormat on both
  174. * the planes and before requestBuffers on any of the planes.
  175. *
  176. * @param[in] flag Integer variable to indicate whether max performance is to be
  177. * enabled/disabled.
  178. *
  179. * @return 0 for success, -1 otherwise.
  180. */
  181. int setMaxPerfMode(int flag);
  182. /**
  183. * Sets constant QP for encoder.
  184. *
  185. * Calls the VIDIOC_S_EXT_CTRLS IOCTL internally with Control ID
  186. * \c V4L2_CID_MPEG_VIDEO_FRAME_RC_ENABLE to enable/disable rate control.
  187. * If the value false is given, it disables RC and set constant QP parameters.
  188. * Must be called after setFormat on both
  189. * the planes and before \c requestBuffers on any of the planes.
  190. *
  191. * @param[in] enabled_rc Boolean value indicating whether to enable/disable
  192. * the control.
  193. *
  194. * @return 0 for success, -1 otherwise.
  195. */
  196. int setConstantQp(bool enabled_rc);
  197. /**
  198. * Sets the encoder rate control mode.
  199. *
  200. * Calls the VIDIOC_S_EXT_CTRLS IOCTL internally with Control ID
  201. * \c V4L2_CID_MPEG_VIDEO_BITRATE_MODE. Must be called after setFormat on both
  202. * the planes and before \c requestBuffers on any of the planes.
  203. *
  204. * @param[in] mode Type of rate control, one of enum
  205. * v4l2_mpeg_video_bitrate_mode.
  206. *
  207. * @return 0 for success, -1 otherwise.
  208. */
  209. int setRateControlMode(enum v4l2_mpeg_video_bitrate_mode mode);
  210. /**
  211. * Sets the encoder I-frame interval.
  212. *
  213. * Calls the VIDIOC_S_EXT_CTRLS IOCTL internally with Control ID
  214. * \c V4L2_CID_MPEG_VIDEO_GOP_SIZE. Must be called after setFormat on both
  215. * the planes and before \c requestBuffers on any of the planes.
  216. *
  217. * @param[in] interval Interval between two I frames, in number of frames.
  218. *
  219. * @return 0 for success, -1 otherwise.
  220. */
  221. int setIFrameInterval(uint32_t interval);
  222. /**
  223. * Sets the encoder IDR interval.
  224. *
  225. * Calls the VIDIOC_S_EXT_CTRLS IOCTL internally with Control ID
  226. * #V4L2_CID_MPEG_VIDEO_IDR_INTERVAL. Must be called after setFormat on both
  227. * the planes and before \c requestBuffers on any of the planes.
  228. *
  229. * @param[in] interval Interval between two IDR frames, in number of frames.
  230. *
  231. * @return 0 for success, -1 otherwise.
  232. */
  233. int setIDRInterval(uint32_t interval);
  234. /**
  235. * Forces an IDR frame.
  236. *
  237. * Calls the VIDIOC_S_EXT_CTRLS IOCTL internally with Control ID
  238. * \c V4L2_CID_MPEG_MFC51_VIDEO_FORCE_FRAME_TYPE. Must be called after
  239. * setFormat on both the planes.
  240. *
  241. * @return 0 for success, -1 otherwise.
  242. */
  243. int forceIDR();
  244. /**
  245. * Sets the encoder Temporal Tradeoff.
  246. *
  247. * Calls the VIDIOC_S_EXT_CTRLS IOCTL internally with Control ID
  248. * #V4L2_CID_MPEG_VIDEOENC_TEMPORAL_TRADEOFF_LEVEL. Must be called after
  249. * setFormat on both the planes and before \c requestBuffers on any of the
  250. * planes.
  251. *
  252. * @param[in] level Temporal tradeoff level, one of
  253. * v4l2_enc_temporal_tradeoff_level_type.
  254. * @return 0 for success, -1 otherwise.
  255. */
  256. int setTemporalTradeoff(v4l2_enc_temporal_tradeoff_level_type level);
  257. /**
  258. * Sets the encoder output slice length.
  259. *
  260. * Calls the VIDIOC_S_EXT_CTRLS IOCTL internally with Control ID
  261. * #V4L2_CID_MPEG_VIDEOENC_SLICE_LENGTH_PARAM. Must be called after setFormat on
  262. * both the planes and before \c requestBuffers on any of the planes.
  263. *
  264. * @param[in] type Slice length type, one of enum v4l2_enc_slice_length_type.
  265. * @param[in] length Length of the slice, in bytes if the type is
  266. * #V4L2_ENC_SLICE_LENGTH_TYPE_BITS, else in number of MBs if the type is
  267. * #V4L2_ENC_SLICE_LENGTH_TYPE_MBLK.
  268. * @return 0 for success, -1 otherwise.
  269. */
  270. int setSliceLength(v4l2_enc_slice_length_type type, uint32_t length);
  271. /**
  272. * Sets the encoder HW Preset Type.
  273. *
  274. * Calls the VIDIOC_S_EXT_CTRLS ioctl internally with Control ID
  275. * #V4L2_CID_MPEG_VIDEOENC_HW_PRESET_TYPE_PARAM. Must be called after setFormat() on
  276. * both the planes and before \c requestBuffers on any of the planes.
  277. *
  278. * @param[in] type HW Preset Type, one of
  279. * enum v4l2_enc_hw_preset_type_param.
  280. * @return 0 for success, -1 otherwise.
  281. */
  282. int setHWPresetType(v4l2_enc_hw_preset_type type);
  283. /**
  284. * Sets the Region of Interest (ROI) parameters for the next buffer, which will
  285. * be queued on the output plane with index \a buffer_index.
  286. *
  287. * Calls the VIDIOC_S_EXT_CTRLS IOCTL internally with Control ID
  288. * #V4L2_CID_MPEG_VIDEOENC_ROI_PARAMS. Must be called after
  289. * requesting buffer on both the planes.
  290. *
  291. * @param[in] buffer_index Index of the output plane buffer to apply the ROI
  292. * params.
  293. * @param[in] params A reference to the parameters to be applied on the frame, structure of
  294. * type v4l2_enc_frame_ROI_params.
  295. * @return 0 for success, -1 otherwise.
  296. */
  297. int setROIParams(uint32_t buffer_index, v4l2_enc_frame_ROI_params & params);
  298. /**
  299. * Sets the preprocessing enhancement (PPE) initialization parameters at the start of the session
  300. *
  301. * Calls the VIDIOC_S_EXT_CTRLS IOCTL internally with Control ID
  302. * #V4L2_CID_MPEG_VIDEOENC_PPE_INIT_PARAMS. Must be called after
  303. * requesting buffer on both the planes.
  304. *
  305. * @param[in] params A reference to the parameters to be applied on the frame, structure of
  306. * type v4l2_enc_ppe_init_params.
  307. * @return 0 for success, -1 otherwise.
  308. */
  309. int setPPEInitParams(v4l2_enc_ppe_init_params & params);
  310. /**
  311. * Enables External ROI.
  312. *
  313. * Calls the VIDIOC_S_EXT_CTRLS IOCTL internally with Control ID
  314. * #V4L2_CID_MPEG_VIDEOENC_ENABLE_ROI_PARAM. Must be called after
  315. * requesting buffer on both the planes.
  316. *
  317. * @param[in] params Parameters to be applied on the frame, structure of
  318. * type #v4l2_enc_enable_roi_param.
  319. * @return 0 for success, -1 otherwise.
  320. */
  321. int enableROI(v4l2_enc_enable_roi_param &params);
  322. /**
  323. * Enables Recon CRC.
  324. *
  325. * Calls the VIDIOC_S_EXT_CTRLS IOCTL internally with Control ID
  326. * #V4L2_CID_MPEG_VIDEOENC_ENABLE_RECONCRC_PARAM. Must be called after
  327. * requesting buffer on both the planes.
  328. *
  329. * @param[in] params Parameters to be applied on the frame, structure of
  330. * type #v4l2_enc_enable_reconcrc_param.
  331. * @return 0 for success, -1 otherwise.
  332. */
  333. int enableReconCRC(v4l2_enc_enable_reconcrc_param &params);
  334. /**
  335. * Enable External RPS
  336. *
  337. * Calls the VIDIOC_S_EXT_CTRLS IOCTL internally with Control ID
  338. * #V4L2_CID_MPEG_VIDEOENC_ENABLE_EXTERNAL_RPS_CONTROL. Must be called after
  339. * requesting buffer on both the planes.
  340. *
  341. * @param[in] params Parameters to be applied on the frame, structure of
  342. * type #v4l2_enc_enable_ext_rps_ctr
  343. * @return 0 for success, -1 otherwise.
  344. */
  345. int enableExternalRPS(v4l2_enc_enable_ext_rps_ctr &params);
  346. /**
  347. * Enable External Picture RC
  348. *
  349. * Calls the VIDIOC_S_EXT_CTRLS IOCTL internally with Control ID
  350. * #V4L2_CID_MPEG_VIDEOENC_ENABLE_EXTERNAL_RATE_CONTROL. Must be called after
  351. * requesting buffer on both the planes.
  352. *
  353. * @param[in] params Parameters to be applied on the frame, structure of
  354. * type ##v4l2_enc_enable_ext_rate_ctr
  355. * @return 0 for success, -1 otherwise.
  356. */
  357. int enableExternalRC(v4l2_enc_enable_ext_rate_ctr &params);
  358. /**
  359. * Enable AV1 Multi-tile configuration
  360. *
  361. * Calls the VIDIOC_S_EXT_CTRLS IOCTL internally with Control ID
  362. * #V4L2_CID_MPEG_VIDEOENC_AV1_TILE_CONFIGURATION. Must be called after
  363. * setFormat on both the planes and before \c requestBuffers on any of the
  364. * planes.
  365. *
  366. * @param[in] params Parameters to be applied, structure of
  367. * type ##v4l2_enc_av1_tile_config
  368. * @return 0 for success, -1 otherwise.
  369. */
  370. int enableAV1Tile(v4l2_enc_av1_tile_config &params);
  371. /**
  372. * Enable AV1 variance based SSIM RDO.
  373. *
  374. * Calls the VIDIOC_S_EXT_CTRLS IOCTL internally with Control ID
  375. * #V4L2_CID_MPEG_VIDEOENC_AV1_ENABLE_SSIMRDO. Must be called after
  376. * setFormat on both the planes.
  377. *
  378. * @param[in] enabled Boolean value indicating whether to enable/disable
  379. * the control.
  380. * @return 0 for success, -1 otherwise.
  381. */
  382. int setAV1SsimRdo(bool enabled);
  383. /**
  384. * Disable AV1 CDF update in the symbol decoding process.
  385. *
  386. * Calls the VIDIOC_S_EXT_CTRLS IOCTL internally with Control ID
  387. * #V4L2_CID_MPEG_VIDEOENC_AV1_DISABLE_CDF_UPDATE. The value true
  388. * disables the CDF update. Must be called after setFormat on both
  389. * the planes.
  390. *
  391. * @param[in] disabled Boolean value indicating whether to enable/disable
  392. * the control.
  393. * @return 0 for success, -1 otherwise.
  394. */
  395. int setAV1DisableCDFUpdate(bool disabled);
  396. /**
  397. * Set input Metadata parameters for the next buffer which will
  398. * be queued on output plane with index \a buffer_index
  399. *
  400. * Calls the VIDIOC_S_EXT_CTRLS IOCTL internally with Control ID
  401. * #V4L2_CID_MPEG_VIDEOENC_INPUT_METADATA. Must be called after
  402. * requesting buffer on both the planes.
  403. *
  404. * @param[in] buffer_index Index of output plane buffer on which the external
  405. * RC params should be applied.
  406. * @param[in] params Parameters to be applied on the frame, structure of
  407. * type #v4l2_ctrl_videoenc_input_metadata
  408. * @return 0 for success, -1 otherwise.
  409. */
  410. int SetInputMetaParams(uint32_t buffer_index, v4l2_ctrl_videoenc_input_metadata &params);
  411. /**
  412. * Sets the virtual buffer size of the encoder.
  413. *
  414. * Calls the VIDIOC_S_EXT_CTRLS IOCTL internally with Control ID
  415. * #V4L2_CID_MPEG_VIDEOENC_VIRTUALBUFFER_SIZE. Must be called after
  416. * setFormat on both the planes.
  417. *
  418. * @param[in] size Virtual buffer size, in bytes.
  419. * @return 0 for success, -1 otherwise.
  420. */
  421. int setVirtualBufferSize(uint32_t size);
  422. /**
  423. * Sets the number of reference frames of the encoder.
  424. *
  425. * Calls the VIDIOC_S_EXT_CTRLS IOCTL internally with Control ID
  426. * #V4L2_CID_MPEG_VIDEOENC_NUM_REFERENCE_FRAMES. Must be called after
  427. * setFormat on both the planes.
  428. *
  429. * @param[in] num_frames Number of reference frames.
  430. * @return 0 for success, -1 otherwise.
  431. */
  432. int setNumReferenceFrames(uint32_t num_frames);
  433. /**
  434. * Sets slice intra-refresh interval params.
  435. *
  436. * Calls the VIDIOC_S_EXT_CTRLS IOCTL internally with Control ID
  437. * #V4L2_CID_MPEG_VIDEOENC_SLICE_INTRAREFRESH_PARAM. Must be called after
  438. * setFormat on both the planes.
  439. *
  440. * @param[in] interval Slice intra-refresh interval, in number of slices.
  441. * @return 0 for success, -1 otherwise.
  442. */
  443. int setSliceIntrarefresh(uint32_t interval);
  444. /**
  445. * Sets the number of B frames to P frames.
  446. *
  447. * Calls the VIDIOC_S_EXT_CTRLS IOCTL internally with Control ID
  448. * #V4L2_CID_MPEG_VIDEOENC_NUM_BFRAMES. Must be called after
  449. * setFormat on both the planes.
  450. *
  451. * @param[in] num Number of B frames.
  452. * @return 0 for success, -1 otherwise.
  453. */
  454. int setNumBFrames(uint32_t num);
  455. /**
  456. * Enables/disables insert SPS PPS at every IDR.
  457. *
  458. * Calls the VIDIOC_S_EXT_CTRLS IOCTL internally with Control ID
  459. * #V4L2_CID_MPEG_VIDEOENC_INSERT_SPS_PPS_AT_IDR. Must be called after
  460. * setFormat on both the planes.
  461. *
  462. * @param[in] enabled Boolean value indicating whether to enable/disable
  463. * the control.
  464. * @return 0 for success, -1 otherwise.
  465. */
  466. int setInsertSpsPpsAtIdrEnabled(bool enabled);
  467. /**
  468. * Enables/disables CABAC entropy encoding for H264.
  469. *
  470. * Calls the VIDIOC_S_EXT_CTRLS IOCTL internally with Control ID
  471. * #V4L2_CID_MPEG_VIDEO_H264_ENTROPY_MODE. Must be called after
  472. * setFormat on both the planes.
  473. *
  474. * @param[in] enabled Boolean value indicating whether to enable/disable
  475. * the control.
  476. * @return 0 for success, -1 otherwise.
  477. */
  478. int setCABAC(bool enabled);
  479. /**
  480. * Enables/disables Slice level encoding for H264 / HEVC.
  481. *
  482. * Calls the VIDIOC_S_EXT_CTRLS IOCTL internally with Control ID
  483. * #V4L2_CID_MPEG_VIDEOENC_ENABLE_SLICE_LEVEL_ENCODE. Must be called after
  484. * setFormat on both the planes.
  485. *
  486. * @param[in] enabled Boolean value indicating whether to enable/disable
  487. * the control.
  488. * @return 0 for success, -1 otherwise.
  489. */
  490. int setSliceLevelEncode(bool enabled);
  491. /**
  492. * Enables video encoder output motion vector metadata reporting.
  493. *
  494. * Calls the VIDIOC_S_EXT_CTRLS IOCTL internally with Control ID
  495. * #V4L2_CID_MPEG_VIDEOENC_METADATA_MV. Must be called after setFormat on
  496. * both the planes and before \c requestBuffers on any of the planes.
  497. *
  498. * @return 0 for success, -1 otherwise.
  499. */
  500. int enableMotionVectorReporting();
  501. /**
  502. * Gets metadata for the encoded capture plane buffer.
  503. *
  504. * Calls the VIDIOC_S_EXT_CTRLS IOCTL internally with Control ID
  505. * #V4L2_CID_MPEG_VIDEOENC_METADATA. Must be called for a buffer that has
  506. * been dequeued from the capture plane. The returned metadata corresponds
  507. * to the last dequeued buffer with index @a buffer_index.
  508. *
  509. * @param[in] buffer_index Index of the capture plane buffer whose metadata
  510. * is required.
  511. * @param[in,out] enc_metadata Reference to the metadata structure
  512. * v4l2_ctrl_videoenc_outputbuf_metadata to be filled.
  513. *
  514. * @return 0 for success, -1 otherwise.
  515. */
  516. int getMetadata(uint32_t buffer_index,
  517. v4l2_ctrl_videoenc_outputbuf_metadata &enc_metadata);
  518. /**
  519. * Gets motion vector metadata for the encoded capture plane buffer.
  520. *
  521. * Calls the VIDIOC_S_EXT_CTRLS IOCTL internally with Control ID
  522. * #V4L2_CID_MPEG_VIDEOENC_METADATA_MV. Must be called for a buffer that has
  523. * been dequeued from the capture plane. The returned metadata corresponds
  524. * to the last dequeued buffer with index @a buffer_index.
  525. *
  526. * @param[in] buffer_index Index of the capture plane buffer whose metadata
  527. * is required.
  528. * @param[in,out] enc_mv_metadata Reference to the metadata structure
  529. * v4l2_ctrl_videoenc_outputbuf_metadata_MV to be filled.
  530. *
  531. * @return 0 for success, -1 otherwise.
  532. */
  533. int getMotionVectors(uint32_t buffer_index,
  534. v4l2_ctrl_videoenc_outputbuf_metadata_MV &enc_mv_metadata);
  535. /**
  536. * Sets QP values for I/P/B frames.
  537. *
  538. * Calls the VIDIOC_S_EXT_CTRLS IOCTL internally with control Id
  539. * %V4L2_CID_MPEG_VIDEOENC_QP_RANGE. Must be called after
  540. * setFormat on both the planes.
  541. *
  542. * @param[in] MinQpI Minimum Qp Value for I frame.
  543. * @param[in] MaxQpI Minimum Qp Value for I frame.
  544. * @param[in] MinQpP Minimum Qp Value for P frame.
  545. * @param[in] MaxQpP Minimum Qp Value for P frame.
  546. * @param[in] MinQpB Minimum Qp Value for B frame.
  547. * @param[in] MaxQpB Minimum Qp Value for B frame.
  548. * @returns 0 for success, -1 otherwise.
  549. */
  550. int setQpRange(uint32_t MinQpI, uint32_t MaxQpI, uint32_t MinQpP,
  551. uint32_t MaxQpP, uint32_t MinQpB, uint32_t MaxQpB);
  552. /**
  553. * Sets Sample Aspect Ratio width for VUI encoding.
  554. *
  555. * Calls the VIDIOC_S_EXT_CTRLS IOCTL internally with control Id
  556. * \c V4L2_CID_MPEG_VIDEO_H264_VUI_EXT_SAR_WIDTH or
  557. * #V4L2_CID_MPEG_VIDEOENC_H265_VUI_EXT_SAR_WIDTH, depending on the
  558. * encoder type. Must be called after setFormat on both the planes.
  559. *
  560. * @param[in] sar_width SAR for width.
  561. * @returns 0 for success, -1 otherwise.
  562. */
  563. int setSampleAspectRatioWidth(uint32_t sar_width);
  564. /**
  565. * Sets Sample Aspect Ratio height for VUI encoding.
  566. *
  567. * Calls the VIDIOC_S_EXT_CTRLS IOCTL internally with control Id
  568. * \c V4L2_CID_MPEG_VIDEO_H264_VUI_EXT_SAR_HEIGHT or
  569. * #V4L2_CID_MPEG_VIDEOENC_H265_VUI_EXT_SAR_HEIGHT, depending on the
  570. * encoder type. Must be called after setFormat on both the planes.
  571. *
  572. * @param[in] sar_height SAR for height.
  573. * @returns 0 for success, -1 otherwise.
  574. */
  575. int setSampleAspectRatioHeight(uint32_t sar_height);
  576. /**
  577. * Enables/disables insert VUI.
  578. *
  579. * Calls the VIDIOC_S_EXT_CTRLS IOCTL internally with Control ID
  580. * @c V4L2_CID_MPEG_VIDEOENC_INSERT_VUI. Must be called after
  581. * setFormat on both the planes.
  582. *
  583. * @param[in] enabled Boolean value indicating whether to enable/disable
  584. * the control.
  585. * @return 0 for success, -1 otherwise.
  586. */
  587. int setInsertVuiEnabled(bool enabled);
  588. /**
  589. * Enables/disables extended color format.
  590. *
  591. * Calls the VIDIOC_S_EXT_CTRLS IOCTL internally with Control ID
  592. * @c V4L2_CID_MPEG_VIDEOENC_EXTEDED_COLORFORMAT. Must be called after
  593. * setFormat on both the planes.
  594. *
  595. * @param[in] enabled Boolean value indicating whether to enable/disable
  596. * the control.
  597. * @return 0 for success, -1 otherwise.
  598. */
  599. int setExtendedColorFormat(bool enabled);
  600. /**
  601. * Enables/disables insert AUD.
  602. *
  603. * Calls the VIDIOC_S_EXT_CTRLS IOCTL internally with Control ID
  604. * @c V4L2_CID_MPEG_VIDEO_H264_AUD_SAR_ENABLE. Must be called after
  605. * setFormat on both the planes.
  606. *
  607. * @param[in] enabled Boolean value indicating whether to enable/disable
  608. * the control.
  609. * @return 0 for success, -1 otherwise.
  610. */
  611. int setInsertAudEnabled(bool enabled);
  612. /**
  613. * Enables/disables all i-frame encode.
  614. *
  615. * Calls the VIDIOC_S_EXT_CTRLS IOCTL internally with Control ID
  616. * @c V4L2_CID_MPEG_VIDEOENC_ENABLE_ALLIFRAME_ENCODE. Must be called after
  617. * setFormat on both the planes.
  618. *
  619. * @param[in] enabled Boolean value indicating whether to enable/disable
  620. * the control.
  621. * @return 0 for success, -1 otherwise.
  622. */
  623. int setAlliFramesEncode(bool enabled);
  624. /**
  625. * Sets the encoder Picture Order Control type.
  626. *
  627. * Calls the VIDIOC_S_EXT_CTRLS IOCTL internally with Control ID
  628. * #V4L2_CID_MPEG_VIDEOENC_POC_TYPE. Must be called after setFormat on both
  629. * the planes and before \c requestBuffers on any of the planes.
  630. *
  631. * @param[in] pocType Set the Picture Order Count for the encoder.
  632. *
  633. * @return 0 for success, -1 otherwise.
  634. */
  635. int setPocType(uint32_t pocType);
  636. /**
  637. * Sets the initial QP for I/P/B frames.
  638. *
  639. * Calls the VIDIOC_S_EXT_CTRLS IOCTL internally with control Id
  640. * %V4L2_CID_MPEG_VIDEOENC_INIT_FRAME_QP. Must be called after
  641. * setFormat on both the planes.
  642. *
  643. *
  644. * @param[in] IinitQP Qp Value for I frame.
  645. * @param[in] PinitQP Qp Value for P frame.
  646. * @param[in] BinitQP Qp Value for B frame.
  647. *
  648. * @return 0 for success, -1 otherwise.
  649. */
  650. int setInitQP(uint32_t IinitQP, uint32_t PinitQP, uint32_t BinitQP);
  651. /**
  652. * Sets the number of frames to encode.
  653. *
  654. * Calls the VIDIOC_S_EXT_CTRLS IOCTL internally with Control ID
  655. * #V4L2_CID_MPEG_VIDEOENC_FRAMES_TO_ENCODE. Must be called after
  656. * setFormat on both the planes.
  657. *
  658. * @param[in] framesToEncode Set the number of frames to encode.
  659. *
  660. * @return 0 for success, -1 otherwise.
  661. */
  662. int setFramesToEncode(uint32_t framesToEncode);
  663. /**
  664. * Sets the H.265 encoder Chroma Format IDC.
  665. *
  666. * Calls the VIDIOC_S_EXT_CTRLS IOCTL internally with Control ID
  667. * #V4L2_CID_MPEG_VIDEOENC_H265_CHROMA_FACTOR_IDC. Must be called after setFormat on both
  668. * the planes and before \c requestBuffers on any of the planes.
  669. *
  670. * @param[in] crfactor Set @c chroma_factor_idc for the encoder.
  671. *
  672. * @return 0 for success, -1 otherwise.
  673. */
  674. int setChromaFactorIDC(uint8_t crfactor);
  675. /**
  676. * Sets the lossless encoding for H.264/H.265.
  677. *
  678. * Calls the VIDIOC_S_EXT_CTRLS IOCTL internally with Control ID
  679. * #V4L2_CID_MPEG_VIDEOENC_ENABLE_LOSSLESS. Must be called after setFormat on both
  680. * the planes and before \c requestBuffers on any of the planes.
  681. *
  682. * @param[in] enabled Indicates whether to enable or disable
  683. * the control.
  684. * @return 0 for success, -1 otherwise.
  685. */
  686. int setLossless(bool enabled);
  687. /**
  688. * Issues Poll on the device which blocks until :
  689. * a) Either there is something to dequeue from capture or output plane or any events.
  690. * b) Poll was interrupted by a call to the device using V4L2_CID_SET_POLL_INTERRUPT
  691. * c) Application has already interrupted polling by V4L2_CID_SET_POLL_INTERRUPT
  692. */
  693. int DevicePoll(v4l2_ctrl_video_device_poll *devicepoll);
  694. /**
  695. * Sets the polling interrupt, now if the application calls Poll, the device should
  696. * not block, in other words polling is disabled.
  697. */
  698. int SetPollInterrupt();
  699. /**
  700. * Clears the polling interrupt, now if the application calls Poll, the device should
  701. * block until the event is triggered, in other words polling is enabled.
  702. */
  703. int ClearPollInterrupt();
  704. private:
  705. /**
  706. * Constructor used by #createVideoEncoder.
  707. */
  708. NvVideoEncoder(const char *name, int flags);
  709. static const NvElementProfiler::ProfilerField valid_fields =
  710. NvElementProfiler::PROFILER_FIELD_TOTAL_UNITS |
  711. NvElementProfiler::PROFILER_FIELD_LATENCIES |
  712. NvElementProfiler::PROFILER_FIELD_FPS;
  713. };
  714. /** @} */
  715. #endif