NvVideoEncoder.cpp 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354
  1. /*
  2. * Copyright (c) 2016-2022, 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 "NvVideoEncoder.h"
  29. #include "NvLogging.h"
  30. #include <cstring>
  31. #include <errno.h>
  32. #include <libv4l2.h>
  33. #define ENCODER_DEV "/dev/nvhost-msenc"
  34. #define ENCODER_COMP_NAME "NVENC"
  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 || capture_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_BUFFERS_NOT_REQUESTED() \
  54. if (output_plane.getNumBuffers() == 0 || capture_plane.getNumBuffers() == 0) { \
  55. COMP_ERROR_MSG("Should be called before requesting buffers on either plane") \
  56. return -1; \
  57. }
  58. #define RETURN_ERROR_IF_FORMATS_NOT_SET() \
  59. if (output_plane_pixfmt == 0 || capture_plane_pixfmt == 0) { \
  60. COMP_ERROR_MSG("Should be called after setting plane formats") \
  61. return -1; \
  62. }
  63. using namespace std;
  64. NvVideoEncoder::NvVideoEncoder(const char *name, int flags)
  65. :NvV4l2Element(name, ENCODER_DEV, flags, valid_fields)
  66. {
  67. }
  68. NvVideoEncoder *
  69. NvVideoEncoder::createVideoEncoder(const char *name, int flags)
  70. {
  71. NvVideoEncoder *enc = new NvVideoEncoder(name, flags);
  72. if (enc->isInError())
  73. {
  74. delete enc;
  75. return NULL;
  76. }
  77. return enc;
  78. }
  79. NvVideoEncoder::~NvVideoEncoder()
  80. {
  81. }
  82. int
  83. NvVideoEncoder::setOutputPlaneFormat(uint32_t pixfmt, uint32_t width,
  84. uint32_t height)
  85. {
  86. struct v4l2_format format;
  87. uint32_t num_bufferplanes;
  88. NvBuffer::NvBufferPlaneFormat planefmts[MAX_PLANES];
  89. if (pixfmt != V4L2_PIX_FMT_YUV420M &&
  90. pixfmt != V4L2_PIX_FMT_P010M && pixfmt != V4L2_PIX_FMT_NV12M &&
  91. pixfmt != V4L2_PIX_FMT_NV24M && pixfmt != V4L2_PIX_FMT_NV24_10LE &&
  92. pixfmt != V4L2_PIX_FMT_YUV444M)
  93. {
  94. COMP_ERROR_MSG("Only YUV420M, NV24, NV24_10LE, YUV444, P010M and NV12M are supported");
  95. return -1;
  96. }
  97. output_plane_pixfmt = pixfmt;
  98. NvBuffer::fill_buffer_plane_format(&num_bufferplanes, planefmts, width,
  99. height, pixfmt);
  100. output_plane.setBufferPlaneFormat(num_bufferplanes, planefmts);
  101. memset(&format, 0, sizeof(struct v4l2_format));
  102. format.type = output_plane.getBufType();
  103. format.fmt.pix_mp.width = width;
  104. format.fmt.pix_mp.height = height;
  105. format.fmt.pix_mp.pixelformat = pixfmt;
  106. format.fmt.pix_mp.num_planes = num_bufferplanes;
  107. return output_plane.setFormat(format);
  108. }
  109. int
  110. NvVideoEncoder::setCapturePlaneFormat(uint32_t pixfmt, uint32_t width,
  111. uint32_t height, uint32_t sizeimage)
  112. {
  113. struct v4l2_format format;
  114. memset(&format, 0, sizeof(struct v4l2_format));
  115. format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
  116. switch (pixfmt)
  117. {
  118. case V4L2_PIX_FMT_H264:
  119. case V4L2_PIX_FMT_H265:
  120. case V4L2_PIX_FMT_VP8:
  121. case V4L2_PIX_FMT_VP9:
  122. case V4L2_PIX_FMT_AV1:
  123. capture_plane_pixfmt = pixfmt;
  124. break;
  125. default:
  126. ERROR_MSG("Unknown supported pixel format for encoder " << pixfmt);
  127. return -1;
  128. }
  129. format.fmt.pix_mp.pixelformat = pixfmt;
  130. format.fmt.pix_mp.width = width;
  131. format.fmt.pix_mp.height = height;
  132. format.fmt.pix_mp.num_planes = 1;
  133. format.fmt.pix_mp.plane_fmt[0].sizeimage = sizeimage;
  134. return capture_plane.setFormat(format);
  135. }
  136. int
  137. NvVideoEncoder::setFrameRate(uint32_t framerate_num, uint32_t framerate_den)
  138. {
  139. struct v4l2_streamparm parms;
  140. RETURN_ERROR_IF_FORMATS_NOT_SET();
  141. memset(&parms, 0, sizeof(parms));
  142. parms.parm.output.timeperframe.numerator = framerate_den;
  143. parms.parm.output.timeperframe.denominator = framerate_num;
  144. CHECK_V4L2_RETURN(output_plane.setStreamParms(parms),
  145. "Setting framerate to " << framerate_num << "/" << framerate_den);
  146. }
  147. int
  148. NvVideoEncoder::setBitrate(uint32_t bitrate)
  149. {
  150. struct v4l2_ext_control control;
  151. struct v4l2_ext_controls ctrls;
  152. cout << capture_plane_pixfmt <<endl;
  153. cout << output_plane_pixfmt <<endl;
  154. RETURN_ERROR_IF_FORMATS_NOT_SET();
  155. memset(&control, 0, sizeof(control));
  156. memset(&ctrls, 0, sizeof(ctrls));
  157. ctrls.count = 1;
  158. ctrls.controls = &control;
  159. ctrls.ctrl_class = V4L2_CTRL_CLASS_MPEG;
  160. control.id = V4L2_CID_MPEG_VIDEO_BITRATE;
  161. control.value = bitrate;
  162. CHECK_V4L2_RETURN(setExtControls(ctrls),
  163. "Setting encoder bitrate to " << bitrate);
  164. }
  165. int
  166. NvVideoEncoder::setEncoderCommand(int cmd, int flags)
  167. {
  168. int ret=0;
  169. struct v4l2_encoder_cmd v4l2_enc_cmd;
  170. v4l2_enc_cmd.cmd = cmd;
  171. v4l2_enc_cmd.flags = flags;
  172. ret = v4l2_ioctl(fd, VIDIOC_ENCODER_CMD, v4l2_enc_cmd);
  173. if (ret < 0)
  174. printf(" Error in encoder command \n");
  175. return ret;
  176. }
  177. int
  178. NvVideoEncoder::setPeakBitrate(uint32_t peak_bitrate)
  179. {
  180. struct v4l2_ext_control control;
  181. struct v4l2_ext_controls ctrls;
  182. cout << capture_plane_pixfmt <<endl;
  183. cout << output_plane_pixfmt <<endl;
  184. RETURN_ERROR_IF_FORMATS_NOT_SET();
  185. memset(&control, 0, sizeof(control));
  186. memset(&ctrls, 0, sizeof(ctrls));
  187. ctrls.count = 1;
  188. ctrls.controls = &control;
  189. ctrls.ctrl_class = V4L2_CTRL_CLASS_MPEG;
  190. control.id = V4L2_CID_MPEG_VIDEO_BITRATE_PEAK;
  191. control.value = peak_bitrate;
  192. CHECK_V4L2_RETURN(setExtControls(ctrls),
  193. "Setting encoder peak bitrate to " << peak_bitrate);
  194. }
  195. int
  196. NvVideoEncoder::setProfile(uint32_t profile)
  197. {
  198. struct v4l2_ext_control control;
  199. struct v4l2_ext_controls ctrls;
  200. RETURN_ERROR_IF_FORMATS_NOT_SET();
  201. RETURN_ERROR_IF_BUFFERS_REQUESTED();
  202. memset(&control, 0, sizeof(control));
  203. memset(&ctrls, 0, sizeof(ctrls));
  204. ctrls.count = 1;
  205. ctrls.controls = &control;
  206. ctrls.ctrl_class = V4L2_CTRL_CLASS_MPEG;
  207. switch (capture_plane_pixfmt)
  208. {
  209. case V4L2_PIX_FMT_H264:
  210. control.id = V4L2_CID_MPEG_VIDEO_H264_PROFILE;
  211. break;
  212. case V4L2_PIX_FMT_H265:
  213. control.id = V4L2_CID_MPEG_VIDEO_H265_PROFILE;
  214. break;
  215. default:
  216. COMP_ERROR_MSG("Unsupported encoder type");
  217. return -1;
  218. }
  219. control.value = profile;
  220. CHECK_V4L2_RETURN(setExtControls(ctrls),
  221. "Setting encoder profile to " << profile);
  222. }
  223. int
  224. NvVideoEncoder::setLevel(uint32_t level)
  225. {
  226. struct v4l2_ext_control control;
  227. struct v4l2_ext_controls ctrls;
  228. RETURN_ERROR_IF_FORMATS_NOT_SET();
  229. RETURN_ERROR_IF_BUFFERS_REQUESTED();
  230. memset(&control, 0, sizeof(control));
  231. memset(&ctrls, 0, sizeof(ctrls));
  232. ctrls.count = 1;
  233. ctrls.controls = &control;
  234. ctrls.ctrl_class = V4L2_CTRL_CLASS_MPEG;
  235. switch (capture_plane_pixfmt)
  236. {
  237. case V4L2_PIX_FMT_H264:
  238. control.id = V4L2_CID_MPEG_VIDEO_H264_LEVEL;
  239. control.value = (enum v4l2_mpeg_video_h264_level)level;
  240. break;
  241. case V4L2_PIX_FMT_H265:
  242. control.id = V4L2_CID_MPEG_VIDEOENC_H265_LEVEL;
  243. control.value = (enum v4l2_mpeg_video_h265_level)level;
  244. break;
  245. default:
  246. COMP_WARN_MSG("Currently only supported for H.264 and H.265");
  247. return 0;
  248. }
  249. CHECK_V4L2_RETURN(setExtControls(ctrls),
  250. "Setting encoder level to " << level);
  251. }
  252. int
  253. NvVideoEncoder::setConstantQp(bool enabled_rc)
  254. {
  255. struct v4l2_ext_control control;
  256. struct v4l2_ext_controls ctrls;
  257. RETURN_ERROR_IF_FORMATS_NOT_SET();
  258. RETURN_ERROR_IF_BUFFERS_REQUESTED();
  259. memset(&control, 0, sizeof(control));
  260. memset(&ctrls, 0, sizeof(ctrls));
  261. ctrls.count = 1;
  262. ctrls.controls = &control;
  263. ctrls.ctrl_class = V4L2_CTRL_CLASS_MPEG;
  264. control.id = V4L2_CID_MPEG_VIDEO_FRAME_RC_ENABLE;
  265. control.value = enabled_rc; // if false: disable rate control
  266. CHECK_V4L2_RETURN(setExtControls(ctrls),
  267. "Setting encoder constant qp to " << enabled_rc);
  268. }
  269. int
  270. NvVideoEncoder::setRateControlMode(enum v4l2_mpeg_video_bitrate_mode mode)
  271. {
  272. struct v4l2_ext_control control;
  273. struct v4l2_ext_controls ctrls;
  274. RETURN_ERROR_IF_FORMATS_NOT_SET();
  275. RETURN_ERROR_IF_BUFFERS_REQUESTED();
  276. memset(&control, 0, sizeof(control));
  277. memset(&ctrls, 0, sizeof(ctrls));
  278. ctrls.count = 1;
  279. ctrls.controls = &control;
  280. ctrls.ctrl_class = V4L2_CTRL_CLASS_MPEG;
  281. control.id = V4L2_CID_MPEG_VIDEO_BITRATE_MODE;
  282. control.value = mode;
  283. CHECK_V4L2_RETURN(setExtControls(ctrls),
  284. "Setting encoder rate control mode to " << mode);
  285. }
  286. int
  287. NvVideoEncoder::setMaxPerfMode(int flag)
  288. {
  289. struct v4l2_ext_control control;
  290. struct v4l2_ext_controls ctrls;
  291. RETURN_ERROR_IF_FORMATS_NOT_SET();
  292. RETURN_ERROR_IF_BUFFERS_REQUESTED();
  293. memset(&control, 0, sizeof(control));
  294. memset(&ctrls, 0, sizeof(ctrls));
  295. ctrls.count = 1;
  296. ctrls.controls = &control;
  297. ctrls.ctrl_class = V4L2_CTRL_CLASS_MPEG;
  298. control.id = V4L2_CID_MPEG_VIDEO_MAX_PERFORMANCE;
  299. control.value = flag;
  300. CHECK_V4L2_RETURN(setExtControls(ctrls),
  301. "Enabling Maximum Performance ");
  302. }
  303. int
  304. NvVideoEncoder::setIFrameInterval(uint32_t interval)
  305. {
  306. struct v4l2_ext_control control;
  307. struct v4l2_ext_controls ctrls;
  308. RETURN_ERROR_IF_FORMATS_NOT_SET();
  309. RETURN_ERROR_IF_BUFFERS_REQUESTED();
  310. memset(&control, 0, sizeof(control));
  311. memset(&ctrls, 0, sizeof(ctrls));
  312. ctrls.count = 1;
  313. ctrls.controls = &control;
  314. ctrls.ctrl_class = V4L2_CTRL_CLASS_MPEG;
  315. control.id = V4L2_CID_MPEG_VIDEO_GOP_SIZE;
  316. control.value = interval;
  317. CHECK_V4L2_RETURN(setExtControls(ctrls),
  318. "Setting encoder I-frame interval to " << interval);
  319. }
  320. int
  321. NvVideoEncoder::setIDRInterval(uint32_t interval)
  322. {
  323. struct v4l2_ext_control control;
  324. struct v4l2_ext_controls ctrls;
  325. RETURN_ERROR_IF_FORMATS_NOT_SET();
  326. RETURN_ERROR_IF_BUFFERS_REQUESTED();
  327. memset(&control, 0, sizeof(control));
  328. memset(&ctrls, 0, sizeof(ctrls));
  329. ctrls.count = 1;
  330. ctrls.controls = &control;
  331. ctrls.ctrl_class = V4L2_CTRL_CLASS_MPEG;
  332. control.id = V4L2_CID_MPEG_VIDEO_IDR_INTERVAL;
  333. control.value = interval;
  334. CHECK_V4L2_RETURN(setExtControls(ctrls),
  335. "Setting encoder IDR interval to " << interval);
  336. }
  337. int
  338. NvVideoEncoder::forceIDR()
  339. {
  340. struct v4l2_ext_control control;
  341. struct v4l2_ext_controls ctrls;
  342. RETURN_ERROR_IF_FORMATS_NOT_SET();
  343. memset(&control, 0, sizeof(control));
  344. memset(&ctrls, 0, sizeof(ctrls));
  345. ctrls.count = 1;
  346. ctrls.controls = &control;
  347. ctrls.ctrl_class = V4L2_CTRL_CLASS_MPEG;
  348. control.id = V4L2_CID_MPEG_MFC51_VIDEO_FORCE_FRAME_TYPE;
  349. CHECK_V4L2_RETURN(setExtControls(ctrls),
  350. "Forcing IDR");
  351. }
  352. int
  353. NvVideoEncoder::setTemporalTradeoff(v4l2_enc_temporal_tradeoff_level_type level)
  354. {
  355. struct v4l2_ext_control control;
  356. struct v4l2_ext_controls ctrls;
  357. RETURN_ERROR_IF_FORMATS_NOT_SET();
  358. RETURN_ERROR_IF_BUFFERS_REQUESTED();
  359. memset(&control, 0, sizeof(control));
  360. memset(&ctrls, 0, sizeof(ctrls));
  361. ctrls.count = 1;
  362. ctrls.controls = &control;
  363. ctrls.ctrl_class = V4L2_CTRL_CLASS_MPEG;
  364. control.id = V4L2_CID_MPEG_VIDEOENC_TEMPORAL_TRADEOFF_LEVEL;
  365. control.value = level;
  366. CHECK_V4L2_RETURN(setExtControls(ctrls),
  367. "Setting encoder temporal tradeoff level to " << level);
  368. }
  369. int
  370. NvVideoEncoder::setSliceLength(v4l2_enc_slice_length_type type, uint32_t length)
  371. {
  372. struct v4l2_ext_control control;
  373. struct v4l2_ext_controls ctrls;
  374. v4l2_enc_slice_length_param param = {type, length};
  375. RETURN_ERROR_IF_FORMATS_NOT_SET();
  376. RETURN_ERROR_IF_BUFFERS_REQUESTED();
  377. memset(&control, 0, sizeof(control));
  378. memset(&ctrls, 0, sizeof(ctrls));
  379. ctrls.count = 1;
  380. ctrls.controls = &control;
  381. ctrls.ctrl_class = V4L2_CTRL_CLASS_MPEG;
  382. control.id = V4L2_CID_MPEG_VIDEOENC_SLICE_LENGTH_PARAM;
  383. control.string = (char *) &param;
  384. CHECK_V4L2_RETURN(setExtControls(ctrls),
  385. "Setting encoder packet type to " << type << ", length to " <<
  386. length);
  387. }
  388. int
  389. NvVideoEncoder::setHWPresetType(v4l2_enc_hw_preset_type type)
  390. {
  391. struct v4l2_ext_control control;
  392. struct v4l2_ext_controls ctrls;
  393. RETURN_ERROR_IF_FORMATS_NOT_SET();
  394. RETURN_ERROR_IF_BUFFERS_REQUESTED();
  395. memset(&control, 0, sizeof(control));
  396. memset(&ctrls, 0, sizeof(ctrls));
  397. ctrls.count = 1;
  398. ctrls.controls = &control;
  399. ctrls.ctrl_class = V4L2_CTRL_CLASS_MPEG;
  400. control.id = V4L2_CID_MPEG_VIDEOENC_HW_PRESET_TYPE_PARAM;
  401. control.value = type;
  402. CHECK_V4L2_RETURN(setExtControls(ctrls),
  403. "Setting encoder HW Preset type to " << type);
  404. }
  405. int
  406. NvVideoEncoder::setROIParams(uint32_t buffer_index,
  407. v4l2_enc_frame_ROI_params &params)
  408. {
  409. struct v4l2_ext_control control;
  410. struct v4l2_ext_controls ctrls;
  411. RETURN_ERROR_IF_FORMATS_NOT_SET();
  412. RETURN_ERROR_IF_BUFFERS_NOT_REQUESTED();
  413. memset(&control, 0, sizeof(control));
  414. memset(&ctrls, 0, sizeof(ctrls));
  415. ctrls.count = 1;
  416. ctrls.controls = &control;
  417. ctrls.ctrl_class = V4L2_CTRL_CLASS_MPEG;
  418. control.id = V4L2_CID_MPEG_VIDEOENC_ROI_PARAMS;
  419. control.string = (char *) &params;
  420. params.config_store = buffer_index;
  421. CHECK_V4L2_RETURN(setExtControls(ctrls),
  422. "Setting encoder ROI params for buffer " << buffer_index);
  423. }
  424. int
  425. NvVideoEncoder::setPPEInitParams(v4l2_enc_ppe_init_params &params)
  426. {
  427. struct v4l2_ext_control control;
  428. struct v4l2_ext_controls ctrls;
  429. RETURN_ERROR_IF_FORMATS_NOT_SET();
  430. RETURN_ERROR_IF_BUFFERS_NOT_REQUESTED();
  431. memset(&control, 0, sizeof(control));
  432. memset(&ctrls, 0, sizeof(ctrls));
  433. ctrls.count = 1;
  434. ctrls.controls = &control;
  435. ctrls.ctrl_class = V4L2_CTRL_CLASS_MPEG;
  436. control.id = V4L2_CID_MPEG_VIDEOENC_PPE_INIT_PARAMS;
  437. control.string = (char *) &params;
  438. CHECK_V4L2_RETURN(setExtControls(ctrls),
  439. "Setting encoder PPE initialization params");
  440. }
  441. int
  442. NvVideoEncoder::SetInputMetaParams(uint32_t buffer_index,
  443. v4l2_ctrl_videoenc_input_metadata &params)
  444. {
  445. struct v4l2_ext_control control;
  446. struct v4l2_ext_controls ctrls;
  447. RETURN_ERROR_IF_FORMATS_NOT_SET();
  448. RETURN_ERROR_IF_BUFFERS_NOT_REQUESTED();
  449. memset(&control, 0, sizeof(control));
  450. memset(&ctrls, 0, sizeof(ctrls));
  451. ctrls.count = 1;
  452. ctrls.controls = &control;
  453. ctrls.ctrl_class = V4L2_CTRL_CLASS_MPEG;
  454. control.id = V4L2_CID_MPEG_VIDEOENC_INPUT_METADATA;
  455. control.string = (char *) &params;
  456. params.config_store = buffer_index;
  457. CHECK_V4L2_RETURN(setExtControls(ctrls),
  458. "Setting encoder input metadata Params");
  459. }
  460. int
  461. NvVideoEncoder::enableROI(v4l2_enc_enable_roi_param &params)
  462. {
  463. struct v4l2_ext_control control;
  464. struct v4l2_ext_controls ctrls;
  465. RETURN_ERROR_IF_FORMATS_NOT_SET();
  466. RETURN_ERROR_IF_BUFFERS_REQUESTED();
  467. memset(&control, 0, sizeof(control));
  468. memset(&ctrls, 0, sizeof(ctrls));
  469. ctrls.count = 1;
  470. ctrls.controls = &control;
  471. ctrls.ctrl_class = V4L2_CTRL_CLASS_MPEG;
  472. control.id = V4L2_CID_MPEG_VIDEOENC_ENABLE_ROI_PARAM;
  473. control.string = (char *) &params;
  474. CHECK_V4L2_RETURN(setExtControls(ctrls),
  475. "Enabling encoder ROI");
  476. }
  477. int
  478. NvVideoEncoder::enableReconCRC(v4l2_enc_enable_reconcrc_param &params)
  479. {
  480. struct v4l2_ext_control control;
  481. struct v4l2_ext_controls ctrls;
  482. RETURN_ERROR_IF_FORMATS_NOT_SET();
  483. RETURN_ERROR_IF_BUFFERS_REQUESTED();
  484. memset(&control, 0, sizeof(control));
  485. memset(&ctrls, 0, sizeof(ctrls));
  486. ctrls.count = 1;
  487. ctrls.controls = &control;
  488. ctrls.ctrl_class = V4L2_CTRL_CLASS_MPEG;
  489. control.id = V4L2_CID_MPEG_VIDEOENC_ENABLE_RECONCRC_PARAM;
  490. control.string = (char *) &params;
  491. CHECK_V4L2_RETURN(setExtControls(ctrls),
  492. "Enabling encoder ReconCRC");
  493. }
  494. int
  495. NvVideoEncoder::enableExternalRPS(v4l2_enc_enable_ext_rps_ctr &params)
  496. {
  497. struct v4l2_ext_control control;
  498. struct v4l2_ext_controls ctrls;
  499. RETURN_ERROR_IF_FORMATS_NOT_SET();
  500. RETURN_ERROR_IF_BUFFERS_REQUESTED();
  501. memset(&control, 0, sizeof(control));
  502. memset(&ctrls, 0, sizeof(ctrls));
  503. ctrls.count = 1;
  504. ctrls.controls = &control;
  505. ctrls.ctrl_class = V4L2_CTRL_CLASS_MPEG;
  506. control.id = V4L2_CID_MPEG_VIDEOENC_ENABLE_EXTERNAL_RPS_CONTROL;
  507. control.string = (char *) &params;
  508. CHECK_V4L2_RETURN(setExtControls(ctrls),
  509. "Enabling encoder External RPS");
  510. }
  511. int
  512. NvVideoEncoder::enableExternalRC(v4l2_enc_enable_ext_rate_ctr &params)
  513. {
  514. struct v4l2_ext_control control;
  515. struct v4l2_ext_controls ctrls;
  516. RETURN_ERROR_IF_FORMATS_NOT_SET();
  517. RETURN_ERROR_IF_BUFFERS_REQUESTED();
  518. memset(&control, 0, sizeof(control));
  519. memset(&ctrls, 0, sizeof(ctrls));
  520. ctrls.count = 1;
  521. ctrls.controls = &control;
  522. ctrls.ctrl_class = V4L2_CTRL_CLASS_MPEG;
  523. control.id = V4L2_CID_MPEG_VIDEOENC_ENABLE_EXTERNAL_RATE_CONTROL;
  524. control.string = (char *) &params;
  525. CHECK_V4L2_RETURN(setExtControls(ctrls),
  526. "Enabling encoder External RC");
  527. }
  528. int
  529. NvVideoEncoder::enableAV1Tile(v4l2_enc_av1_tile_config &params)
  530. {
  531. struct v4l2_ext_control control;
  532. struct v4l2_ext_controls ctrls;
  533. RETURN_ERROR_IF_FORMATS_NOT_SET();
  534. RETURN_ERROR_IF_BUFFERS_REQUESTED();
  535. memset(&control, 0, sizeof(control));
  536. memset(&ctrls, 0, sizeof(ctrls));
  537. ctrls.count = 1;
  538. ctrls.controls = &control;
  539. ctrls.ctrl_class = V4L2_CTRL_CLASS_MPEG;
  540. control.id = V4L2_CID_MPEG_VIDEOENC_AV1_TILE_CONFIGURATION;
  541. control.string = (char *) &params;
  542. CHECK_V4L2_RETURN(setExtControls(ctrls),
  543. "Enabling encoder AV1 tile");
  544. }
  545. int
  546. NvVideoEncoder::setAV1SsimRdo(bool enabled)
  547. {
  548. struct v4l2_ext_control control;
  549. struct v4l2_ext_controls ctrls;
  550. RETURN_ERROR_IF_FORMATS_NOT_SET();
  551. RETURN_ERROR_IF_BUFFERS_REQUESTED();
  552. memset(&control, 0, sizeof(control));
  553. memset(&ctrls, 0, sizeof(ctrls));
  554. ctrls.count = 1;
  555. ctrls.controls = &control;
  556. ctrls.ctrl_class = V4L2_CTRL_CLASS_MPEG;
  557. control.id = V4L2_CID_MPEG_VIDEOENC_AV1_ENABLE_SSIMRDO;
  558. control.value = enabled;
  559. CHECK_V4L2_RETURN(setExtControls(ctrls),
  560. "Setting encoder Ssim Rdo to " << enabled);
  561. }
  562. int
  563. NvVideoEncoder::setAV1DisableCDFUpdate(bool disabled)
  564. {
  565. struct v4l2_ext_control control;
  566. struct v4l2_ext_controls ctrls;
  567. RETURN_ERROR_IF_FORMATS_NOT_SET();
  568. RETURN_ERROR_IF_BUFFERS_REQUESTED();
  569. memset(&control, 0, sizeof(control));
  570. memset(&ctrls, 0, sizeof(ctrls));
  571. ctrls.count = 1;
  572. ctrls.controls = &control;
  573. ctrls.ctrl_class = V4L2_CTRL_CLASS_MPEG;
  574. control.id = V4L2_CID_MPEG_VIDEOENC_AV1_DISABLE_CDF_UPDATE;
  575. control.value = disabled;
  576. CHECK_V4L2_RETURN(setExtControls(ctrls),
  577. "Setting encoder CDF update to " << !disabled);
  578. }
  579. int
  580. NvVideoEncoder::setVirtualBufferSize(uint32_t size)
  581. {
  582. struct v4l2_ext_control control;
  583. struct v4l2_ext_controls ctrls;
  584. v4l2_enc_virtual_buffer_size buffer_size = {size};
  585. RETURN_ERROR_IF_FORMATS_NOT_SET();
  586. RETURN_ERROR_IF_BUFFERS_REQUESTED();
  587. memset(&control, 0, sizeof(control));
  588. memset(&ctrls, 0, sizeof(ctrls));
  589. ctrls.count = 1;
  590. ctrls.controls = &control;
  591. ctrls.ctrl_class = V4L2_CTRL_CLASS_MPEG;
  592. control.id = V4L2_CID_MPEG_VIDEOENC_VIRTUALBUFFER_SIZE;
  593. control.string = (char *) &buffer_size;
  594. CHECK_V4L2_RETURN(setExtControls(ctrls),
  595. "Setting encoder virtual buffer size to " << size);
  596. }
  597. int
  598. NvVideoEncoder::setNumReferenceFrames(uint32_t num_frames)
  599. {
  600. struct v4l2_ext_control control;
  601. struct v4l2_ext_controls ctrls;
  602. v4l2_enc_num_ref_frames frames = { num_frames };
  603. RETURN_ERROR_IF_FORMATS_NOT_SET();
  604. RETURN_ERROR_IF_BUFFERS_REQUESTED();
  605. memset(&control, 0, sizeof(control));
  606. memset(&ctrls, 0, sizeof(ctrls));
  607. ctrls.count = 1;
  608. ctrls.controls = &control;
  609. ctrls.ctrl_class = V4L2_CTRL_CLASS_MPEG;
  610. control.id = V4L2_CID_MPEG_VIDEOENC_NUM_REFERENCE_FRAMES;
  611. control.string = (char *) &frames;
  612. CHECK_V4L2_RETURN(setExtControls(ctrls),
  613. "Setting encoder number of reference frames to " << num_frames);
  614. }
  615. int
  616. NvVideoEncoder::setSliceIntrarefresh(uint32_t interval)
  617. {
  618. struct v4l2_ext_control control;
  619. struct v4l2_ext_controls ctrls;
  620. v4l2_enc_slice_intrarefresh_param param = {interval};
  621. RETURN_ERROR_IF_FORMATS_NOT_SET();
  622. RETURN_ERROR_IF_BUFFERS_REQUESTED();
  623. memset(&control, 0, sizeof(control));
  624. memset(&ctrls, 0, sizeof(ctrls));
  625. ctrls.count = 1;
  626. ctrls.controls = &control;
  627. ctrls.ctrl_class = V4L2_CTRL_CLASS_MPEG;
  628. control.id = V4L2_CID_MPEG_VIDEOENC_SLICE_INTRAREFRESH_PARAM;
  629. control.string = (char *) &param;
  630. CHECK_V4L2_RETURN(setExtControls(ctrls),
  631. "Setting encoder slice intrarefresh interval to " << interval);
  632. }
  633. int
  634. NvVideoEncoder::setNumBFrames(uint32_t num)
  635. {
  636. struct v4l2_ext_control control;
  637. struct v4l2_ext_controls ctrls;
  638. RETURN_ERROR_IF_FORMATS_NOT_SET();
  639. RETURN_ERROR_IF_BUFFERS_REQUESTED();
  640. memset(&control, 0, sizeof(control));
  641. memset(&ctrls, 0, sizeof(ctrls));
  642. ctrls.count = 1;
  643. ctrls.controls = &control;
  644. ctrls.ctrl_class = V4L2_CTRL_CLASS_MPEG;
  645. control.id = V4L2_CID_MPEG_VIDEOENC_NUM_BFRAMES;
  646. control.value = num;
  647. CHECK_V4L2_RETURN(setExtControls(ctrls),
  648. "Setting encoder number of B frames to " << num);
  649. }
  650. int
  651. NvVideoEncoder::setInsertSpsPpsAtIdrEnabled(bool enabled)
  652. {
  653. struct v4l2_ext_control control;
  654. struct v4l2_ext_controls ctrls;
  655. RETURN_ERROR_IF_FORMATS_NOT_SET();
  656. RETURN_ERROR_IF_BUFFERS_REQUESTED();
  657. memset(&control, 0, sizeof(control));
  658. memset(&ctrls, 0, sizeof(ctrls));
  659. ctrls.count = 1;
  660. ctrls.controls = &control;
  661. ctrls.ctrl_class = V4L2_CTRL_CLASS_MPEG;
  662. control.id = V4L2_CID_MPEG_VIDEOENC_INSERT_SPS_PPS_AT_IDR;
  663. control.value = enabled;
  664. CHECK_V4L2_RETURN(setExtControls(ctrls),
  665. "Setting encoder SPSPPSatIDR to " << enabled);
  666. }
  667. int
  668. NvVideoEncoder::setCABAC(bool enabled)
  669. {
  670. struct v4l2_ext_control control;
  671. struct v4l2_ext_controls ctrls;
  672. RETURN_ERROR_IF_FORMATS_NOT_SET();
  673. RETURN_ERROR_IF_BUFFERS_REQUESTED();
  674. memset(&control, 0, sizeof(control));
  675. memset(&ctrls, 0, sizeof(ctrls));
  676. ctrls.count = 1;
  677. ctrls.controls = &control;
  678. ctrls.ctrl_class = V4L2_CTRL_CLASS_MPEG;
  679. control.id = V4L2_CID_MPEG_VIDEO_H264_ENTROPY_MODE;
  680. control.value = enabled;
  681. CHECK_V4L2_RETURN(setExtControls(ctrls),
  682. "Setting encoder CABAC encoding to " << enabled);
  683. }
  684. int
  685. NvVideoEncoder::setSliceLevelEncode(bool enabled)
  686. {
  687. struct v4l2_ext_control control;
  688. struct v4l2_ext_controls ctrls;
  689. RETURN_ERROR_IF_FORMATS_NOT_SET();
  690. RETURN_ERROR_IF_BUFFERS_REQUESTED();
  691. memset(&control, 0, sizeof(control));
  692. memset(&ctrls, 0, sizeof(ctrls));
  693. ctrls.count = 1;
  694. ctrls.controls = &control;
  695. ctrls.ctrl_class = V4L2_CTRL_CLASS_MPEG;
  696. control.id = V4L2_CID_MPEG_VIDEOENC_ENABLE_SLICE_LEVEL_ENCODE;
  697. control.value = enabled;
  698. CHECK_V4L2_RETURN(setExtControls(ctrls),
  699. "Setting encode slice level encoding to " << enabled);
  700. }
  701. int
  702. NvVideoEncoder::enableMotionVectorReporting()
  703. {
  704. struct v4l2_ext_control control;
  705. struct v4l2_ext_controls ctrls;
  706. RETURN_ERROR_IF_FORMATS_NOT_SET();
  707. RETURN_ERROR_IF_BUFFERS_REQUESTED();
  708. memset(&control, 0, sizeof(control));
  709. memset(&ctrls, 0, sizeof(ctrls));
  710. ctrls.count = 1;
  711. ctrls.controls = &control;
  712. ctrls.ctrl_class = V4L2_CTRL_CLASS_MPEG;
  713. control.id = V4L2_CID_MPEG_VIDEOENC_ENABLE_METADATA_MV;
  714. control.value = 1;
  715. CHECK_V4L2_RETURN(setExtControls(ctrls),
  716. "Enabling encoder motion vector reporting");
  717. }
  718. int
  719. NvVideoEncoder::getMetadata(uint32_t buffer_index,
  720. v4l2_ctrl_videoenc_outputbuf_metadata &enc_metadata)
  721. {
  722. v4l2_ctrl_video_metadata metadata;
  723. struct v4l2_ext_control control;
  724. struct v4l2_ext_controls ctrls;
  725. RETURN_ERROR_IF_BUFFERS_NOT_REQUESTED();
  726. ctrls.count = 1;
  727. ctrls.controls = &control;
  728. ctrls.ctrl_class = V4L2_CTRL_CLASS_MPEG;
  729. metadata.buffer_index = buffer_index;
  730. metadata.VideoEncMetadata = &enc_metadata;
  731. control.id = V4L2_CID_MPEG_VIDEOENC_METADATA;
  732. control.string = (char *)&metadata;
  733. CHECK_V4L2_RETURN(getExtControls(ctrls),
  734. "Getting encoder output metadata for buffer " << buffer_index);
  735. }
  736. int
  737. NvVideoEncoder::getMotionVectors(uint32_t buffer_index,
  738. v4l2_ctrl_videoenc_outputbuf_metadata_MV &enc_mv_metadata)
  739. {
  740. v4l2_ctrl_video_metadata metadata;
  741. struct v4l2_ext_control control;
  742. struct v4l2_ext_controls ctrls;
  743. RETURN_ERROR_IF_BUFFERS_NOT_REQUESTED();
  744. ctrls.count = 1;
  745. ctrls.controls = &control;
  746. ctrls.ctrl_class = V4L2_CTRL_CLASS_MPEG;
  747. metadata.buffer_index = buffer_index;
  748. metadata.VideoEncMetadataMV = &enc_mv_metadata;
  749. control.id = V4L2_CID_MPEG_VIDEOENC_METADATA_MV;
  750. control.string = (char *)&metadata;
  751. CHECK_V4L2_RETURN(getExtControls(ctrls),
  752. "Getting encoder output MV metadata for buffer " << buffer_index);
  753. }
  754. int
  755. NvVideoEncoder::setQpRange(uint32_t MinQpI, uint32_t MaxQpI, uint32_t MinQpP,
  756. uint32_t MaxQpP, uint32_t MinQpB, uint32_t MaxQpB)
  757. {
  758. v4l2_ctrl_video_qp_range qprange;
  759. struct v4l2_ext_control control;
  760. struct v4l2_ext_controls ctrls;
  761. RETURN_ERROR_IF_FORMATS_NOT_SET();
  762. RETURN_ERROR_IF_BUFFERS_REQUESTED();
  763. memset(&control, 0, sizeof(control));
  764. memset(&ctrls, 0, sizeof(ctrls));
  765. qprange.MinQpI = MinQpI;
  766. qprange.MaxQpI = MaxQpI;
  767. qprange.MinQpP = MinQpP;
  768. qprange.MaxQpP = MaxQpP;
  769. qprange.MinQpB = MinQpB;
  770. qprange.MaxQpB = MaxQpB;
  771. ctrls.count = 1;
  772. ctrls.controls = &control;
  773. ctrls.ctrl_class = V4L2_CTRL_CLASS_MPEG;
  774. control.id = V4L2_CID_MPEG_VIDEOENC_QP_RANGE;
  775. control.string = (char *)&qprange;
  776. CHECK_V4L2_RETURN(setExtControls(ctrls),
  777. "Setting encoder Qp range " << ctrls.count);
  778. }
  779. int
  780. NvVideoEncoder::setSampleAspectRatioWidth(uint32_t sar_width)
  781. {
  782. struct v4l2_ext_control control;
  783. struct v4l2_ext_controls ctrls;
  784. RETURN_ERROR_IF_FORMATS_NOT_SET();
  785. RETURN_ERROR_IF_BUFFERS_REQUESTED();
  786. memset(&control, 0, sizeof(control));
  787. memset(&ctrls, 0, sizeof(ctrls));
  788. ctrls.count = 1;
  789. ctrls.controls = &control;
  790. ctrls.ctrl_class = V4L2_CTRL_CLASS_MPEG;
  791. switch (capture_plane_pixfmt)
  792. {
  793. case V4L2_PIX_FMT_H264:
  794. control.id = V4L2_CID_MPEG_VIDEO_H264_VUI_EXT_SAR_WIDTH;
  795. break;
  796. case V4L2_PIX_FMT_H265:
  797. control.id = V4L2_CID_MPEG_VIDEOENC_H265_VUI_EXT_SAR_WIDTH;
  798. break;
  799. default:
  800. COMP_ERROR_MSG("Unsupported encoder type");
  801. return -1;
  802. }
  803. control.value = sar_width;
  804. CHECK_V4L2_RETURN(setExtControls(ctrls),
  805. "Setting encoder SAR width to " << sar_width);
  806. }
  807. int
  808. NvVideoEncoder::setSampleAspectRatioHeight(uint32_t sar_height)
  809. {
  810. struct v4l2_ext_control control;
  811. struct v4l2_ext_controls ctrls;
  812. RETURN_ERROR_IF_FORMATS_NOT_SET();
  813. RETURN_ERROR_IF_BUFFERS_REQUESTED();
  814. memset(&control, 0, sizeof(control));
  815. memset(&ctrls, 0, sizeof(ctrls));
  816. ctrls.count = 1;
  817. ctrls.controls = &control;
  818. ctrls.ctrl_class = V4L2_CTRL_CLASS_MPEG;
  819. switch (capture_plane_pixfmt)
  820. {
  821. case V4L2_PIX_FMT_H264:
  822. control.id = V4L2_CID_MPEG_VIDEO_H264_VUI_EXT_SAR_HEIGHT;
  823. break;
  824. case V4L2_PIX_FMT_H265:
  825. control.id = V4L2_CID_MPEG_VIDEOENC_H265_VUI_EXT_SAR_HEIGHT;
  826. break;
  827. default:
  828. COMP_ERROR_MSG("Unsupported encoder type");
  829. return -1;
  830. }
  831. control.value = sar_height;
  832. CHECK_V4L2_RETURN(setExtControls(ctrls),
  833. "Setting encoder SAR width to " << sar_height);
  834. }
  835. int
  836. NvVideoEncoder::setInsertVuiEnabled(bool enabled)
  837. {
  838. struct v4l2_ext_control control;
  839. struct v4l2_ext_controls ctrls;
  840. RETURN_ERROR_IF_FORMATS_NOT_SET();
  841. RETURN_ERROR_IF_BUFFERS_REQUESTED();
  842. memset(&control, 0, sizeof(control));
  843. memset(&ctrls, 0, sizeof(ctrls));
  844. ctrls.count = 1;
  845. ctrls.controls = &control;
  846. ctrls.ctrl_class = V4L2_CTRL_CLASS_MPEG;
  847. control.id = V4L2_CID_MPEG_VIDEOENC_INSERT_VUI;
  848. control.value = enabled;
  849. CHECK_V4L2_RETURN(setExtControls(ctrls),
  850. "Setting encoder InsertVUI to " << enabled);
  851. }
  852. int
  853. NvVideoEncoder::setExtendedColorFormat(bool enabled)
  854. {
  855. struct v4l2_ext_control control;
  856. struct v4l2_ext_controls ctrls;
  857. RETURN_ERROR_IF_FORMATS_NOT_SET();
  858. RETURN_ERROR_IF_BUFFERS_REQUESTED();
  859. memset(&control, 0, sizeof(control));
  860. memset(&ctrls, 0, sizeof(ctrls));
  861. ctrls.count = 1;
  862. ctrls.controls = &control;
  863. ctrls.ctrl_class = V4L2_CTRL_CLASS_MPEG;
  864. control.id = V4L2_CID_MPEG_VIDEOENC_EXTEDED_COLORFORMAT;
  865. control.value = enabled;
  866. CHECK_V4L2_RETURN(setExtControls(ctrls),
  867. "Setting encoder extended colorformat to " << enabled);
  868. }
  869. int
  870. NvVideoEncoder::setInsertAudEnabled(bool enabled)
  871. {
  872. struct v4l2_ext_control control;
  873. struct v4l2_ext_controls ctrls;
  874. RETURN_ERROR_IF_FORMATS_NOT_SET();
  875. RETURN_ERROR_IF_BUFFERS_REQUESTED();
  876. memset(&control, 0, sizeof(control));
  877. memset(&ctrls, 0, sizeof(ctrls));
  878. ctrls.count = 1;
  879. ctrls.controls = &control;
  880. ctrls.ctrl_class = V4L2_CTRL_CLASS_MPEG;
  881. control.id = V4L2_CID_MPEG_VIDEOENC_INSERT_AUD;
  882. control.value = enabled;
  883. CHECK_V4L2_RETURN(setExtControls(ctrls),
  884. "Setting encoder InsertAUD to " << enabled);
  885. }
  886. int
  887. NvVideoEncoder::setAlliFramesEncode(bool enabled)
  888. {
  889. struct v4l2_ext_control control;
  890. struct v4l2_ext_controls ctrls;
  891. RETURN_ERROR_IF_FORMATS_NOT_SET();
  892. RETURN_ERROR_IF_BUFFERS_REQUESTED();
  893. memset(&control, 0, sizeof(control));
  894. memset(&ctrls, 0, sizeof(ctrls));
  895. ctrls.count = 1;
  896. ctrls.controls = &control;
  897. ctrls.ctrl_class = V4L2_CTRL_CLASS_MPEG;
  898. control.id = V4L2_CID_MPEG_VIDEOENC_ENABLE_ALLIFRAME_ENCODE;
  899. control.value = enabled;
  900. CHECK_V4L2_RETURN(setExtControls(ctrls),
  901. "Setting encoder InsertAUD to " << enabled);
  902. }
  903. int
  904. NvVideoEncoder::DevicePoll(v4l2_ctrl_video_device_poll *devicepoll)
  905. {
  906. struct v4l2_ext_control control;
  907. struct v4l2_ext_controls ctrls;
  908. RETURN_ERROR_IF_FORMATS_NOT_SET();
  909. memset(&control, 0, sizeof(control));
  910. memset(&ctrls, 0, sizeof(ctrls));
  911. ctrls.count = 1;
  912. ctrls.controls = &control;
  913. ctrls.ctrl_class = V4L2_CTRL_CLASS_MPEG;
  914. control.id = V4L2_CID_MPEG_VIDEO_DEVICE_POLL;
  915. control.string = (char *)devicepoll;
  916. CHECK_V4L2_RETURN(setExtControls(ctrls),
  917. "Done calling video device poll ");
  918. }
  919. int
  920. NvVideoEncoder::SetPollInterrupt()
  921. {
  922. struct v4l2_ext_control control;
  923. struct v4l2_ext_controls ctrls;
  924. RETURN_ERROR_IF_FORMATS_NOT_SET();
  925. memset(&control, 0, sizeof(control));
  926. memset(&ctrls, 0, sizeof(ctrls));
  927. ctrls.count = 1;
  928. ctrls.controls = &control;
  929. ctrls.ctrl_class = V4L2_CTRL_CLASS_MPEG;
  930. control.id = V4L2_CID_MPEG_SET_POLL_INTERRUPT;
  931. control.value = 1;
  932. CHECK_V4L2_RETURN(setExtControls(ctrls),
  933. "Setting encoder poll interrupt to 1 ");
  934. }
  935. int
  936. NvVideoEncoder::ClearPollInterrupt()
  937. {
  938. struct v4l2_ext_control control;
  939. struct v4l2_ext_controls ctrls;
  940. RETURN_ERROR_IF_FORMATS_NOT_SET();
  941. memset(&control, 0, sizeof(control));
  942. memset(&ctrls, 0, sizeof(ctrls));
  943. ctrls.count = 1;
  944. ctrls.controls = &control;
  945. ctrls.ctrl_class = V4L2_CTRL_CLASS_MPEG;
  946. control.id = V4L2_CID_MPEG_SET_POLL_INTERRUPT;
  947. control.value = 0;
  948. CHECK_V4L2_RETURN(setExtControls(ctrls),
  949. "Setting encoder poll interrupt to 0 ");
  950. }
  951. int
  952. NvVideoEncoder::setPocType(uint32_t pocType)
  953. {
  954. struct v4l2_ext_control control;
  955. struct v4l2_ext_controls ctrls;
  956. RETURN_ERROR_IF_FORMATS_NOT_SET();
  957. RETURN_ERROR_IF_BUFFERS_REQUESTED();
  958. memset(&control, 0, sizeof(control));
  959. memset(&ctrls, 0, sizeof(ctrls));
  960. ctrls.count = 1;
  961. ctrls.controls = &control;
  962. ctrls.ctrl_class = V4L2_CTRL_CLASS_MPEG;
  963. control.id = V4L2_CID_MPEG_VIDEOENC_POC_TYPE;
  964. control.value = pocType;
  965. CHECK_V4L2_RETURN(setExtControls(ctrls),
  966. "Setting Pic_order_cnt_type to " << pocType);
  967. }
  968. int NvVideoEncoder::setInitQP(uint32_t IinitQP,
  969. uint32_t PinitQP, uint32_t BinitQP)
  970. {
  971. v4l2_ctrl_video_init_qp initqp;
  972. struct v4l2_ext_control control;
  973. struct v4l2_ext_controls ctrls;
  974. RETURN_ERROR_IF_FORMATS_NOT_SET();
  975. RETURN_ERROR_IF_BUFFERS_REQUESTED();
  976. memset(&control, 0, sizeof(control));
  977. memset(&ctrls, 0, sizeof(ctrls));
  978. initqp.IInitQP = IinitQP;
  979. initqp.PInitQP = PinitQP;
  980. initqp.BInitQP = BinitQP;
  981. ctrls.count = 1;
  982. ctrls.controls = &control;
  983. ctrls.ctrl_class = V4L2_CTRL_CLASS_MPEG;
  984. control.id = V4L2_CID_MPEG_VIDEOENC_INIT_FRAME_QP;
  985. control.string = (char *)&initqp;
  986. CHECK_V4L2_RETURN(setExtControls(ctrls),
  987. "Setting encoder Init QP " << ctrls.count);
  988. }
  989. int
  990. NvVideoEncoder::setFramesToEncode(uint32_t framesToEncode)
  991. {
  992. struct v4l2_ext_control control;
  993. struct v4l2_ext_controls ctrls;
  994. memset(&control, 0, sizeof(control));
  995. memset(&ctrls, 0, sizeof(ctrls));
  996. ctrls.count = 1;
  997. ctrls.controls = &control;
  998. ctrls.ctrl_class = V4L2_CTRL_CLASS_MPEG;
  999. control.id = V4L2_CID_MPEG_VIDEOENC_FRAMES_TO_ENCODE;
  1000. control.value = framesToEncode;
  1001. CHECK_V4L2_RETURN(setExtControls(ctrls),
  1002. "Setting num_frames_to_encode to " << framesToEncode);
  1003. }
  1004. int
  1005. NvVideoEncoder::setChromaFactorIDC(uint8_t value)
  1006. {
  1007. struct v4l2_ext_control control;
  1008. struct v4l2_ext_controls ctrls;
  1009. RETURN_ERROR_IF_FORMATS_NOT_SET();
  1010. RETURN_ERROR_IF_BUFFERS_REQUESTED();
  1011. memset(&control, 0, sizeof(control));
  1012. memset(&ctrls, 0, sizeof(ctrls));
  1013. ctrls.count = 1;
  1014. ctrls.controls = &control;
  1015. ctrls.ctrl_class = V4L2_CTRL_CLASS_MPEG;
  1016. control.id = V4L2_CID_MPEG_VIDEOENC_H265_CHROMA_FACTOR_IDC;
  1017. control.value = value;
  1018. CHECK_V4L2_RETURN(setExtControls(ctrls),
  1019. "Setting chroma_format_idc to " << value);
  1020. }
  1021. int
  1022. NvVideoEncoder::setLossless(bool enabled)
  1023. {
  1024. struct v4l2_ext_control control;
  1025. struct v4l2_ext_controls ctrls;
  1026. RETURN_ERROR_IF_FORMATS_NOT_SET();
  1027. RETURN_ERROR_IF_BUFFERS_REQUESTED();
  1028. memset(&control, 0, sizeof(control));
  1029. memset(&ctrls, 0, sizeof(ctrls));
  1030. ctrls.count = 1;
  1031. ctrls.controls = &control;
  1032. ctrls.ctrl_class = V4L2_CTRL_CLASS_MPEG;
  1033. control.id = V4L2_CID_MPEG_VIDEOENC_ENABLE_LOSSLESS;
  1034. control.value = enabled;
  1035. CHECK_V4L2_RETURN(setExtControls(ctrls),
  1036. "Setting lossless encoding to " << enabled);
  1037. }