video_encode_main.cpp 96 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746
  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 "NvUtils.h"
  29. #include <fstream>
  30. #include <iostream>
  31. #include <linux/videodev2.h>
  32. #include <malloc.h>
  33. #include <sstream>
  34. #include <string.h>
  35. #include <fcntl.h>
  36. #include <poll.h>
  37. #include "video_encode.h"
  38. #define TEST_ERROR(cond, str, label) if(cond) { \
  39. cerr << str << endl; \
  40. error = 1; \
  41. goto label; }
  42. #define TEST_PARSE_ERROR(cond, label) if(cond) { \
  43. cerr << "Error parsing runtime parameter changes string" << endl; \
  44. goto label; }
  45. #define IS_DIGIT(c) (c >= '0' && c <= '9')
  46. #define MICROSECOND_UNIT 1000000
  47. using namespace std;
  48. /**
  49. * Abort on error.
  50. *
  51. * @param ctx : Encoder context
  52. */
  53. static void
  54. abort(context_t *ctx)
  55. {
  56. ctx->got_error = true;
  57. ctx->enc->abort();
  58. }
  59. static const char*
  60. get_pixfmt_string(uint32_t pixfmt)
  61. {
  62. switch (pixfmt)
  63. {
  64. case V4L2_PIX_FMT_YUV420M: return "V4L2_PIX_FMT_YUV420M";
  65. case V4L2_PIX_FMT_NV12M: return "V4L2_PIX_FMT_NV12M";
  66. case V4L2_PIX_FMT_YUV444M: return "V4L2_PIX_FMT_YUV444M";
  67. case V4L2_PIX_FMT_NV24M: return "V4L2_PIX_FMT_NV24M";
  68. case V4L2_PIX_FMT_P010M: return "V4L2_PIX_FMT_P010M";
  69. case V4L2_PIX_FMT_NV24_10LE: return "V4L2_PIX_FMT_NV24_10LE";
  70. case V4L2_PIX_FMT_H264: return "V4L2_PIX_FMT_H264";
  71. case V4L2_PIX_FMT_H265: return "V4L2_PIX_FMT_H265";
  72. case V4L2_PIX_FMT_VP8: return "V4L2_PIX_FMT_VP8";
  73. case V4L2_PIX_FMT_VP9: return "V4L2_PIX_FMT_VP9";
  74. default: return "";
  75. }
  76. }
  77. /**
  78. * Initialise CRC Rec and creates CRC Table based on the polynomial.
  79. *
  80. * @param CrcPolynomial : CRC Polynomial values
  81. */
  82. static
  83. Crc* InitCrc(unsigned int CrcPolynomial)
  84. {
  85. unsigned short int i;
  86. unsigned short int j;
  87. unsigned int tempcrc;
  88. Crc *phCrc;
  89. phCrc = (Crc*) malloc (sizeof(Crc));
  90. if (phCrc == NULL)
  91. {
  92. cerr << "Mem allocation failed for Init CRC" <<endl;
  93. return NULL;
  94. }
  95. memset (phCrc, 0, sizeof(Crc));
  96. for (i = 0; i <= 255; i++)
  97. {
  98. tempcrc = i;
  99. for (j = 8; j > 0; j--)
  100. {
  101. if (tempcrc & 1)
  102. {
  103. tempcrc = (tempcrc >> 1) ^ CrcPolynomial;
  104. }
  105. else
  106. {
  107. tempcrc >>= 1;
  108. }
  109. }
  110. phCrc->CRCTable[i] = tempcrc;
  111. }
  112. phCrc->CrcValue = 0;
  113. return phCrc;
  114. }
  115. /**
  116. * Calculates CRC of data provided in by buffer.
  117. *
  118. * @param *phCrc : bitstream CRC
  119. * @param buffer : process buffer
  120. * @param count : bytes used
  121. */
  122. static
  123. void CalculateCrc(Crc *phCrc, unsigned char *buffer, uint32_t count)
  124. {
  125. unsigned char *p;
  126. unsigned int temp1;
  127. unsigned int temp2;
  128. unsigned int crc = phCrc->CrcValue;
  129. unsigned int *CRCTable = phCrc->CRCTable;
  130. if(!count)
  131. return;
  132. p = (unsigned char *) buffer;
  133. while (count-- != 0)
  134. {
  135. temp1 = (crc >> 8) & 0x00FFFFFFL;
  136. temp2 = CRCTable[((unsigned int) crc ^ *p++) & 0xFF];
  137. crc = temp1 ^ temp2;
  138. }
  139. phCrc->CrcValue = crc;
  140. }
  141. /**
  142. * Closes CRC related handles.
  143. *
  144. * @param *phCrc : bitstream CRC
  145. */
  146. static
  147. void CloseCrc(Crc **phCrc)
  148. {
  149. if (*phCrc)
  150. free (*phCrc);
  151. }
  152. /**
  153. * Write encoded frame data.
  154. *
  155. * @param stream : output stream
  156. * @param buffer : output nvbuffer
  157. */
  158. static int
  159. write_encoder_output_frame(ofstream * stream, NvBuffer * buffer)
  160. {
  161. printf("write-----\n");
  162. stream->write((char *) buffer->planes[0].data, buffer->planes[0].bytesused);
  163. return 0;
  164. }
  165. /**
  166. * Encoder capture-plane deque buffer callback function.
  167. *
  168. * @param v4l2_buf : v4l2 buffer
  169. * @param buffer : NvBuffer
  170. * @param shared_buffer : shared NvBuffer
  171. * @param arg : context pointer
  172. */
  173. static bool
  174. encoder_capture_plane_dq_callback(struct v4l2_buffer *v4l2_buf, NvBuffer * buffer,
  175. NvBuffer * shared_buffer, void *arg)
  176. {
  177. context_t *ctx = (context_t *) arg;
  178. NvVideoEncoder *enc = ctx->enc;
  179. pthread_setname_np(pthread_self(), "EncCapPlane");
  180. uint32_t frame_num = ctx->enc->capture_plane.getTotalDequeuedBuffers() - 1;
  181. uint32_t ReconRef_Y_CRC = 0;
  182. uint32_t ReconRef_U_CRC = 0;
  183. uint32_t ReconRef_V_CRC = 0;
  184. static uint32_t num_encoded_frames = 1;
  185. struct v4l2_event ev;
  186. int ret = 0;
  187. if (v4l2_buf == NULL)
  188. {
  189. cout << "Error while dequeing buffer from output plane" << endl;
  190. abort(ctx);
  191. return false;
  192. }
  193. if (ctx->b_use_enc_cmd)
  194. {
  195. if(v4l2_buf->flags & V4L2_BUF_FLAG_LAST)
  196. {
  197. memset(&ev,0,sizeof(struct v4l2_event));
  198. ret = ctx->enc->dqEvent(ev,1000);
  199. if (ret < 0)
  200. cout << "Error in dqEvent" << endl;
  201. if(ev.type == V4L2_EVENT_EOS)
  202. return false;
  203. }
  204. }
  205. /* Received EOS from encoder. Stop dqthread. */
  206. if (buffer->planes[0].bytesused == 0)
  207. {
  208. cout << "Got 0 size buffer in capture \n";
  209. return false;
  210. }
  211. /* Computing CRC with each frame */
  212. if(ctx->pBitStreamCrc)
  213. CalculateCrc (ctx->pBitStreamCrc, buffer->planes[0].data, buffer->planes[0].bytesused);
  214. if (!ctx->stats)
  215. printf("ctx-stats ------------------------------------------------------ \n");
  216. write_encoder_output_frame(ctx->out_file, buffer);
  217. /* Accounting for the first frame as it is only sps+pps */
  218. if (ctx->gdr_out_frame_number != 0xFFFFFFFF)
  219. if ( (ctx->enableGDR) && (ctx->GDR_out_file_path) && (num_encoded_frames >= ctx->gdr_out_frame_number+1))
  220. write_encoder_output_frame(ctx->gdr_out_file, buffer);
  221. num_encoded_frames++;
  222. if (ctx->report_metadata)
  223. {
  224. v4l2_ctrl_videoenc_outputbuf_metadata enc_metadata;
  225. if (ctx->enc->getMetadata(v4l2_buf->index, enc_metadata) == 0)
  226. {
  227. if (ctx->bReconCrc && enc_metadata.bValidReconCRC) {
  228. /* CRC for Recon frame */
  229. cout << "Frame: " << frame_num << endl;
  230. cout << "ReconFrame_Y_CRC " << enc_metadata.ReconFrame_Y_CRC <<
  231. " ReconFrame_U_CRC " << enc_metadata.ReconFrame_U_CRC <<
  232. " ReconFrame_V_CRC " << enc_metadata.ReconFrame_V_CRC <<
  233. endl;
  234. if (!ctx->recon_Ref_file->eof())
  235. {
  236. string recon_ref_YUV_data[4];
  237. parse_csv_recon_file(ctx->recon_Ref_file, recon_ref_YUV_data);
  238. ReconRef_Y_CRC = stoul(recon_ref_YUV_data[0]);
  239. ReconRef_U_CRC = stoul(recon_ref_YUV_data[1]);
  240. ReconRef_V_CRC = stoul(recon_ref_YUV_data[2]);
  241. }
  242. if ((ReconRef_Y_CRC != enc_metadata.ReconFrame_Y_CRC) ||
  243. (ReconRef_U_CRC != enc_metadata.ReconFrame_U_CRC) ||
  244. (ReconRef_V_CRC != enc_metadata.ReconFrame_V_CRC))
  245. {
  246. cout << "Recon CRC FAIL" << endl;
  247. cout << "ReconRef_Y_CRC " << ReconRef_Y_CRC <<
  248. " ReconRef_U_CRC " << ReconRef_U_CRC <<
  249. " ReconRef_V_CRC " << ReconRef_V_CRC <<
  250. endl;
  251. abort(ctx);
  252. return false;
  253. }
  254. cout << "Recon CRC PASS for frame : " << frame_num << endl;
  255. } else if (ctx->externalRPS && enc_metadata.bRPSFeedback_status) {
  256. /* RPS Feedback */
  257. ctx->rps_par.nActiveRefFrames = enc_metadata.nActiveRefFrames;
  258. cout << "Frame: " << frame_num << endl;
  259. cout << "nCurrentRefFrameId " << enc_metadata.nCurrentRefFrameId <<
  260. " nActiveRefFrames " << enc_metadata.nActiveRefFrames << endl;
  261. for (uint32_t i = 0; i < enc_metadata.nActiveRefFrames; i++)
  262. {
  263. /* Update RPS List */
  264. ctx->rps_par.rps_list[i].nFrameId = enc_metadata.RPSList[i].nFrameId;
  265. ctx->rps_par.rps_list[i].bLTRefFrame = enc_metadata.RPSList[i].bLTRefFrame;
  266. cout << "FrameId " << enc_metadata.RPSList[i].nFrameId <<
  267. " IdrFrame " << (int) enc_metadata.RPSList[i].bIdrFrame <<
  268. " LTRefFrame " << (int) enc_metadata.RPSList[i].bLTRefFrame <<
  269. " PictureOrderCnt " << enc_metadata.RPSList[i].nPictureOrderCnt <<
  270. " FrameNum " << enc_metadata.RPSList[i].nFrameNum <<
  271. " LTFrameIdx " << enc_metadata.RPSList[i].nLTRFrameIdx << endl;
  272. }
  273. } else if (ctx->externalRCHints) {
  274. /* Rate Control Feedback */
  275. cout << "Frame: " << frame_num << endl;
  276. cout << "EncodedBits " << enc_metadata.EncodedFrameBits <<
  277. " MinQP " << enc_metadata.FrameMinQP <<
  278. " MaxQP " << enc_metadata.FrameMaxQP <<
  279. endl;
  280. } else {
  281. cout << "Frame " << frame_num <<
  282. ": isKeyFrame=" << (int) enc_metadata.KeyFrame <<
  283. " AvgQP=" << enc_metadata.AvgQP <<
  284. " MinQP=" << enc_metadata.FrameMinQP <<
  285. " MaxQP=" << enc_metadata.FrameMaxQP <<
  286. " EncodedBits=" << enc_metadata.EncodedFrameBits <<
  287. endl;
  288. }
  289. }
  290. }
  291. if (ctx->dump_mv)
  292. {
  293. /* Get motion vector parameters of the frames from encoder */
  294. v4l2_ctrl_videoenc_outputbuf_metadata_MV enc_mv_metadata;
  295. if (ctx->enc->getMotionVectors(v4l2_buf->index, enc_mv_metadata) == 0)
  296. {
  297. uint32_t numMVs = enc_mv_metadata.bufSize / sizeof(MVInfo);
  298. MVInfo *pInfo = enc_mv_metadata.pMVInfo;
  299. cout << "Frame " << frame_num << ": Num MVs=" << numMVs << endl;
  300. for (uint32_t i = 0; i < numMVs; i++, pInfo++)
  301. {
  302. cout << i << ": mv_x=" << pInfo->mv_x <<
  303. " mv_y=" << pInfo->mv_y <<
  304. " weight=" << pInfo->weight <<
  305. endl;
  306. }
  307. }
  308. }
  309. if (ctx->blocking_mode && ctx->RPS_threeLayerSvc)
  310. {
  311. sem_post(&ctx->rps_par.sema);
  312. }
  313. /* encoder qbuffer for capture plane */
  314. if (enc->capture_plane.qBuffer(*v4l2_buf, NULL) < 0)
  315. {
  316. cerr << "Error while Qing buffer at capture plane" << endl;
  317. abort(ctx);
  318. return false;
  319. }
  320. return true;
  321. }
  322. /**
  323. * Parse runtime command stream.
  324. *
  325. * @param ctx : Encoder context
  326. * @param id : string id
  327. * @param value : Integer value
  328. */
  329. static int
  330. get_next_parsed_pair(context_t *ctx, char *id, uint32_t *value)
  331. {
  332. char charval;
  333. *ctx->runtime_params_str >> *id;
  334. if (ctx->runtime_params_str->eof())
  335. {
  336. return -1;
  337. }
  338. charval = ctx->runtime_params_str->peek();
  339. if (!IS_DIGIT(charval))
  340. {
  341. return -1;
  342. }
  343. *ctx->runtime_params_str >> *value;
  344. *ctx->runtime_params_str >> charval;
  345. if (ctx->runtime_params_str->eof())
  346. {
  347. return 0;
  348. }
  349. return charval;
  350. }
  351. /**
  352. * Set Runtime Parameters.
  353. *
  354. * @param ctx : Encoder context
  355. */
  356. static int
  357. set_runtime_params(context_t *ctx)
  358. {
  359. char charval;
  360. uint32_t intval;
  361. int ret, next;
  362. cout << "Frame " << ctx->next_param_change_frame <<
  363. ": Changing parameters" << endl;
  364. while (!ctx->runtime_params_str->eof())
  365. {
  366. next = get_next_parsed_pair(ctx, &charval, &intval);
  367. TEST_PARSE_ERROR(next < 0, err);
  368. switch (charval)
  369. {
  370. case 'b':
  371. if (ctx->ratecontrol == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR &&
  372. ctx->peak_bitrate < intval) {
  373. uint32_t peak_bitrate = 1.2f * intval;
  374. cout << "Peak bitrate = " << peak_bitrate << endl;
  375. ret = ctx->enc->setPeakBitrate(peak_bitrate);
  376. if (ret < 0)
  377. {
  378. cerr << "Could not set encoder peakbitrate" << endl;
  379. goto err;
  380. }
  381. }
  382. cout << "Bitrate = " << intval << endl;
  383. ret = ctx->enc->setBitrate(intval);
  384. if (ret < 0)
  385. {
  386. cerr << "Could not set encoder bitrate" << endl;
  387. goto err;
  388. }
  389. break;
  390. case 'p':
  391. cout << "Peak bitrate = " << intval << endl;
  392. ret = ctx->enc->setPeakBitrate(intval);
  393. if (ret < 0)
  394. {
  395. cerr << "Could not set encoder peakbitrate" << endl;
  396. goto err;
  397. }
  398. break;
  399. case 'r':
  400. {
  401. int fps_num = intval;
  402. TEST_PARSE_ERROR(next != '/', err);
  403. ctx->runtime_params_str->seekg(-1, ios::cur);
  404. next = get_next_parsed_pair(ctx, &charval, &intval);
  405. TEST_PARSE_ERROR(next < 0, err);
  406. cout << "Framerate = " << fps_num << "/" << intval << endl;
  407. ret = ctx->enc->setFrameRate(fps_num, intval);
  408. if (ret < 0)
  409. {
  410. cerr << "Could not set framerate" << endl;
  411. goto err;
  412. }
  413. break;
  414. }
  415. case 'i':
  416. if (intval > 0)
  417. {
  418. ctx->enc->forceIDR();
  419. cout << "Forcing IDR" << endl;
  420. }
  421. break;
  422. default:
  423. TEST_PARSE_ERROR(true, err);
  424. }
  425. switch (next)
  426. {
  427. case 0:
  428. delete ctx->runtime_params_str;
  429. ctx->runtime_params_str = NULL;
  430. return 0;
  431. case '#':
  432. return 0;
  433. case ',':
  434. break;
  435. default:
  436. break;
  437. }
  438. }
  439. return 0;
  440. err:
  441. cerr << "Skipping further runtime parameter changes" <<endl;
  442. delete ctx->runtime_params_str;
  443. ctx->runtime_params_str = NULL;
  444. return -1;
  445. }
  446. /**
  447. * Get the next runtime parameters change for frame.
  448. *
  449. * @param ctx : Encoder context
  450. */
  451. static int
  452. get_next_runtime_param_change_frame(context_t *ctx)
  453. {
  454. char charval;
  455. int ret;
  456. ret = get_next_parsed_pair(ctx, &charval, &ctx->next_param_change_frame);
  457. if(ret == 0)
  458. {
  459. return 0;
  460. }
  461. TEST_PARSE_ERROR((ret != ';' && ret != ',') || charval != 'f', err);
  462. return 0;
  463. err:
  464. cerr << "Skipping further runtime parameter changes" <<endl;
  465. delete ctx->runtime_params_str;
  466. ctx->runtime_params_str = NULL;
  467. return -1;
  468. }
  469. /**
  470. * Set encoder context defaults values.
  471. *
  472. * @param ctx : Encoder context
  473. */
  474. static void
  475. set_defaults(context_t * ctx)
  476. {
  477. memset(ctx, 0, sizeof(context_t));
  478. ctx->in_file_path = "/home/nvidia/Desktop/env_enc/jetson_multimedia_api/data/Video/output.yuv";
  479. ctx->out_file_path = "out.h264";
  480. ctx->width = 1280;
  481. ctx->height = 720;
  482. ctx->encoder_pixfmt= V4L2_PIX_FMT_H264;
  483. ctx->raw_pixfmt = V4L2_PIX_FMT_YUV420M;
  484. ctx->bitrate = 4 * 1024 * 1024;
  485. ctx->peak_bitrate = 0;
  486. ctx->profile = V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE;
  487. ctx->ratecontrol = V4L2_MPEG_VIDEO_BITRATE_MODE_CBR;
  488. ctx->iframe_interval = 30;
  489. ctx->externalRPS = false;
  490. ctx->enableGDR = false;
  491. ctx->enableROI = false;
  492. ctx->bnoIframe = false;
  493. ctx->bGapsInFrameNumAllowed = false;
  494. ctx->bReconCrc = false;
  495. ctx->enableLossless = false;
  496. ctx->nH264FrameNumBits = 0;
  497. ctx->nH265PocLsbBits = 0;
  498. ctx->idr_interval = 256;
  499. ctx->level = -1;
  500. ctx->fps_n = 30;
  501. ctx->fps_d = 1;
  502. ctx->gdr_start_frame_number = 0xffffffff;
  503. ctx->gdr_num_frames = 0xffffffff;
  504. ctx->gdr_out_frame_number = 0xffffffff;
  505. ctx->num_b_frames = (uint32_t) -1;
  506. ctx->nMinQpI = (uint32_t)QP_RETAIN_VAL;
  507. ctx->nMaxQpI = (uint32_t)QP_RETAIN_VAL;
  508. ctx->nMinQpP = (uint32_t)QP_RETAIN_VAL;
  509. ctx->nMaxQpP = (uint32_t)QP_RETAIN_VAL;
  510. ctx->nMinQpB = (uint32_t)QP_RETAIN_VAL;
  511. ctx->nMaxQpB = (uint32_t)QP_RETAIN_VAL;
  512. ctx->use_gold_crc = false;
  513. ctx->pBitStreamCrc = NULL;
  514. ctx->externalRCHints = false;
  515. ctx->input_metadata = false;
  516. ctx->sMaxQp = 51;
  517. ctx->stats = false;
  518. ctx->stress_test = 1;
  519. ctx->output_memory_type = V4L2_MEMORY_DMABUF;
  520. ctx->capture_memory_type = V4L2_MEMORY_MMAP;
  521. ctx->cs = V4L2_COLORSPACE_SMPTE170M;
  522. ctx->copy_timestamp = false;
  523. ctx->sar_width = 0;
  524. ctx->sar_height = 0;
  525. ctx->start_ts = 0;
  526. ctx->max_perf = 0;
  527. ctx->blocking_mode = 1;
  528. ctx->startf = 0;
  529. ctx->endf = 0;
  530. ctx->num_output_buffers = 6;
  531. ctx->num_frames_to_encode = -1;
  532. ctx->poc_type = 0;
  533. ctx->chroma_format_idc = -1;
  534. ctx->bit_depth = 8;
  535. ctx->is_semiplanar = false;
  536. ctx->enable_initQP = false;
  537. ctx->IinitQP = 0;
  538. ctx->PinitQP = 0;
  539. ctx->BinitQP = 0;
  540. ctx->enable_ratecontrol = true;
  541. ctx->enable_av1tile = false;
  542. ctx->log2_num_av1rows = 0;
  543. ctx->log2_num_av1cols = 0;
  544. ctx->enable_av1ssimrdo = (uint8_t)-1;
  545. ctx->disable_av1cdfupdate = (uint8_t)-1;
  546. ctx->ppe_init_params.enable_ppe = false;
  547. ctx->ppe_init_params.wait_time_ms = -1;
  548. ctx->ppe_init_params.feature_flags = V4L2_PPE_FEATURE_NONE;
  549. ctx->ppe_init_params.enable_profiler = 0;
  550. ctx->ppe_init_params.taq_max_qp_delta = 5;
  551. /* TAQ for B-frames is enabled by default */
  552. ctx->ppe_init_params.taq_b_frame_mode = 1;
  553. }
  554. /**
  555. * Populate the Region Of Interest(ROI) Parameters.
  556. *
  557. * @param stream : input stream
  558. * @param VEnc_ROI_params : encoder ROI Parameters
  559. */
  560. static void
  561. populate_roi_Param(std::ifstream * stream, v4l2_enc_frame_ROI_params *VEnc_ROI_params)
  562. {
  563. unsigned int ROIIndex = 0;
  564. if (!stream->eof()) {
  565. *stream >> VEnc_ROI_params->num_ROI_regions;
  566. while (ROIIndex < VEnc_ROI_params->num_ROI_regions)
  567. {
  568. if (ROIIndex == V4L2_MAX_ROI_REGIONS) {
  569. string skip_str;
  570. getline(*stream, skip_str);
  571. VEnc_ROI_params->num_ROI_regions = V4L2_MAX_ROI_REGIONS;
  572. cout << "Maximum of " << V4L2_MAX_ROI_REGIONS <<
  573. "regions can be applied for a frame" << endl;
  574. break;
  575. }
  576. /* Populate Region Of Interest(ROI) coordinates and qpdelta value */
  577. *stream >> VEnc_ROI_params->ROI_params[ROIIndex].QPdelta;
  578. *stream >> VEnc_ROI_params->ROI_params[ROIIndex].ROIRect.left;
  579. *stream >> VEnc_ROI_params->ROI_params[ROIIndex].ROIRect.top;
  580. *stream >> VEnc_ROI_params->ROI_params[ROIIndex].ROIRect.width;
  581. *stream >> VEnc_ROI_params->ROI_params[ROIIndex].ROIRect.height;
  582. ROIIndex++;
  583. }
  584. } else {
  585. cout << "EOF of ROI_param_file & rewind" << endl;
  586. stream->clear();
  587. stream->seekg(0);
  588. }
  589. }
  590. /**
  591. * Populate the External Reference Picture Set(RPS) Parameters.
  592. *
  593. * @param stream : input stream
  594. * @param VEnc_ext_rps_ctrl_params : encoder RPS Parameters
  595. */
  596. static void
  597. populate_ext_rps_ctrl_Param (std::ifstream * stream, v4l2_enc_frame_ext_rps_ctrl_params *VEnc_ext_rps_ctrl_params)
  598. {
  599. unsigned int RPSIndex = 0;
  600. unsigned int temp = 0;
  601. stream->peek();
  602. restart :
  603. if (stream->eof()) {
  604. cout << "EOF of rps_param_file & rewind" << endl;
  605. stream->clear();
  606. stream->seekg(0);
  607. }
  608. if (!stream->eof()) {
  609. /* Populate External Reference Picture Set(RPS) specific configuration */
  610. *stream >> VEnc_ext_rps_ctrl_params->nFrameId;
  611. if (stream->eof())
  612. goto restart;
  613. *stream >> temp;
  614. VEnc_ext_rps_ctrl_params->bRefFrame = ((temp)?true:false);
  615. *stream >> temp;
  616. VEnc_ext_rps_ctrl_params->bLTRefFrame = ((temp)?true:false);
  617. *stream >> VEnc_ext_rps_ctrl_params->nMaxRefFrames;
  618. *stream >> VEnc_ext_rps_ctrl_params->nActiveRefFrames;
  619. *stream >> VEnc_ext_rps_ctrl_params->nCurrentRefFrameId;
  620. while (RPSIndex < VEnc_ext_rps_ctrl_params->nActiveRefFrames)
  621. {
  622. if (RPSIndex == V4L2_MAX_REF_FRAMES) {
  623. string skip_str;
  624. getline(*stream, skip_str);
  625. VEnc_ext_rps_ctrl_params->nActiveRefFrames = V4L2_MAX_REF_FRAMES;
  626. cout << "Maximum of " << V4L2_MAX_REF_FRAMES <<
  627. "reference frames are valid" << endl;
  628. break;
  629. }
  630. *stream >> VEnc_ext_rps_ctrl_params->RPSList[RPSIndex].nFrameId;
  631. *stream >> temp;
  632. VEnc_ext_rps_ctrl_params->RPSList[RPSIndex].bLTRefFrame = ((temp)?true:false);
  633. RPSIndex++;
  634. }
  635. }
  636. }
  637. static void
  638. populate_ext_rps_threeLayerSvc_Param (context_t *ctx, v4l2_enc_frame_ext_rps_ctrl_params *VEnc_ext_rps_ctrl_params)
  639. {
  640. uint32_t RPSIndex = 0;
  641. uint32_t temporalCycle;
  642. uint32_t temporalID;
  643. int32_t previousTemporal0Id;
  644. bool bRefFrame;
  645. uint32_t nCurrentRefFrameId;
  646. uint32_t nFrameId = ctx->input_frames_queued_count;
  647. temporalCycle = (1<<(ctx->rps_par.m_numTemperalLayers-1));
  648. temporalID = (nFrameId % 2);
  649. if (nFrameId % temporalCycle == 0)
  650. {
  651. previousTemporal0Id = nFrameId - temporalCycle;
  652. if (previousTemporal0Id < 0)
  653. {
  654. previousTemporal0Id = 0;
  655. }
  656. }
  657. else
  658. {
  659. previousTemporal0Id = (nFrameId>>2)<<2;
  660. }
  661. if (ctx->rps_par.m_numTemperalLayers > 2)
  662. {
  663. temporalID += (nFrameId % temporalCycle) ? 1 : 0;
  664. }
  665. bRefFrame = (bool)(temporalID != ctx->rps_par.m_numTemperalLayers - 1);
  666. nCurrentRefFrameId = (temporalID == (ctx->rps_par.m_numTemperalLayers - 1)? nFrameId - 1 : previousTemporal0Id);
  667. VEnc_ext_rps_ctrl_params->nFrameId = nFrameId;
  668. VEnc_ext_rps_ctrl_params->bRefFrame = bRefFrame;
  669. VEnc_ext_rps_ctrl_params->bLTRefFrame = false;
  670. VEnc_ext_rps_ctrl_params->nMaxRefFrames = ctx->num_reference_frames;
  671. VEnc_ext_rps_ctrl_params->nCurrentRefFrameId = nCurrentRefFrameId;
  672. VEnc_ext_rps_ctrl_params->nActiveRefFrames =
  673. ctx->rps_par.nActiveRefFrames;
  674. cout << "[Input]nFrameId:" << nFrameId << ",";
  675. cout << "bRefFrame:" << bRefFrame << ",";
  676. cout << "nMaxRefFrames:" << ctx->num_reference_frames << ",";
  677. cout << "nCurrentRefFrameId:" << nCurrentRefFrameId << endl;
  678. while (RPSIndex < VEnc_ext_rps_ctrl_params->nActiveRefFrames)
  679. {
  680. VEnc_ext_rps_ctrl_params->RPSList[RPSIndex].nFrameId =
  681. ctx->rps_par.rps_list[RPSIndex].nFrameId;
  682. VEnc_ext_rps_ctrl_params->RPSList[RPSIndex].bLTRefFrame =
  683. ctx->rps_par.rps_list[RPSIndex].bLTRefFrame;
  684. RPSIndex++;
  685. }
  686. }
  687. /**
  688. * Setup output plane for DMABUF io-mode.
  689. *
  690. * @param ctx : encoder context
  691. * @param num_buffers : request buffer count
  692. */
  693. static int
  694. setup_output_dmabuf(context_t *ctx, uint32_t num_buffers )
  695. {
  696. int ret=0;
  697. NvBufSurf::NvCommonAllocateParams cParams;
  698. int fd;
  699. ret = ctx->enc->output_plane.reqbufs(V4L2_MEMORY_DMABUF,num_buffers);
  700. if(ret)
  701. {
  702. cerr << "reqbufs failed for output plane V4L2_MEMORY_DMABUF" << endl;
  703. return ret;
  704. }
  705. for (uint32_t i = 0; i < ctx->enc->output_plane.getNumBuffers(); i++)
  706. {
  707. cParams.width = ctx->width;
  708. cParams.height = ctx->height;
  709. cParams.layout = NVBUF_LAYOUT_PITCH;
  710. switch (ctx->cs)
  711. {
  712. case V4L2_COLORSPACE_REC709:
  713. cParams.colorFormat = ctx->enable_extended_colorformat ?
  714. NVBUF_COLOR_FORMAT_YUV420_709_ER : NVBUF_COLOR_FORMAT_YUV420_709;
  715. break;
  716. case V4L2_COLORSPACE_SMPTE170M:
  717. default:
  718. cParams.colorFormat = ctx->enable_extended_colorformat ?
  719. NVBUF_COLOR_FORMAT_YUV420_ER : NVBUF_COLOR_FORMAT_YUV420;
  720. }
  721. if (ctx->is_semiplanar)
  722. {
  723. cParams.colorFormat = NVBUF_COLOR_FORMAT_NV12;
  724. }
  725. if (ctx->encoder_pixfmt == V4L2_PIX_FMT_H264)
  726. {
  727. if (ctx->enableLossless)
  728. {
  729. if (ctx->is_semiplanar)
  730. cParams.colorFormat = NVBUF_COLOR_FORMAT_NV24;
  731. else
  732. cParams.colorFormat = NVBUF_COLOR_FORMAT_YUV444;
  733. }
  734. }
  735. else if (ctx->encoder_pixfmt == V4L2_PIX_FMT_H265)
  736. {
  737. if (ctx->chroma_format_idc == 3)
  738. {
  739. if (ctx->is_semiplanar)
  740. cParams.colorFormat = NVBUF_COLOR_FORMAT_NV24;
  741. else
  742. cParams.colorFormat = NVBUF_COLOR_FORMAT_YUV444;
  743. if (ctx->bit_depth == 10)
  744. cParams.colorFormat = NVBUF_COLOR_FORMAT_NV24_10LE;
  745. }
  746. if (ctx->profile == V4L2_MPEG_VIDEO_H265_PROFILE_MAIN10 && (ctx->bit_depth == 10))
  747. {
  748. cParams.colorFormat = NVBUF_COLOR_FORMAT_NV12_10LE;
  749. }
  750. }
  751. cParams.memtag = NvBufSurfaceTag_VIDEO_ENC;
  752. cParams.memType = NVBUF_MEM_SURFACE_ARRAY;
  753. /* Create output plane fd for DMABUF io-mode */
  754. ret = NvBufSurf::NvAllocate(&cParams, 1, &fd);
  755. if(ret < 0)
  756. {
  757. cerr << "Failed to create NvBuffer" << endl;
  758. return ret;
  759. }
  760. ctx->output_plane_fd[i]=fd;
  761. }
  762. return ret;
  763. }
  764. static int
  765. setup_capture_dmabuf(context_t *ctx, uint32_t num_buffers )
  766. {
  767. NvBufSurfaceAllocateParams cParams = {{0}};
  768. NvBufSurface *surface = 0;
  769. int ret=0;
  770. ret = ctx->enc->capture_plane.reqbufs(V4L2_MEMORY_DMABUF,num_buffers);
  771. if(ret)
  772. {
  773. cerr << "reqbufs failed for capture plane V4L2_MEMORY_DMABUF" << endl;
  774. return ret;
  775. }
  776. for (uint32_t i = 0; i < ctx->enc->capture_plane.getNumBuffers(); i++)
  777. {
  778. ret = ctx->enc->capture_plane.queryBuffer(i);
  779. if (ret)
  780. {
  781. cerr << "Error in querying for " << i << "th buffer plane" << endl;
  782. return ret;
  783. }
  784. NvBuffer *buffer = ctx->enc->capture_plane.getNthBuffer(i);
  785. cParams.params.memType = NVBUF_MEM_HANDLE;
  786. cParams.params.size = buffer->planes[0].length;
  787. cParams.memtag = NvBufSurfaceTag_VIDEO_ENC;
  788. ret = NvBufSurfaceAllocate(&surface, 1, &cParams);
  789. if(ret < 0)
  790. {
  791. cerr << "Failed to create NvBuffer" << endl;
  792. return ret;
  793. }
  794. surface->numFilled = 1;
  795. ctx->capture_plane_fd[i] = surface->surfaceList[0].bufferDesc;
  796. }
  797. return ret;
  798. }
  799. /**
  800. * Populate the External Rate Control(RC) Parameters.
  801. *
  802. * @param stream : input stream
  803. * @param VEnc_ext_rate_ctrl_params : encoder external RC Parameters
  804. */
  805. static void
  806. populate_ext_rate_ctrl_Param(std::ifstream * stream, v4l2_enc_frame_ext_rate_ctrl_params *VEnc_ext_rate_ctrl_params)
  807. {
  808. stream->peek();
  809. restart:
  810. if (stream->eof()) {
  811. cout << "EOF of hints_param_file & rewind" << endl;
  812. stream->clear();
  813. stream->seekg(0);
  814. }
  815. if (!stream->eof()) {
  816. /* Populate External Rate Control specific configuration */
  817. *stream >> VEnc_ext_rate_ctrl_params->nTargetFrameBits;
  818. if (stream->eof())
  819. goto restart;
  820. *stream >> VEnc_ext_rate_ctrl_params->nFrameQP;
  821. *stream >> VEnc_ext_rate_ctrl_params->nFrameMinQp;
  822. *stream >> VEnc_ext_rate_ctrl_params->nFrameMaxQp;
  823. *stream >> VEnc_ext_rate_ctrl_params->nMaxQPDeviation;
  824. }
  825. }
  826. /**
  827. * Populate the Gradual Decoder Refresh(GDR) Parameters.
  828. *
  829. * @param stream : input stream
  830. * @param start_frame_num : start frame number
  831. * @param gdr_num_frames : GDR frame number
  832. */
  833. static void
  834. populate_gdr_Param(std::ifstream * stream, uint32_t *start_frame_num, uint32_t *gdr_num_frames)
  835. {
  836. if (stream->eof()) {
  837. *start_frame_num = 0xFFFFFFFF;
  838. cout << "GDR param EoF reached \n";
  839. }
  840. if (!stream->eof()) {
  841. *stream >> *start_frame_num;
  842. *stream >> *gdr_num_frames;
  843. }
  844. }
  845. /**
  846. * Encoder polling thread loop function.
  847. *
  848. * @param args : void arguments
  849. */
  850. static void *encoder_pollthread_fcn(void *arg)
  851. {
  852. context_t *ctx = (context_t *) arg;
  853. v4l2_ctrl_video_device_poll devicepoll;
  854. cout << "Starting Device Poll Thread " << endl;
  855. memset(&devicepoll, 0, sizeof(v4l2_ctrl_video_device_poll));
  856. /* wait here until signalled to issue the Poll call.
  857. Check if the abort status is set , if so exit
  858. Else issue the Poll on the encoder and block.
  859. When the Poll returns, signal the encoder thread to continue. */
  860. while (!ctx->got_error && !ctx->enc->isInError())
  861. {
  862. sem_wait(&ctx->pollthread_sema);
  863. if (ctx->got_eos)
  864. {
  865. cout << "Got eos, exiting poll thread \n";
  866. return NULL;
  867. }
  868. devicepoll.req_events = POLLIN | POLLOUT | POLLERR | POLLPRI;
  869. /* This call shall wait in the v4l2 encoder library */
  870. ctx->enc->DevicePoll(&devicepoll);
  871. /* Can check the devicepoll.resp_events bitmask to see which events are set. */
  872. sem_post(&ctx->encoderthread_sema);
  873. }
  874. return NULL;
  875. }
  876. /**
  877. * Encode processing function for non-blocking mode.
  878. *
  879. * @param ctx : Encoder context
  880. * @param eos : end of stream
  881. */
  882. static int encoder_proc_nonblocking(context_t &ctx, bool eos)
  883. {
  884. /* NOTE: In non-blocking mode, we will have this function do below things:
  885. 1) Issue signal to PollThread so it starts Poll and wait until signalled.
  886. 2) After we are signalled, it means there is something to dequeue,
  887. either output plane or capture plane or there's an event.
  888. 3) Try dequeuing from all three and then act appropriately.
  889. 4) After enqueuing go back to the same loop. */
  890. /* Since all the output plane buffers have been queued, we first need to
  891. dequeue a buffer from output plane before we can read new data into it
  892. and queue it again. */
  893. int ret = 0;
  894. while (!ctx.got_error && !ctx.enc->isInError())
  895. {
  896. /* Call SetPollInterrupt */
  897. ctx.enc->SetPollInterrupt();
  898. /* Since buffers have been queued, issue a post to start polling and
  899. then wait here */
  900. sem_post(&ctx.pollthread_sema);
  901. sem_wait(&ctx.encoderthread_sema);
  902. /* Already end of file, no more queue-dequeue for output plane */
  903. if (eos)
  904. goto check_capture_buffers;
  905. /* Check if can dequeue from output plane */
  906. while (1)
  907. {
  908. struct v4l2_buffer v4l2_output_buf;
  909. struct v4l2_plane output_planes[MAX_PLANES];
  910. NvBuffer *outplane_buffer = NULL;
  911. memset(&v4l2_output_buf, 0, sizeof(v4l2_output_buf));
  912. memset(output_planes, 0, sizeof(output_planes));
  913. v4l2_output_buf.m.planes = output_planes;
  914. /* Dequeue from output plane, fill the frame and enqueue it back again.
  915. NOTE: This could be moved out to a different thread as an optimization. */
  916. ret = ctx.enc->output_plane.dqBuffer(v4l2_output_buf, &outplane_buffer, NULL, 10);
  917. if (ret < 0)
  918. {
  919. if (errno == EAGAIN)
  920. {
  921. goto check_capture_buffers;
  922. }
  923. cerr << "ERROR while DQing buffer at output plane" << endl;
  924. abort(&ctx);
  925. return -1;
  926. }
  927. /* Get the parsed encoder runtime parameters */
  928. if (ctx.runtime_params_str &&
  929. (ctx.enc->output_plane.getTotalQueuedBuffers() ==
  930. ctx.next_param_change_frame))
  931. {
  932. set_runtime_params(&ctx);
  933. if (ctx.runtime_params_str)
  934. get_next_runtime_param_change_frame(&ctx);
  935. }
  936. /* Read yuv frame data from input file */
  937. if (read_video_frame(ctx.in_file, *outplane_buffer) < 0 || ctx.num_frames_to_encode == 0)
  938. {
  939. cerr << "Could not read complete frame from input file" << endl;
  940. v4l2_output_buf.m.planes[0].bytesused = 0;
  941. if(ctx.b_use_enc_cmd)
  942. {
  943. ret = ctx.enc->setEncoderCommand(V4L2_ENC_CMD_STOP, 1);
  944. eos = true;
  945. break;
  946. }
  947. else
  948. {
  949. eos = true;
  950. v4l2_output_buf.m.planes[0].m.userptr = 0;
  951. v4l2_output_buf.m.planes[0].bytesused = 0;
  952. v4l2_output_buf.m.planes[1].bytesused = 0;
  953. v4l2_output_buf.m.planes[2].bytesused = 0;
  954. }
  955. }
  956. /* Encoder supported input metadata specific configurations */
  957. if (ctx.input_metadata)
  958. {
  959. v4l2_ctrl_videoenc_input_metadata VEnc_imeta_param;
  960. v4l2_enc_frame_ROI_params VEnc_ROI_params;
  961. v4l2_enc_frame_ReconCRC_params VEnc_ReconCRC_params;
  962. v4l2_enc_frame_ext_rps_ctrl_params VEnc_ext_rps_ctrl_params;
  963. v4l2_enc_frame_ext_rate_ctrl_params VEnc_ext_rate_ctrl_params;
  964. v4l2_enc_gdr_params VEnc_gdr_params;
  965. VEnc_imeta_param.flag = 0;
  966. if (ctx.ROI_Param_file_path)
  967. {
  968. if (ctx.enableROI) {
  969. VEnc_imeta_param.flag |= V4L2_ENC_INPUT_ROI_PARAM_FLAG;
  970. VEnc_imeta_param.VideoEncROIParams = &VEnc_ROI_params;
  971. /* Update Region of Intrest parameters from ROI params file */
  972. populate_roi_Param(ctx.roi_Param_file, VEnc_imeta_param.VideoEncROIParams);
  973. }
  974. }
  975. if (ctx.bReconCrc)
  976. {
  977. VEnc_imeta_param.flag |= V4L2_ENC_INPUT_RECONCRC_PARAM_FLAG;
  978. VEnc_ReconCRC_params.ReconCRCRect.left = ctx.rl;
  979. VEnc_ReconCRC_params.ReconCRCRect.top = ctx.rt;
  980. VEnc_ReconCRC_params.ReconCRCRect.width = ctx.rw;
  981. VEnc_ReconCRC_params.ReconCRCRect.height = ctx.rh;
  982. /* Update reconstructed CRC Parameters */
  983. VEnc_imeta_param.VideoReconCRCParams = &VEnc_ReconCRC_params;
  984. }
  985. if (ctx.RPS_Param_file_path)
  986. {
  987. if (ctx.externalRPS) {
  988. VEnc_imeta_param.flag |= V4L2_ENC_INPUT_RPS_PARAM_FLAG;
  989. VEnc_imeta_param.VideoEncRPSParams = &VEnc_ext_rps_ctrl_params;
  990. /* Update external reference picture set parameters from RPS params file */
  991. populate_ext_rps_ctrl_Param(ctx.rps_Param_file, VEnc_imeta_param.VideoEncRPSParams);
  992. }
  993. }
  994. if (ctx.GDR_Param_file_path)
  995. {
  996. if (ctx.enableGDR)
  997. {
  998. /* Update GDR parameters from GDR params file */
  999. if (ctx.gdr_start_frame_number == 0xFFFFFFFF)
  1000. populate_gdr_Param(ctx.gdr_Param_file, &ctx.gdr_start_frame_number,
  1001. &ctx.gdr_num_frames);
  1002. if (ctx.input_frames_queued_count == ctx.gdr_start_frame_number)
  1003. {
  1004. ctx.gdr_out_frame_number = ctx.gdr_start_frame_number;
  1005. VEnc_gdr_params.nGDRFrames = ctx.gdr_num_frames;
  1006. VEnc_imeta_param.flag |= V4L2_ENC_INPUT_GDR_PARAM_FLAG;
  1007. VEnc_imeta_param.VideoEncGDRParams = &VEnc_gdr_params;
  1008. }
  1009. }
  1010. }
  1011. if (ctx.hints_Param_file_path)
  1012. {
  1013. if (ctx.externalRCHints) {
  1014. VEnc_imeta_param.flag |= V4L2_ENC_INPUT_RC_PARAM_FLAG;
  1015. VEnc_imeta_param.VideoEncExtRCParams = &VEnc_ext_rate_ctrl_params;
  1016. /* Update external rate control parameters from hints params file */
  1017. populate_ext_rate_ctrl_Param(ctx.hints_Param_file, VEnc_imeta_param.VideoEncExtRCParams);
  1018. }
  1019. }
  1020. if (VEnc_imeta_param.flag)
  1021. {
  1022. /* Set encoder input metadatas */
  1023. ctx.enc->SetInputMetaParams(v4l2_output_buf.index, VEnc_imeta_param);
  1024. v4l2_output_buf.reserved2 = v4l2_output_buf.index;
  1025. }
  1026. }
  1027. if (ctx.copy_timestamp)
  1028. {
  1029. /* Set user provided timestamp when copy timestamp is enabled */
  1030. v4l2_output_buf.flags |= V4L2_BUF_FLAG_TIMESTAMP_COPY;
  1031. ctx.timestamp += ctx.timestampincr;
  1032. v4l2_output_buf.timestamp.tv_sec = ctx.timestamp / (MICROSECOND_UNIT);
  1033. v4l2_output_buf.timestamp.tv_usec = ctx.timestamp % (MICROSECOND_UNIT);
  1034. }
  1035. if(!ctx.num_frames_to_encode)
  1036. {
  1037. outplane_buffer->planes[0].bytesused = outplane_buffer->planes[1].bytesused = outplane_buffer->planes[2].bytesused = 0;
  1038. }
  1039. if(ctx.output_memory_type == V4L2_MEMORY_DMABUF || ctx.output_memory_type == V4L2_MEMORY_MMAP)
  1040. {
  1041. for (uint32_t j = 0 ; j < outplane_buffer->n_planes; j++)
  1042. {
  1043. NvBufSurface *nvbuf_surf = 0;
  1044. ret = NvBufSurfaceFromFd (outplane_buffer->planes[j].fd, (void**)(&nvbuf_surf));
  1045. if (ret < 0)
  1046. {
  1047. cerr << "Error while NvBufSurfaceFromFd" << endl;
  1048. abort(&ctx);
  1049. return -1;
  1050. }
  1051. ret = NvBufSurfaceSyncForDevice (nvbuf_surf, 0, j);
  1052. if (ret < 0)
  1053. {
  1054. cerr << "Error while NvBufSurfaceSyncForDevice at output plane for V4L2_MEMORY_DMABUF" << endl;
  1055. abort(&ctx);
  1056. return -1;
  1057. }
  1058. }
  1059. }
  1060. if(ctx.output_memory_type == V4L2_MEMORY_DMABUF)
  1061. {
  1062. for (uint32_t j = 0 ; j < outplane_buffer->n_planes; j++)
  1063. {
  1064. v4l2_output_buf.m.planes[j].bytesused = outplane_buffer->planes[j].bytesused;
  1065. }
  1066. }
  1067. /* encoder qbuffer for output plane */
  1068. ret = ctx.enc->output_plane.qBuffer(v4l2_output_buf, NULL);
  1069. if (ret < 0)
  1070. {
  1071. cerr << "Error while queueing buffer at output plane" << endl;
  1072. abort(&ctx);
  1073. return -1;
  1074. }
  1075. if(ctx.num_frames_to_encode > 0)
  1076. {
  1077. ctx.num_frames_to_encode--;
  1078. }
  1079. ctx.input_frames_queued_count++;
  1080. if (v4l2_output_buf.m.planes[0].bytesused == 0)
  1081. {
  1082. cerr << "File read complete." << endl;
  1083. eos = true;
  1084. goto check_capture_buffers;
  1085. }
  1086. }
  1087. check_capture_buffers:
  1088. while (1)
  1089. {
  1090. struct v4l2_buffer v4l2_capture_buf;
  1091. struct v4l2_plane capture_planes[MAX_PLANES];
  1092. NvBuffer *capplane_buffer = NULL;
  1093. bool capture_dq_continue = true;
  1094. memset(&v4l2_capture_buf, 0, sizeof(v4l2_capture_buf));
  1095. memset(capture_planes, 0, sizeof(capture_planes));
  1096. v4l2_capture_buf.m.planes = capture_planes;
  1097. v4l2_capture_buf.length = 1;
  1098. /* Dequeue from output plane, fill the frame and enqueue it back again.
  1099. NOTE: This could be moved out to a different thread as an optimization. */
  1100. ret = ctx.enc->capture_plane.dqBuffer(v4l2_capture_buf, &capplane_buffer, NULL, 10);
  1101. if (ret < 0)
  1102. {
  1103. if (errno == EAGAIN)
  1104. break;
  1105. cerr << "ERROR while DQing buffer at capture plane" << endl;
  1106. abort(&ctx);
  1107. return -1;
  1108. }
  1109. /* Invoke encoder capture-plane deque buffer callback */
  1110. capture_dq_continue = encoder_capture_plane_dq_callback(&v4l2_capture_buf, capplane_buffer, NULL,
  1111. &ctx);
  1112. if (!capture_dq_continue)
  1113. {
  1114. cout << "Capture plane dequeued 0 size buffer " << endl;
  1115. ctx.got_eos = true;
  1116. return 0;
  1117. }
  1118. }
  1119. }
  1120. return 0;
  1121. }
  1122. /**
  1123. * Encode processing function for blocking mode.
  1124. *
  1125. * @param ctx : Encoder context
  1126. * @param eos : end of stream
  1127. */
  1128. static int encoder_proc_blocking(context_t &ctx, bool eos)
  1129. {
  1130. int ret = 0;
  1131. /* Read video frame and queue all the output plane buffers. */
  1132. for (uint32_t i = 0; i < ctx.enc->output_plane.getNumBuffers(); i++)
  1133. {
  1134. struct v4l2_buffer v4l2_buf;
  1135. struct v4l2_plane planes[MAX_PLANES];
  1136. NvBuffer *buffer = ctx.enc->output_plane.getNthBuffer(i);
  1137. memset(&v4l2_buf, 0, sizeof(v4l2_buf));
  1138. memset(planes, 0, MAX_PLANES * sizeof(struct v4l2_plane));
  1139. v4l2_buf.index = i;
  1140. v4l2_buf.m.planes = planes;
  1141. if(ctx.output_memory_type == V4L2_MEMORY_DMABUF)
  1142. {
  1143. v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
  1144. v4l2_buf.memory = V4L2_MEMORY_DMABUF;
  1145. /* Map output plane buffer for memory type DMABUF. */
  1146. ret = ctx.enc->output_plane.mapOutputBuffers(v4l2_buf, ctx.output_plane_fd[i]);
  1147. if (ret < 0)
  1148. {
  1149. cerr << "Error while mapping buffer at output plane" << endl;
  1150. abort(&ctx);
  1151. goto cleanup;
  1152. }
  1153. }
  1154. if(ctx.startf)
  1155. {
  1156. uint32_t i = 0, frame_size = 0;
  1157. for (i = 0; i < buffer->n_planes; i++)
  1158. {
  1159. frame_size += buffer->planes[i].fmt.bytesperpixel * buffer->planes[i].fmt.width * buffer->planes[i].fmt.height;
  1160. }
  1161. frame_size = frame_size * ctx.startf;
  1162. ctx.in_file->seekg (frame_size, std::ios::cur);
  1163. ctx.startf = 0;
  1164. }
  1165. /* Read yuv frame data from input file */
  1166. if (read_video_frame(ctx.in_file, *buffer) < 0 || ctx.num_frames_to_encode == 0)
  1167. {
  1168. cerr << "Could not read complete frame from input file" << endl;
  1169. v4l2_buf.m.planes[0].bytesused = 0;
  1170. if(ctx.b_use_enc_cmd)
  1171. {
  1172. /* Send v4l2 command for encoder stop */
  1173. ret = ctx.enc->setEncoderCommand(V4L2_ENC_CMD_STOP, 1);
  1174. eos = true;
  1175. break;
  1176. }
  1177. else
  1178. {
  1179. eos = true;
  1180. v4l2_buf.m.planes[0].m.userptr = 0;
  1181. v4l2_buf.m.planes[0].bytesused = v4l2_buf.m.planes[1].bytesused = v4l2_buf.m.planes[2].bytesused = 0;
  1182. }
  1183. }
  1184. if (ctx.runtime_params_str &&
  1185. (ctx.enc->output_plane.getTotalQueuedBuffers() ==
  1186. ctx.next_param_change_frame))
  1187. {
  1188. /* Set runtime configuration parameters */
  1189. set_runtime_params(&ctx);
  1190. if (ctx.runtime_params_str)
  1191. get_next_runtime_param_change_frame(&ctx);
  1192. }
  1193. /* Encoder supported input metadata specific configurations */
  1194. if (ctx.input_metadata)
  1195. {
  1196. v4l2_ctrl_videoenc_input_metadata VEnc_imeta_param;
  1197. v4l2_enc_frame_ROI_params VEnc_ROI_params;
  1198. v4l2_enc_frame_ReconCRC_params VEnc_ReconCRC_params;
  1199. v4l2_enc_frame_ext_rps_ctrl_params VEnc_ext_rps_ctrl_params;
  1200. v4l2_enc_frame_ext_rate_ctrl_params VEnc_ext_rate_ctrl_params;
  1201. v4l2_enc_gdr_params VEnc_gdr_params;
  1202. VEnc_imeta_param.flag = 0;
  1203. if (ctx.ROI_Param_file_path)
  1204. {
  1205. if (ctx.enableROI) {
  1206. VEnc_imeta_param.flag |= V4L2_ENC_INPUT_ROI_PARAM_FLAG;
  1207. VEnc_imeta_param.VideoEncROIParams = &VEnc_ROI_params;
  1208. /* Update Region of Intrest parameters from ROI params file */
  1209. populate_roi_Param(ctx.roi_Param_file, VEnc_imeta_param.VideoEncROIParams);
  1210. }
  1211. }
  1212. if (ctx.bReconCrc)
  1213. {
  1214. VEnc_imeta_param.flag |= V4L2_ENC_INPUT_RECONCRC_PARAM_FLAG;
  1215. VEnc_ReconCRC_params.ReconCRCRect.left = ctx.rl;
  1216. VEnc_ReconCRC_params.ReconCRCRect.top = ctx.rt;
  1217. VEnc_ReconCRC_params.ReconCRCRect.width = ctx.rw;
  1218. VEnc_ReconCRC_params.ReconCRCRect.height = ctx.rh;
  1219. /* Update reconstructed CRC Parameters */
  1220. VEnc_imeta_param.VideoReconCRCParams = &VEnc_ReconCRC_params;
  1221. }
  1222. if (ctx.externalRPS)
  1223. {
  1224. if (ctx.RPS_threeLayerSvc)
  1225. {
  1226. if (ctx.input_frames_queued_count > 0)
  1227. {
  1228. sem_wait(&ctx.rps_par.sema);
  1229. }
  1230. VEnc_imeta_param.flag |= V4L2_ENC_INPUT_RPS_PARAM_FLAG;
  1231. VEnc_imeta_param.VideoEncRPSParams = &VEnc_ext_rps_ctrl_params;
  1232. populate_ext_rps_threeLayerSvc_Param(&ctx, VEnc_imeta_param.VideoEncRPSParams);
  1233. }
  1234. else if (ctx.RPS_Param_file_path)
  1235. {
  1236. VEnc_imeta_param.flag |= V4L2_ENC_INPUT_RPS_PARAM_FLAG;
  1237. VEnc_imeta_param.VideoEncRPSParams = &VEnc_ext_rps_ctrl_params;
  1238. /* Update external reference picture set parameters from RPS params file */
  1239. populate_ext_rps_ctrl_Param(ctx.rps_Param_file, VEnc_imeta_param.VideoEncRPSParams);
  1240. }
  1241. }
  1242. // if (ctx.GDR_Param_file_path)
  1243. // {
  1244. // printf ('00000000000000000000000000000000000000000ooooooooooooooooooooooooooooooo\n');
  1245. // if (ctx.enableGDR)
  1246. // {
  1247. // /* Update GDR parameters from GDR params file */
  1248. // if (ctx.gdr_start_frame_number == 0xFFFFFFFF)
  1249. // populate_gdr_Param(ctx.gdr_Param_file, &ctx.gdr_start_frame_number,
  1250. // &ctx.gdr_num_frames);
  1251. // if (ctx.input_frames_queued_count == ctx.gdr_start_frame_number)
  1252. // {
  1253. // ctx.gdr_out_frame_number = ctx.gdr_start_frame_number;
  1254. // VEnc_gdr_params.nGDRFrames = ctx.gdr_num_frames;
  1255. // VEnc_imeta_param.flag |= V4L2_ENC_INPUT_GDR_PARAM_FLAG;
  1256. // VEnc_imeta_param.VideoEncGDRParams = &VEnc_gdr_params;
  1257. // }
  1258. // }
  1259. // }
  1260. if (ctx.hints_Param_file_path)
  1261. {
  1262. if (ctx.externalRCHints) {
  1263. VEnc_imeta_param.flag |= V4L2_ENC_INPUT_RC_PARAM_FLAG;
  1264. VEnc_imeta_param.VideoEncExtRCParams = &VEnc_ext_rate_ctrl_params;
  1265. /* Update external rate control parameters from hints params file */
  1266. populate_ext_rate_ctrl_Param(ctx.hints_Param_file, VEnc_imeta_param.VideoEncExtRCParams);
  1267. }
  1268. }
  1269. if (VEnc_imeta_param.flag)
  1270. {
  1271. /* Set encoder input metadatas */
  1272. ctx.enc->SetInputMetaParams(v4l2_buf.index, VEnc_imeta_param);
  1273. v4l2_buf.reserved2 = v4l2_buf.index;
  1274. }
  1275. }
  1276. if (ctx.copy_timestamp)
  1277. {
  1278. v4l2_buf.flags |= V4L2_BUF_FLAG_TIMESTAMP_COPY;
  1279. ctx.timestamp += ctx.timestampincr;
  1280. v4l2_buf.timestamp.tv_sec = ctx.timestamp / (MICROSECOND_UNIT);
  1281. v4l2_buf.timestamp.tv_usec = ctx.timestamp % (MICROSECOND_UNIT);
  1282. }
  1283. if(ctx.output_memory_type == V4L2_MEMORY_DMABUF || ctx.output_memory_type == V4L2_MEMORY_MMAP)
  1284. {
  1285. for (uint32_t j = 0 ; j < buffer->n_planes; j++)
  1286. {
  1287. NvBufSurface *nvbuf_surf = 0;
  1288. ret = NvBufSurfaceFromFd (buffer->planes[j].fd, (void**)(&nvbuf_surf));
  1289. if (ret < 0)
  1290. {
  1291. cerr << "Error while NvBufSurfaceFromFd" << endl;
  1292. abort(&ctx);
  1293. goto cleanup;
  1294. }
  1295. ret = NvBufSurfaceSyncForDevice (nvbuf_surf, 0, j);
  1296. if (ret < 0)
  1297. {
  1298. cerr << "Error while NvBufSurfaceSyncForDevice at output plane for V4L2_MEMORY_DMABUF" << endl;
  1299. abort(&ctx);
  1300. goto cleanup;
  1301. }
  1302. }
  1303. }
  1304. if(ctx.output_memory_type == V4L2_MEMORY_DMABUF)
  1305. {
  1306. for (uint32_t j = 0 ; j < buffer->n_planes ; j++)
  1307. {
  1308. v4l2_buf.m.planes[j].bytesused = buffer->planes[j].bytesused;
  1309. }
  1310. }
  1311. /* encoder qbuffer for output plane */
  1312. ret = ctx.enc->output_plane.qBuffer(v4l2_buf, NULL);
  1313. if (ret < 0)
  1314. {
  1315. cerr << "Error while queueing buffer at output plane" << endl;
  1316. abort(&ctx);
  1317. goto cleanup;
  1318. }
  1319. if(ctx.num_frames_to_encode > 0)
  1320. {
  1321. ctx.num_frames_to_encode --;
  1322. }
  1323. if (v4l2_buf.m.planes[0].bytesused == 0)
  1324. {
  1325. cerr << "File read complete." << endl;
  1326. eos = true;
  1327. break;
  1328. }
  1329. ctx.input_frames_queued_count++;
  1330. }
  1331. /* Keep reading input till EOS is reached */
  1332. while (!ctx.got_error && !ctx.enc->isInError() && !eos)
  1333. {
  1334. struct v4l2_buffer v4l2_buf;
  1335. struct v4l2_plane planes[MAX_PLANES];
  1336. NvBuffer *buffer;
  1337. memset(&v4l2_buf, 0, sizeof(v4l2_buf));
  1338. memset(planes, 0, sizeof(planes));
  1339. v4l2_buf.m.planes = planes;
  1340. /* dequeue buffer from encoder output plane */
  1341. if (ctx.enc->output_plane.dqBuffer(v4l2_buf, &buffer, NULL, 10) < 0)
  1342. {
  1343. cerr << "ERROR while DQing buffer at output plane" << endl;
  1344. abort(&ctx);
  1345. goto cleanup;
  1346. }
  1347. /* Get the parsed encoder runtime parameters */
  1348. if (ctx.runtime_params_str &&
  1349. (ctx.enc->output_plane.getTotalQueuedBuffers() ==
  1350. ctx.next_param_change_frame))
  1351. {
  1352. set_runtime_params(&ctx);
  1353. if (ctx.runtime_params_str)
  1354. get_next_runtime_param_change_frame(&ctx);
  1355. }
  1356. /* Read yuv frame data from input file */
  1357. if (read_video_frame(ctx.in_file, *buffer) < 0 || ctx.num_frames_to_encode == 0)
  1358. {
  1359. cerr << "Could not read complete frame from input file" << endl;
  1360. v4l2_buf.m.planes[0].bytesused = 0;
  1361. if(ctx.b_use_enc_cmd)
  1362. {
  1363. ret = ctx.enc->setEncoderCommand(V4L2_ENC_CMD_STOP, 1);
  1364. eos = true;
  1365. ctx.got_eos = true;
  1366. break;
  1367. }
  1368. else
  1369. {
  1370. eos = true;
  1371. ctx.got_eos = true;
  1372. v4l2_buf.m.planes[0].m.userptr = 0;
  1373. v4l2_buf.m.planes[0].bytesused = v4l2_buf.m.planes[1].bytesused = v4l2_buf.m.planes[2].bytesused = 0;
  1374. }
  1375. }
  1376. /* Encoder supported input metadata specific configurations */
  1377. if (ctx.input_metadata)
  1378. {
  1379. v4l2_ctrl_videoenc_input_metadata VEnc_imeta_param;
  1380. v4l2_enc_frame_ROI_params VEnc_ROI_params;
  1381. v4l2_enc_frame_ReconCRC_params VEnc_ReconCRC_params;
  1382. v4l2_enc_frame_ext_rps_ctrl_params VEnc_ext_rps_ctrl_params;
  1383. v4l2_enc_frame_ext_rate_ctrl_params VEnc_ext_rate_ctrl_params;
  1384. v4l2_enc_gdr_params VEnc_gdr_params;
  1385. VEnc_imeta_param.flag = 0;
  1386. if (ctx.ROI_Param_file_path)
  1387. {
  1388. if (ctx.enableROI) {
  1389. VEnc_imeta_param.flag |= V4L2_ENC_INPUT_ROI_PARAM_FLAG;
  1390. VEnc_imeta_param.VideoEncROIParams = &VEnc_ROI_params;
  1391. /* Update Region of Intrest parameters from ROI params file */
  1392. populate_roi_Param(ctx.roi_Param_file, VEnc_imeta_param.VideoEncROIParams);
  1393. }
  1394. }
  1395. if (ctx.bReconCrc)
  1396. {
  1397. VEnc_imeta_param.flag |= V4L2_ENC_INPUT_RECONCRC_PARAM_FLAG;
  1398. VEnc_ReconCRC_params.ReconCRCRect.left = ctx.rl;
  1399. VEnc_ReconCRC_params.ReconCRCRect.top = ctx.rt;
  1400. VEnc_ReconCRC_params.ReconCRCRect.width = ctx.rw;
  1401. VEnc_ReconCRC_params.ReconCRCRect.height = ctx.rh;
  1402. /* Update reconstructed CRC Parameters */
  1403. VEnc_imeta_param.VideoReconCRCParams = &VEnc_ReconCRC_params;
  1404. }
  1405. if (ctx.externalRPS)
  1406. {
  1407. if (ctx.RPS_threeLayerSvc)
  1408. {
  1409. if (ctx.input_frames_queued_count > 0)
  1410. {
  1411. sem_wait(&ctx.rps_par.sema);
  1412. }
  1413. VEnc_imeta_param.flag |= V4L2_ENC_INPUT_RPS_PARAM_FLAG;
  1414. VEnc_imeta_param.VideoEncRPSParams = &VEnc_ext_rps_ctrl_params;
  1415. populate_ext_rps_threeLayerSvc_Param(&ctx, VEnc_imeta_param.VideoEncRPSParams);
  1416. }
  1417. else if (ctx.RPS_Param_file_path)
  1418. {
  1419. VEnc_imeta_param.flag |= V4L2_ENC_INPUT_RPS_PARAM_FLAG;
  1420. VEnc_imeta_param.VideoEncRPSParams = &VEnc_ext_rps_ctrl_params;
  1421. /* Update external reference picture set parameters from RPS params file */
  1422. populate_ext_rps_ctrl_Param(ctx.rps_Param_file, VEnc_imeta_param.VideoEncRPSParams);
  1423. }
  1424. }
  1425. if (ctx.GDR_Param_file_path)
  1426. {
  1427. if (ctx.enableGDR)
  1428. {
  1429. /* Update GDR parameters from GDR params file */
  1430. if (ctx.gdr_start_frame_number == 0xFFFFFFFF)
  1431. populate_gdr_Param(ctx.gdr_Param_file, &ctx.gdr_start_frame_number,
  1432. &ctx.gdr_num_frames);
  1433. if (ctx.input_frames_queued_count == ctx.gdr_start_frame_number)
  1434. {
  1435. ctx.gdr_out_frame_number = ctx.gdr_start_frame_number;
  1436. VEnc_gdr_params.nGDRFrames = ctx.gdr_num_frames;
  1437. VEnc_imeta_param.flag |= V4L2_ENC_INPUT_GDR_PARAM_FLAG;
  1438. VEnc_imeta_param.VideoEncGDRParams = &VEnc_gdr_params;
  1439. }
  1440. }
  1441. }
  1442. if (ctx.hints_Param_file_path)
  1443. {
  1444. if (ctx.externalRCHints) {
  1445. VEnc_imeta_param.flag |= V4L2_ENC_INPUT_RC_PARAM_FLAG;
  1446. VEnc_imeta_param.VideoEncExtRCParams = &VEnc_ext_rate_ctrl_params;
  1447. /* Update external rate control parameters from hints params file */
  1448. populate_ext_rate_ctrl_Param(ctx.hints_Param_file, VEnc_imeta_param.VideoEncExtRCParams);
  1449. }
  1450. }
  1451. if (VEnc_imeta_param.flag)
  1452. {
  1453. /* Set encoder input metadatas */
  1454. ctx.enc->SetInputMetaParams(v4l2_buf.index, VEnc_imeta_param);
  1455. v4l2_buf.reserved2 = v4l2_buf.index;
  1456. }
  1457. }
  1458. if (ctx.copy_timestamp)
  1459. {
  1460. /* Set user provided timestamp when copy timestamp is enabled */
  1461. v4l2_buf.flags |= V4L2_BUF_FLAG_TIMESTAMP_COPY;
  1462. ctx.timestamp += ctx.timestampincr;
  1463. v4l2_buf.timestamp.tv_sec = ctx.timestamp / (MICROSECOND_UNIT);
  1464. v4l2_buf.timestamp.tv_usec = ctx.timestamp % (MICROSECOND_UNIT);
  1465. }
  1466. if(ctx.output_memory_type == V4L2_MEMORY_DMABUF || ctx.output_memory_type == V4L2_MEMORY_MMAP)
  1467. {
  1468. for (uint32_t j = 0 ; j < buffer->n_planes ; j++)
  1469. {
  1470. NvBufSurface *nvbuf_surf = 0;
  1471. ret = NvBufSurfaceFromFd (buffer->planes[j].fd, (void**)(&nvbuf_surf));
  1472. if (ret < 0)
  1473. {
  1474. cerr << "Error while NvBufSurfaceFromFd" << endl;
  1475. abort(&ctx);
  1476. goto cleanup;
  1477. }
  1478. ret = NvBufSurfaceSyncForDevice (nvbuf_surf, 0, j);
  1479. if (ret < 0)
  1480. {
  1481. cerr << "Error while NvBufSurfaceSyncForDevice at output plane for V4L2_MEMORY_DMABUF" << endl;
  1482. abort(&ctx);
  1483. goto cleanup;
  1484. }
  1485. }
  1486. }
  1487. if(!ctx.num_frames_to_encode)
  1488. {
  1489. buffer->planes[0].bytesused = buffer->planes[1].bytesused = buffer->planes[2].bytesused = 0;
  1490. }
  1491. if(ctx.output_memory_type == V4L2_MEMORY_DMABUF)
  1492. {
  1493. for (uint32_t j = 0 ; j < buffer->n_planes ; j++)
  1494. {
  1495. v4l2_buf.m.planes[j].bytesused = buffer->planes[j].bytesused;
  1496. }
  1497. }
  1498. /* encoder qbuffer for output plane */
  1499. ret = ctx.enc->output_plane.qBuffer(v4l2_buf, NULL);
  1500. if (ret < 0)
  1501. {
  1502. cerr << "Error while queueing buffer at output plane" << endl;
  1503. abort(&ctx);
  1504. goto cleanup;
  1505. }
  1506. if(ctx.num_frames_to_encode > 0)
  1507. {
  1508. ctx.num_frames_to_encode--;
  1509. }
  1510. ctx.input_frames_queued_count++;
  1511. if (v4l2_buf.m.planes[0].bytesused == 0)
  1512. {
  1513. cerr << "File read complete." << endl;
  1514. eos = true;
  1515. ctx.got_eos = true;
  1516. return 0;
  1517. }
  1518. }
  1519. cleanup:
  1520. return -1;
  1521. }
  1522. /**
  1523. * Encode processing function.
  1524. *
  1525. * @param ctx : Encoder context
  1526. * @param argc : Argument Count
  1527. * @param argv : Argument Vector
  1528. */
  1529. static int encode_proc(context_t& ctx)
  1530. {
  1531. int ret = 0;
  1532. int error = 0;
  1533. bool eos = false;
  1534. /* Set default values for encoder context members. */
  1535. set_defaults(&ctx);
  1536. /* Parse application command line options. */
  1537. // ret = parse_csv_args(&ctx, argc, argv);
  1538. // TEST_ERROR(ret < 0, "Error parsing commandline arguments", cleanup);
  1539. /* Set thread name for encoder Output Plane thread. */
  1540. pthread_setname_np(pthread_self(),"EncOutPlane");
  1541. /* Get the parsed encoder runtime parameters */
  1542. if (ctx.runtime_params_str)
  1543. {
  1544. get_next_runtime_param_change_frame(&ctx);
  1545. }
  1546. if (ctx.encoder_pixfmt == V4L2_PIX_FMT_H265)
  1547. {
  1548. TEST_ERROR(ctx.width < 144 || ctx.height < 144, "Height/Width should be"
  1549. " > 144 for H.265", cleanup);
  1550. }
  1551. if (ctx.endf) {
  1552. TEST_ERROR(ctx.startf > ctx.endf, "End frame should be greater than start frame", cleanup);
  1553. ctx.num_frames_to_encode = ctx.endf - ctx.startf + 1;
  1554. }
  1555. if (ctx.use_gold_crc)
  1556. {
  1557. /* CRC specific initializetion if gold_crc flag is set */
  1558. ctx.pBitStreamCrc = InitCrc(CRC32_POLYNOMIAL);
  1559. TEST_ERROR(!ctx.pBitStreamCrc, "InitCrc failed", cleanup);
  1560. }
  1561. /* Open input file for raw yuv */
  1562. ctx.in_file = new ifstream(ctx.in_file_path);
  1563. TEST_ERROR(!ctx.in_file->is_open(), "Could not open input file", cleanup);
  1564. if (!ctx.stats)
  1565. {
  1566. /* Open output file for encoded bitstream */
  1567. ctx.out_file = new ofstream(ctx.out_file_path);
  1568. TEST_ERROR(!ctx.out_file->is_open(), "Could not open output file", cleanup);
  1569. }
  1570. if (ctx.ROI_Param_file_path) {
  1571. /* Open Region of Intreset(ROI) parameter file when ROI feature enabled */
  1572. ctx.roi_Param_file = new ifstream(ctx.ROI_Param_file_path);
  1573. TEST_ERROR(!ctx.roi_Param_file->is_open(), "Could not open roi param file", cleanup);
  1574. }
  1575. if (ctx.Recon_Ref_file_path) {
  1576. /* Open Reconstructed CRC reference file when ReconCRC feature enabled */
  1577. ctx.recon_Ref_file = new ifstream(ctx.Recon_Ref_file_path);
  1578. TEST_ERROR(!ctx.recon_Ref_file->is_open(), "Could not open recon crc reference file", cleanup);
  1579. }
  1580. if (ctx.RPS_Param_file_path) {
  1581. /* Open Reference Picture set(RPS) specififc reference file when Dynamic RPS feature enabled */
  1582. ctx.rps_Param_file = new ifstream(ctx.RPS_Param_file_path);
  1583. TEST_ERROR(!ctx.rps_Param_file->is_open(), "Could not open rps param file", cleanup);
  1584. }
  1585. if (ctx.GDR_Param_file_path) {
  1586. /* Open Gradual Decoder Refresh(GDR) parameters reference file when GDR feature enabled */
  1587. ctx.gdr_Param_file = new ifstream(ctx.GDR_Param_file_path);
  1588. TEST_ERROR(!ctx.gdr_Param_file->is_open(), "Could not open GDR param file", cleanup);
  1589. }
  1590. if (ctx.GDR_out_file_path) {
  1591. /* Open Gradual Decoder Refresh(GDR) output parameters reference file when GDR feature enabled */
  1592. ctx.gdr_out_file = new ofstream(ctx.GDR_out_file_path);
  1593. TEST_ERROR(!ctx.gdr_out_file->is_open(), "Could not open GDR Out file", cleanup);
  1594. }
  1595. if (ctx.hints_Param_file_path) {
  1596. /* Open external hints parameters file for when external rate control feature enabled */
  1597. ctx.hints_Param_file = new ifstream(ctx.hints_Param_file_path);
  1598. TEST_ERROR(!ctx.hints_Param_file->is_open(), "Could not open hints param file", cleanup);
  1599. }
  1600. /* Create NvVideoEncoder object for blocking or non-blocking I/O mode. */
  1601. if (ctx.blocking_mode) //进入blocking mode
  1602. {
  1603. cout << "Creating Encoder in blocking mode \n";
  1604. ctx.enc = NvVideoEncoder::createVideoEncoder("enc0");
  1605. }
  1606. else
  1607. {
  1608. cout << "Creating Encoder in non-blocking mode \n";
  1609. ctx.enc = NvVideoEncoder::createVideoEncoder("enc0", O_NONBLOCK);
  1610. }
  1611. TEST_ERROR(!ctx.enc, "Could not create encoder", cleanup);
  1612. if (ctx.stats)
  1613. {
  1614. ctx.enc->enableProfiling();
  1615. }
  1616. if (log_level >= LOG_LEVEL_DEBUG)
  1617. cout << "Encode pixel format :" << get_pixfmt_string(ctx.encoder_pixfmt) << endl;
  1618. /* Set encoder capture plane format.
  1619. NOTE: It is necessary that Capture Plane format be set before Output Plane
  1620. format. It is necessary to set width and height on the capture plane as well */
  1621. ret =
  1622. ctx.enc->setCapturePlaneFormat(ctx.encoder_pixfmt, ctx.width,
  1623. ctx.height, 2 * 1024 * 1024);
  1624. TEST_ERROR(ret < 0, "Could not set capture plane format", cleanup);
  1625. if (ctx.encoder_pixfmt == V4L2_PIX_FMT_H265)
  1626. {
  1627. switch (ctx.profile)
  1628. {
  1629. case V4L2_MPEG_VIDEO_H265_PROFILE_MAIN10:
  1630. {
  1631. ctx.raw_pixfmt = V4L2_PIX_FMT_P010M;
  1632. ctx.is_semiplanar = true; /* To keep previous execution commands working */
  1633. ctx.bit_depth = 10;
  1634. break;
  1635. }
  1636. case V4L2_MPEG_VIDEO_H265_PROFILE_MAIN:
  1637. {
  1638. if (ctx.is_semiplanar)
  1639. ctx.raw_pixfmt = V4L2_PIX_FMT_NV12M;
  1640. else
  1641. ctx.raw_pixfmt = V4L2_PIX_FMT_YUV420M;
  1642. if (ctx.chroma_format_idc == 3)
  1643. {
  1644. if (ctx.bit_depth == 10 && ctx.is_semiplanar)
  1645. ctx.raw_pixfmt = V4L2_PIX_FMT_NV24_10LE;
  1646. if (ctx.bit_depth == 8)
  1647. {
  1648. if (ctx.is_semiplanar)
  1649. ctx.raw_pixfmt = V4L2_PIX_FMT_NV24M;
  1650. else
  1651. ctx.raw_pixfmt = V4L2_PIX_FMT_YUV444M;
  1652. }
  1653. }
  1654. }
  1655. break;
  1656. default:
  1657. ctx.raw_pixfmt = V4L2_PIX_FMT_YUV420M;
  1658. }
  1659. }
  1660. if (ctx.encoder_pixfmt == V4L2_PIX_FMT_H264)
  1661. {
  1662. if (ctx.enableLossless &&
  1663. ctx.profile == V4L2_MPEG_VIDEO_H264_PROFILE_HIGH_444_PREDICTIVE)
  1664. {
  1665. if (ctx.is_semiplanar)
  1666. ctx.raw_pixfmt = V4L2_PIX_FMT_NV24M;
  1667. else
  1668. ctx.raw_pixfmt = V4L2_PIX_FMT_YUV444M;
  1669. }
  1670. else if ((ctx.enableLossless &&
  1671. ctx.profile != V4L2_MPEG_VIDEO_H264_PROFILE_HIGH_444_PREDICTIVE) ||
  1672. (!ctx.enableLossless && ctx.profile == V4L2_MPEG_VIDEO_H264_PROFILE_HIGH_444_PREDICTIVE))
  1673. {
  1674. cerr << "Lossless encoding is supported only for high444 profile\n";
  1675. error = 1;
  1676. goto cleanup;
  1677. }
  1678. else
  1679. {
  1680. if (ctx.is_semiplanar)
  1681. ctx.raw_pixfmt = V4L2_PIX_FMT_NV12M;
  1682. else
  1683. ctx.raw_pixfmt = V4L2_PIX_FMT_YUV420M;
  1684. }
  1685. }
  1686. if (log_level >= LOG_LEVEL_DEBUG)
  1687. cout << "Raw pixel format :" << get_pixfmt_string(ctx.raw_pixfmt) << endl;
  1688. /* Set encoder output plane format */
  1689. ret =
  1690. ctx.enc->setOutputPlaneFormat(ctx.raw_pixfmt, ctx.width,
  1691. ctx.height);
  1692. TEST_ERROR(ret < 0, "Could not set output plane format", cleanup);
  1693. if (ctx.num_frames_to_encode)
  1694. {
  1695. printf("ctx.num_frames_to_encode\n");
  1696. ret = ctx.enc->setFramesToEncode(ctx.num_frames_to_encode);
  1697. TEST_ERROR(ret < 0, "Could not set frames to encode", cleanup);
  1698. }
  1699. ret = ctx.enc->setBitrate(ctx.bitrate);
  1700. TEST_ERROR(ret < 0, "Could not set encoder bitrate", cleanup);
  1701. if (ctx.encoder_pixfmt == V4L2_PIX_FMT_H264)
  1702. {
  1703. /* Set encoder profile for H264 format */
  1704. ret = ctx.enc->setProfile(ctx.profile);
  1705. TEST_ERROR(ret < 0, "Could not set encoder profile", cleanup);
  1706. if (ctx.level == (uint32_t)-1)
  1707. {
  1708. ctx.level = (uint32_t)V4L2_MPEG_VIDEO_H264_LEVEL_5_1;
  1709. }
  1710. ret = ctx.enc->setLevel(ctx.level);
  1711. TEST_ERROR(ret < 0, "Could not set encoder level", cleanup);
  1712. }
  1713. else if (ctx.encoder_pixfmt == V4L2_PIX_FMT_H265)
  1714. {
  1715. ret = ctx.enc->setProfile(ctx.profile);
  1716. TEST_ERROR(ret < 0, "Could not set encoder profile", cleanup);
  1717. if (ctx.level != (uint32_t)-1)
  1718. {
  1719. ret = ctx.enc->setLevel(ctx.level);
  1720. TEST_ERROR(ret < 0, "Could not set encoder level", cleanup);
  1721. }
  1722. if (ctx.chroma_format_idc != (uint8_t)-1)
  1723. {
  1724. ret = ctx.enc->setChromaFactorIDC(ctx.chroma_format_idc);
  1725. TEST_ERROR(ret < 0, "Could not set chroma_format_idc", cleanup);
  1726. }
  1727. }
  1728. if (ctx.enable_initQP)
  1729. {
  1730. ret = ctx.enc->setInitQP(ctx.IinitQP, ctx.PinitQP, ctx.BinitQP);
  1731. TEST_ERROR(ret < 0, "Could not set encoder init QP", cleanup);
  1732. }
  1733. if (ctx.enableLossless)
  1734. {
  1735. ret = ctx.enc->setLossless(ctx.enableLossless);
  1736. TEST_ERROR(ret < 0, "Could not set lossless encoding", cleanup);
  1737. }
  1738. else if (!ctx.enable_ratecontrol)
  1739. {
  1740. /* Set constant QP configuration by disabling rate control */
  1741. ret = ctx.enc->setConstantQp(ctx.enable_ratecontrol);
  1742. TEST_ERROR(ret < 0, "Could not set encoder constant QP", cleanup);
  1743. }
  1744. else
  1745. {
  1746. /* Set rate control mode for encoder */
  1747. ret = ctx.enc->setRateControlMode(ctx.ratecontrol);
  1748. TEST_ERROR(ret < 0, "Could not set encoder rate control mode", cleanup);
  1749. if (ctx.ratecontrol == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR) {
  1750. uint32_t peak_bitrate;
  1751. if (ctx.peak_bitrate < ctx.bitrate)
  1752. peak_bitrate = 1.2f * ctx.bitrate;
  1753. else
  1754. peak_bitrate = ctx.peak_bitrate;
  1755. /* Set peak bitrate value for variable bitrate mode for encoder */
  1756. ret = ctx.enc->setPeakBitrate(peak_bitrate);
  1757. TEST_ERROR(ret < 0, "Could not set encoder peak bitrate", cleanup);
  1758. }
  1759. }
  1760. if (ctx.poc_type)
  1761. {
  1762. ret = ctx.enc->setPocType(ctx.poc_type);
  1763. TEST_ERROR(ret < 0, "Could not set Picture Order Count value", cleanup);
  1764. }
  1765. /* Set IDR frame interval for encoder */
  1766. ret = ctx.enc->setIDRInterval(ctx.idr_interval);
  1767. TEST_ERROR(ret < 0, "Could not set encoder IDR interval", cleanup);
  1768. /* Set I frame interval for encoder */
  1769. ret = ctx.enc->setIFrameInterval(ctx.iframe_interval);
  1770. TEST_ERROR(ret < 0, "Could not set encoder I-Frame interval", cleanup);
  1771. /* Set framerate for encoder */
  1772. ret = ctx.enc->setFrameRate(ctx.fps_n, ctx.fps_d);
  1773. TEST_ERROR(ret < 0, "Could not set framerate", cleanup);
  1774. if (ctx.temporal_tradeoff_level)
  1775. {
  1776. /* Set temporal tradeoff level value for encoder */
  1777. ret = ctx.enc->setTemporalTradeoff(ctx.temporal_tradeoff_level);
  1778. TEST_ERROR(ret < 0, "Could not set temporal tradeoff level", cleanup);
  1779. }
  1780. if (ctx.slice_length)
  1781. {
  1782. /* Set slice length value for encoder */
  1783. ret = ctx.enc->setSliceLength(ctx.slice_length_type,
  1784. ctx.slice_length);
  1785. TEST_ERROR(ret < 0, "Could not set slice length params", cleanup);
  1786. }
  1787. if (ctx.enable_slice_level_encode)
  1788. {
  1789. /* Enable slice level encode for encoder */
  1790. ret = ctx.enc->setSliceLevelEncode(true);
  1791. TEST_ERROR(ret < 0, "Could not set slice level encode", cleanup);
  1792. }
  1793. if (ctx.hw_preset_type)
  1794. {
  1795. /* Set hardware preset value for encoder */
  1796. ret = ctx.enc->setHWPresetType(ctx.hw_preset_type);
  1797. TEST_ERROR(ret < 0, "Could not set encoder HW Preset Type", cleanup);
  1798. }
  1799. if (ctx.virtual_buffer_size)
  1800. {
  1801. /* Set virtual buffer size value for encoder */
  1802. ret = ctx.enc->setVirtualBufferSize(ctx.virtual_buffer_size);
  1803. TEST_ERROR(ret < 0, "Could not set virtual buffer size", cleanup);
  1804. }
  1805. if (ctx.slice_intrarefresh_interval)
  1806. {
  1807. /* Set slice intra refresh interval value for encoder */
  1808. ret = ctx.enc->setSliceIntrarefresh(ctx.slice_intrarefresh_interval);
  1809. TEST_ERROR(ret < 0, "Could not set slice intrarefresh interval", cleanup);
  1810. }
  1811. if (ctx.insert_sps_pps_at_idr)
  1812. {
  1813. /* Enable insert of SPSPPS at IDR frames */
  1814. ret = ctx.enc->setInsertSpsPpsAtIdrEnabled(true);
  1815. TEST_ERROR(ret < 0, "Could not set insertSPSPPSAtIDR", cleanup);
  1816. }
  1817. if (ctx.disable_cabac)
  1818. {
  1819. /* Disable CABAC entropy encoding */
  1820. ret = ctx.enc->setCABAC(false);
  1821. TEST_ERROR(ret < 0, "Could not set disable CABAC", cleanup);
  1822. }
  1823. if (ctx.sar_width)
  1824. {
  1825. /* Set SAR width */
  1826. ret = ctx.enc->setSampleAspectRatioWidth(ctx.sar_width);
  1827. TEST_ERROR(ret < 0, "Could not set Sample Aspect Ratio width", cleanup);
  1828. }
  1829. if (ctx.sar_height)
  1830. {
  1831. /* Set SAR width */
  1832. ret = ctx.enc->setSampleAspectRatioHeight(ctx.sar_height);
  1833. TEST_ERROR(ret < 0, "Could not set Sample Aspect Ratio height", cleanup);
  1834. }
  1835. if (ctx.insert_vui)
  1836. {
  1837. /* Enable insert of VUI parameters */
  1838. ret = ctx.enc->setInsertVuiEnabled(true);
  1839. TEST_ERROR(ret < 0, "Could not set insertVUI", cleanup);
  1840. }
  1841. if (ctx.enable_extended_colorformat)
  1842. {
  1843. /* Enable extnded colorformat for encoder */
  1844. ret = ctx.enc->setExtendedColorFormat(true);
  1845. TEST_ERROR(ret < 0, "Could not set extended color format", cleanup);
  1846. }
  1847. if (ctx.insert_aud)
  1848. {
  1849. /* Enable insert of AUD parameters */
  1850. ret = ctx.enc->setInsertAudEnabled(true);
  1851. TEST_ERROR(ret < 0, "Could not set insertAUD", cleanup);
  1852. }
  1853. if (ctx.alliframes)
  1854. {
  1855. /* Enable all I-frame encode */
  1856. ret = ctx.enc->setAlliFramesEncode(true);
  1857. TEST_ERROR(ret < 0, "Could not set Alliframes encoding", cleanup);
  1858. }
  1859. if (ctx.num_b_frames != (uint32_t) -1)
  1860. {
  1861. /* Set number of B-frames to to be used by encoder */
  1862. ret = ctx.enc->setNumBFrames(ctx.num_b_frames);
  1863. TEST_ERROR(ret < 0, "Could not set number of B Frames", cleanup);
  1864. }
  1865. if ((ctx.nMinQpI != (uint32_t)QP_RETAIN_VAL) ||
  1866. (ctx.nMaxQpI != (uint32_t)QP_RETAIN_VAL) ||
  1867. (ctx.nMinQpP != (uint32_t)QP_RETAIN_VAL) ||
  1868. (ctx.nMaxQpP != (uint32_t)QP_RETAIN_VAL) ||
  1869. (ctx.nMinQpB != (uint32_t)QP_RETAIN_VAL) ||
  1870. (ctx.nMaxQpB != (uint32_t)QP_RETAIN_VAL))
  1871. {
  1872. /* Set Min & Max qp range values for I/P/B-frames to be used by encoder */
  1873. ret = ctx.enc->setQpRange(ctx.nMinQpI, ctx.nMaxQpI, ctx.nMinQpP,
  1874. ctx.nMaxQpP, ctx.nMinQpB, ctx.nMaxQpB);
  1875. TEST_ERROR(ret < 0, "Could not set quantization parameters", cleanup);
  1876. }
  1877. if (ctx.max_perf)
  1878. {
  1879. /* Enable maximum performance mode by disabling internal DFS logic.
  1880. NOTE: This enables encoder to run at max clocks */
  1881. ret = ctx.enc->setMaxPerfMode(ctx.max_perf);
  1882. TEST_ERROR(ret < 0, "Error while setting encoder to max perf", cleanup);
  1883. }
  1884. if (ctx.dump_mv)
  1885. {
  1886. /* Enable dumping of motion vectors report from encoder */
  1887. ret = ctx.enc->enableMotionVectorReporting();
  1888. TEST_ERROR(ret < 0, "Could not enable motion vector reporting", cleanup);
  1889. }
  1890. if (ctx.bnoIframe) {
  1891. ctx.iframe_interval = ((1<<31) + 1); /* TODO: how can we do this properly */
  1892. ret = ctx.enc->setIFrameInterval(ctx.iframe_interval);
  1893. TEST_ERROR(ret < 0, "Could not set encoder I-Frame interval", cleanup);
  1894. }
  1895. if (ctx.enableROI) {
  1896. v4l2_enc_enable_roi_param VEnc_enable_ext_roi_ctrl;
  1897. VEnc_enable_ext_roi_ctrl.bEnableROI = ctx.enableROI;
  1898. /* Enable region of intrest configuration for encoder */
  1899. ret = ctx.enc->enableROI(VEnc_enable_ext_roi_ctrl);
  1900. TEST_ERROR(ret < 0, "Could not enable ROI", cleanup);
  1901. }
  1902. if (ctx.bReconCrc) {
  1903. v4l2_enc_enable_reconcrc_param VEnc_enable_recon_crc_ctrl;
  1904. VEnc_enable_recon_crc_ctrl.bEnableReconCRC = ctx.bReconCrc;
  1905. /* Enable reconstructed CRC configuration for encoder */
  1906. ret = ctx.enc->enableReconCRC(VEnc_enable_recon_crc_ctrl);
  1907. TEST_ERROR(ret < 0, "Could not enable Recon CRC", cleanup);
  1908. }
  1909. if (ctx.externalRPS) {
  1910. v4l2_enc_enable_ext_rps_ctr VEnc_enable_ext_rps_ctrl;
  1911. VEnc_enable_ext_rps_ctrl.bEnableExternalRPS = ctx.externalRPS;
  1912. if (ctx.encoder_pixfmt == V4L2_PIX_FMT_H264) {
  1913. VEnc_enable_ext_rps_ctrl.bGapsInFrameNumAllowed = ctx.bGapsInFrameNumAllowed;
  1914. VEnc_enable_ext_rps_ctrl.nH264FrameNumBits = ctx.nH264FrameNumBits;
  1915. }
  1916. if (ctx.encoder_pixfmt == V4L2_PIX_FMT_H265) {
  1917. VEnc_enable_ext_rps_ctrl.nH265PocLsbBits = ctx.nH265PocLsbBits;
  1918. }
  1919. /* Enable external reference picture set configuration for encoder */
  1920. ret = ctx.enc->enableExternalRPS(VEnc_enable_ext_rps_ctrl);
  1921. TEST_ERROR(ret < 0, "Could not enable external RPS", cleanup);
  1922. }
  1923. if (ctx.num_reference_frames)
  1924. {
  1925. /* Set number of reference frame configuration value for encoder */
  1926. ret = ctx.enc->setNumReferenceFrames(ctx.num_reference_frames);
  1927. TEST_ERROR(ret < 0, "Could not set num reference frames", cleanup);
  1928. }
  1929. if (ctx.externalRCHints) {
  1930. v4l2_enc_enable_ext_rate_ctr VEnc_enable_ext_rate_ctrl;
  1931. VEnc_enable_ext_rate_ctrl.bEnableExternalPictureRC = ctx.externalRCHints;
  1932. VEnc_enable_ext_rate_ctrl.nsessionMaxQP = ctx.sMaxQp;
  1933. /* Enable external rate control configuration for encoder */
  1934. ret = ctx.enc->enableExternalRC(VEnc_enable_ext_rate_ctrl);
  1935. TEST_ERROR(ret < 0, "Could not enable external RC", cleanup);
  1936. }
  1937. if (ctx.encoder_pixfmt == V4L2_PIX_FMT_AV1)
  1938. {
  1939. if (ctx.enable_av1tile)
  1940. {
  1941. v4l2_enc_av1_tile_config VEnc_av1_tile_config;
  1942. VEnc_av1_tile_config.bEnableTile = ctx.enable_av1tile;
  1943. VEnc_av1_tile_config.nLog2RowTiles = ctx.log2_num_av1rows;
  1944. VEnc_av1_tile_config.nLog2ColTiles = ctx.log2_num_av1cols;
  1945. /* Enable tile configuration for encoder */
  1946. ret = ctx.enc->enableAV1Tile(VEnc_av1_tile_config);
  1947. TEST_ERROR(ret < 0, "Could not enable Tile Configuration", cleanup);
  1948. }
  1949. if (ctx.enable_av1ssimrdo != (uint8_t) -1)
  1950. {
  1951. ret = ctx.enc->setAV1SsimRdo(ctx.enable_av1ssimrdo);
  1952. TEST_ERROR(ret < 0, "Could not set Ssim RDO", cleanup);
  1953. }
  1954. if (ctx.disable_av1cdfupdate != (uint8_t) -1)
  1955. {
  1956. ret = ctx.enc->setAV1DisableCDFUpdate(ctx.disable_av1cdfupdate);
  1957. TEST_ERROR(ret < 0, "Could not set disable CDF update", cleanup);
  1958. }
  1959. }
  1960. /* Query, Export and Map the output plane buffers so that we can read
  1961. raw data into the buffers */
  1962. switch(ctx.output_memory_type)
  1963. {
  1964. case V4L2_MEMORY_MMAP:
  1965. ret = ctx.enc->output_plane.setupPlane(V4L2_MEMORY_MMAP, 10, true, false);
  1966. TEST_ERROR(ret < 0, "Could not setup output plane", cleanup);
  1967. break;
  1968. case V4L2_MEMORY_USERPTR:
  1969. ret = ctx.enc->output_plane.setupPlane(V4L2_MEMORY_USERPTR, 10, false, true);
  1970. TEST_ERROR(ret < 0, "Could not setup output plane", cleanup);
  1971. break;
  1972. case V4L2_MEMORY_DMABUF:
  1973. ret = setup_output_dmabuf(&ctx,10);
  1974. TEST_ERROR(ret < 0, "Could not setup plane", cleanup);
  1975. break;
  1976. default :
  1977. TEST_ERROR(true, "Not a valid plane", cleanup);
  1978. }
  1979. /* Query, Export and Map the capture plane buffers so that we can write
  1980. encoded bitstream data into the buffers */
  1981. switch(ctx.capture_memory_type)
  1982. {
  1983. case V4L2_MEMORY_MMAP:
  1984. ret = ctx.enc->capture_plane.setupPlane(V4L2_MEMORY_MMAP, ctx.num_output_buffers, true, false);
  1985. TEST_ERROR(ret < 0, "Could not setup capture plane", cleanup);
  1986. break;
  1987. case V4L2_MEMORY_DMABUF:
  1988. ret = setup_capture_dmabuf(&ctx,ctx.num_output_buffers);
  1989. TEST_ERROR(ret < 0, "Could not setup plane", cleanup);
  1990. break;
  1991. default :
  1992. TEST_ERROR(true, "Not a valid plane", cleanup);
  1993. }
  1994. /* Subscibe for End Of Stream event */
  1995. ret = ctx.enc->subscribeEvent(V4L2_EVENT_EOS,0,0);
  1996. TEST_ERROR(ret < 0, "Could not subscribe EOS event", cleanup);
  1997. if (ctx.b_use_enc_cmd)
  1998. {
  1999. /* Send v4l2 command for encoder start */
  2000. ret = ctx.enc->setEncoderCommand(V4L2_ENC_CMD_START, 0);
  2001. TEST_ERROR(ret < 0, "Error in start of encoder commands ", cleanup);
  2002. }
  2003. else
  2004. {
  2005. /* set encoder output plane STREAMON */
  2006. ret = ctx.enc->output_plane.setStreamStatus(true);
  2007. TEST_ERROR(ret < 0, "Error in output plane streamon", cleanup);
  2008. /* set encoder capture plane STREAMON */
  2009. ret = ctx.enc->capture_plane.setStreamStatus(true);
  2010. TEST_ERROR(ret < 0, "Error in capture plane streamon", cleanup);
  2011. }
  2012. if (ctx.blocking_mode)
  2013. {
  2014. if (ctx.RPS_threeLayerSvc)
  2015. {
  2016. sem_init(&ctx.rps_par.sema, 0, 0);
  2017. }
  2018. /* Set encoder capture plane dq thread callback for blocking io mode */
  2019. ctx.enc->capture_plane.setDQThreadCallback(encoder_capture_plane_dq_callback);
  2020. printf("jinrujinru \n");
  2021. /* startDQThread starts a thread internally which calls the
  2022. encoder_capture_plane_dq_callback whenever a buffer is dequeued
  2023. on the plane */
  2024. ctx.enc->capture_plane.startDQThread(&ctx);
  2025. }
  2026. else
  2027. {
  2028. sem_init(&ctx.pollthread_sema, 0, 0);
  2029. sem_init(&ctx.encoderthread_sema, 0, 0);
  2030. /* Set encoder poll thread for non-blocking io mode */
  2031. pthread_create(&ctx.enc_pollthread, NULL, encoder_pollthread_fcn, &ctx);
  2032. pthread_setname_np(ctx.enc_pollthread, "EncPollThread");
  2033. cout << "Created the PollThread and Encoder Thread \n";
  2034. }
  2035. /* Enqueue all the empty capture plane buffers. */
  2036. for (uint32_t i = 0; i < ctx.enc->capture_plane.getNumBuffers(); i++)
  2037. {
  2038. struct v4l2_buffer v4l2_buf;
  2039. struct v4l2_plane planes[MAX_PLANES];
  2040. memset(&v4l2_buf, 0, sizeof(v4l2_buf));
  2041. memset(planes, 0, MAX_PLANES * sizeof(struct v4l2_plane));
  2042. v4l2_buf.index = i;
  2043. v4l2_buf.m.planes = planes;
  2044. if(ctx.capture_memory_type == V4L2_MEMORY_DMABUF)
  2045. {
  2046. v4l2_buf.m.planes[0].m.fd = ctx.capture_plane_fd[i];
  2047. /* Map capture plane buffer for memory type DMABUF. */
  2048. ret = ctx.enc->capture_plane.mapOutputBuffers(v4l2_buf, ctx.capture_plane_fd[i]);
  2049. if (ret < 0)
  2050. {
  2051. cerr << "Error while mapping buffer at capture plane" << endl;
  2052. abort(&ctx);
  2053. goto cleanup;
  2054. }
  2055. }
  2056. ret = ctx.enc->capture_plane.qBuffer(v4l2_buf, NULL);
  2057. if (ret < 0)
  2058. {
  2059. cerr << "Error while queueing buffer at capture plane" << endl;
  2060. abort(&ctx);
  2061. goto cleanup;
  2062. }
  2063. }
  2064. if (ctx.copy_timestamp) {
  2065. /* Set user provided timestamp when copy timestamp is enabled */
  2066. ctx.timestamp = (ctx.start_ts * MICROSECOND_UNIT);
  2067. ctx.timestampincr = (MICROSECOND_UNIT * 16) / ((uint32_t) (ctx.fps_n * 16));
  2068. }
  2069. if(ctx.ppe_init_params.enable_ppe)
  2070. {
  2071. ret = ctx.enc->setPPEInitParams(ctx.ppe_init_params);
  2072. if (ret < 0){
  2073. cerr << "Error calling setPPEInitParams" << endl;
  2074. }
  2075. }
  2076. // /* Read video frame and queue all the output plane buffers. */
  2077. // for (uint32_t i = 0; i < ctx.enc->output_plane.getNumBuffers(); i++)
  2078. // {
  2079. // struct v4l2_buffer v4l2_buf;
  2080. // struct v4l2_plane planes[MAX_PLANES];
  2081. // NvBuffer *buffer = ctx.enc->output_plane.getNthBuffer(i);
  2082. // memset(&v4l2_buf, 0, sizeof(v4l2_buf));
  2083. // memset(planes, 0, MAX_PLANES * sizeof(struct v4l2_plane));
  2084. // v4l2_buf.index = i;
  2085. // v4l2_buf.m.planes = planes;
  2086. // if(ctx.output_memory_type == V4L2_MEMORY_DMABUF)
  2087. // {
  2088. // v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
  2089. // v4l2_buf.memory = V4L2_MEMORY_DMABUF;
  2090. // /* Map output plane buffer for memory type DMABUF. */
  2091. // ret = ctx.enc->output_plane.mapOutputBuffers(v4l2_buf, ctx.output_plane_fd[i]);
  2092. // if (ret < 0)
  2093. // {
  2094. // cerr << "Error while mapping buffer at output plane" << endl;
  2095. // abort(&ctx);
  2096. // goto cleanup;
  2097. // }
  2098. // }
  2099. // if(ctx.startf)
  2100. // {
  2101. // uint32_t i = 0, frame_size = 0;
  2102. // for (i = 0; i < buffer->n_planes; i++)
  2103. // {
  2104. // frame_size += buffer->planes[i].fmt.bytesperpixel * buffer->planes[i].fmt.width * buffer->planes[i].fmt.height;
  2105. // }
  2106. // frame_size = frame_size * ctx.startf;
  2107. // ctx.in_file->seekg (frame_size, std::ios::cur);
  2108. // ctx.startf = 0;
  2109. // }
  2110. // /* Read yuv frame data from input file */
  2111. // if (read_video_frame(ctx.in_file, *buffer) < 0 || ctx.num_frames_to_encode == 0)
  2112. // {
  2113. // cerr << "Could not read complete frame from input file" << endl;
  2114. // v4l2_buf.m.planes[0].bytesused = 0;
  2115. // if(ctx.b_use_enc_cmd)
  2116. // {
  2117. // /* Send v4l2 command for encoder stop */
  2118. // ret = ctx.enc->setEncoderCommand(V4L2_ENC_CMD_STOP, 1);
  2119. // eos = true;
  2120. // break;
  2121. // }
  2122. // else
  2123. // {
  2124. // eos = true;
  2125. // v4l2_buf.m.planes[0].m.userptr = 0;
  2126. // v4l2_buf.m.planes[0].bytesused = v4l2_buf.m.planes[1].bytesused = v4l2_buf.m.planes[2].bytesused = 0;
  2127. // }
  2128. // }
  2129. // if (ctx.runtime_params_str &&
  2130. // (ctx.enc->output_plane.getTotalQueuedBuffers() ==
  2131. // ctx.next_param_change_frame))
  2132. // {
  2133. // /* Set runtime configuration parameters */
  2134. // set_runtime_params(&ctx);
  2135. // if (ctx.runtime_params_str)
  2136. // get_next_runtime_param_change_frame(&ctx);
  2137. // }
  2138. // /* Encoder supported input metadata specific configurations */
  2139. // if (ctx.input_metadata)
  2140. // {
  2141. // v4l2_ctrl_videoenc_input_metadata VEnc_imeta_param;
  2142. // v4l2_enc_frame_ROI_params VEnc_ROI_params;
  2143. // v4l2_enc_frame_ReconCRC_params VEnc_ReconCRC_params;
  2144. // v4l2_enc_frame_ext_rps_ctrl_params VEnc_ext_rps_ctrl_params;
  2145. // v4l2_enc_frame_ext_rate_ctrl_params VEnc_ext_rate_ctrl_params;
  2146. // v4l2_enc_gdr_params VEnc_gdr_params;
  2147. // VEnc_imeta_param.flag = 0;
  2148. // if (ctx.ROI_Param_file_path)
  2149. // {
  2150. // if (ctx.enableROI) {
  2151. // VEnc_imeta_param.flag |= V4L2_ENC_INPUT_ROI_PARAM_FLAG;
  2152. // VEnc_imeta_param.VideoEncROIParams = &VEnc_ROI_params;
  2153. // /* Update Region of Intrest parameters from ROI params file */
  2154. // populate_roi_Param(ctx.roi_Param_file, VEnc_imeta_param.VideoEncROIParams);
  2155. // }
  2156. // }
  2157. // if (ctx.bReconCrc)
  2158. // {
  2159. // VEnc_imeta_param.flag |= V4L2_ENC_INPUT_RECONCRC_PARAM_FLAG;
  2160. // VEnc_ReconCRC_params.ReconCRCRect.left = ctx.rl;
  2161. // VEnc_ReconCRC_params.ReconCRCRect.top = ctx.rt;
  2162. // VEnc_ReconCRC_params.ReconCRCRect.width = ctx.rw;
  2163. // VEnc_ReconCRC_params.ReconCRCRect.height = ctx.rh;
  2164. // /* Update reconstructed CRC Parameters */
  2165. // VEnc_imeta_param.VideoReconCRCParams = &VEnc_ReconCRC_params;
  2166. // }
  2167. // if (ctx.externalRPS)
  2168. // {
  2169. // if (ctx.RPS_threeLayerSvc)
  2170. // {
  2171. // if (ctx.input_frames_queued_count > 0)
  2172. // {
  2173. // sem_wait(&ctx.rps_par.sema);
  2174. // }
  2175. // VEnc_imeta_param.flag |= V4L2_ENC_INPUT_RPS_PARAM_FLAG;
  2176. // VEnc_imeta_param.VideoEncRPSParams = &VEnc_ext_rps_ctrl_params;
  2177. // populate_ext_rps_threeLayerSvc_Param(&ctx, VEnc_imeta_param.VideoEncRPSParams);
  2178. // }
  2179. // else if (ctx.RPS_Param_file_path)
  2180. // {
  2181. // VEnc_imeta_param.flag |= V4L2_ENC_INPUT_RPS_PARAM_FLAG;
  2182. // VEnc_imeta_param.VideoEncRPSParams = &VEnc_ext_rps_ctrl_params;
  2183. // /* Update external reference picture set parameters from RPS params file */
  2184. // populate_ext_rps_ctrl_Param(ctx.rps_Param_file, VEnc_imeta_param.VideoEncRPSParams);
  2185. // }
  2186. // }
  2187. // if (ctx.GDR_Param_file_path)
  2188. // {
  2189. // if (ctx.enableGDR)
  2190. // {
  2191. // /* Update GDR parameters from GDR params file */
  2192. // if (ctx.gdr_start_frame_number == 0xFFFFFFFF)
  2193. // populate_gdr_Param(ctx.gdr_Param_file, &ctx.gdr_start_frame_number,
  2194. // &ctx.gdr_num_frames);
  2195. // if (ctx.input_frames_queued_count == ctx.gdr_start_frame_number)
  2196. // {
  2197. // ctx.gdr_out_frame_number = ctx.gdr_start_frame_number;
  2198. // VEnc_gdr_params.nGDRFrames = ctx.gdr_num_frames;
  2199. // VEnc_imeta_param.flag |= V4L2_ENC_INPUT_GDR_PARAM_FLAG;
  2200. // VEnc_imeta_param.VideoEncGDRParams = &VEnc_gdr_params;
  2201. // }
  2202. // }
  2203. // }
  2204. // if (ctx.hints_Param_file_path)
  2205. // {
  2206. // if (ctx.externalRCHints) {
  2207. // VEnc_imeta_param.flag |= V4L2_ENC_INPUT_RC_PARAM_FLAG;
  2208. // VEnc_imeta_param.VideoEncExtRCParams = &VEnc_ext_rate_ctrl_params;
  2209. // /* Update external rate control parameters from hints params file */
  2210. // populate_ext_rate_ctrl_Param(ctx.hints_Param_file, VEnc_imeta_param.VideoEncExtRCParams);
  2211. // }
  2212. // }
  2213. // if (VEnc_imeta_param.flag)
  2214. // {
  2215. // /* Set encoder input metadatas */
  2216. // ctx.enc->SetInputMetaParams(v4l2_buf.index, VEnc_imeta_param);
  2217. // v4l2_buf.reserved2 = v4l2_buf.index;
  2218. // }
  2219. // }
  2220. // if (ctx.copy_timestamp)
  2221. // {
  2222. // v4l2_buf.flags |= V4L2_BUF_FLAG_TIMESTAMP_COPY;
  2223. // ctx.timestamp += ctx.timestampincr;
  2224. // v4l2_buf.timestamp.tv_sec = ctx.timestamp / (MICROSECOND_UNIT);
  2225. // v4l2_buf.timestamp.tv_usec = ctx.timestamp % (MICROSECOND_UNIT);
  2226. // }
  2227. // if(ctx.output_memory_type == V4L2_MEMORY_DMABUF || ctx.output_memory_type == V4L2_MEMORY_MMAP)
  2228. // {
  2229. // for (uint32_t j = 0 ; j < buffer->n_planes; j++)
  2230. // {
  2231. // NvBufSurface *nvbuf_surf = 0;
  2232. // ret = NvBufSurfaceFromFd (buffer->planes[j].fd, (void**)(&nvbuf_surf));
  2233. // if (ret < 0)
  2234. // {
  2235. // cerr << "Error while NvBufSurfaceFromFd" << endl;
  2236. // abort(&ctx);
  2237. // goto cleanup;
  2238. // }
  2239. // ret = NvBufSurfaceSyncForDevice (nvbuf_surf, 0, j);
  2240. // if (ret < 0)
  2241. // {
  2242. // cerr << "Error while NvBufSurfaceSyncForDevice at output plane for V4L2_MEMORY_DMABUF" << endl;
  2243. // abort(&ctx);
  2244. // goto cleanup;
  2245. // }
  2246. // }
  2247. // }
  2248. // if(ctx.output_memory_type == V4L2_MEMORY_DMABUF)
  2249. // {
  2250. // for (uint32_t j = 0 ; j < buffer->n_planes ; j++)
  2251. // {
  2252. // v4l2_buf.m.planes[j].bytesused = buffer->planes[j].bytesused;
  2253. // }
  2254. // }
  2255. // /* encoder qbuffer for output plane */
  2256. // ret = ctx.enc->output_plane.qBuffer(v4l2_buf, NULL);
  2257. // if (ret < 0)
  2258. // {
  2259. // cerr << "Error while queueing buffer at output plane" << endl;
  2260. // abort(&ctx);
  2261. // goto cleanup;
  2262. // }
  2263. // if(ctx.num_frames_to_encode > 0)
  2264. // {
  2265. // ctx.num_frames_to_encode --;
  2266. // }
  2267. // if (v4l2_buf.m.planes[0].bytesused == 0)
  2268. // {
  2269. // cerr << "File read complete." << endl;
  2270. // eos = true;
  2271. // break;
  2272. // }
  2273. // ctx.input_frames_queued_count++;
  2274. // }
  2275. if (ctx.blocking_mode)
  2276. {
  2277. /* Wait till capture plane DQ Thread finishes
  2278. i.e. all the capture plane buffers are dequeued. */
  2279. if (encoder_proc_blocking(ctx, eos) != 0)
  2280. goto cleanup;
  2281. ctx.enc->capture_plane.waitForDQThread(-1);
  2282. }
  2283. else
  2284. {
  2285. if (encoder_proc_nonblocking(ctx, eos) != 0)
  2286. goto cleanup;
  2287. }
  2288. if (ctx.stats)
  2289. {
  2290. ctx.enc->printProfilingStats(cout);
  2291. }
  2292. cleanup:
  2293. if (ctx.enc && ctx.enc->isInError())
  2294. {
  2295. cerr << "Encoder is in error" << endl;
  2296. error = 1;
  2297. }
  2298. if (ctx.got_error)
  2299. {
  2300. error = 1;
  2301. }
  2302. if (ctx.pBitStreamCrc)
  2303. {
  2304. char *pgold_crc = ctx.gold_crc;
  2305. Crc *pout_crc= ctx.pBitStreamCrc;
  2306. char StrCrcValue[20];
  2307. snprintf (StrCrcValue, 20, "%u", pout_crc->CrcValue);
  2308. /* Remove CRLF from end of CRC, if present */
  2309. do {
  2310. unsigned int len = strlen(pgold_crc);
  2311. if (len == 0) break;
  2312. if (pgold_crc[len-1] == '\n')
  2313. pgold_crc[len-1] = '\0';
  2314. else if (pgold_crc[len-1] == '\r')
  2315. pgold_crc[len-1] = '\0';
  2316. else
  2317. break;
  2318. } while(1);
  2319. /* Check with golden CRC */
  2320. if (strcmp (StrCrcValue, pgold_crc))
  2321. {
  2322. cout << "======================" << endl;
  2323. cout << "video_encode: CRC FAILED" << endl;
  2324. cout << "======================" << endl;
  2325. cout << "Encoded CRC: " << StrCrcValue << " Gold CRC: " << pgold_crc << endl;
  2326. error = 1;
  2327. }
  2328. else
  2329. {
  2330. cout << "======================" << endl;
  2331. cout << "video_encode: CRC PASSED" << endl;
  2332. cout << "======================" << endl;
  2333. }
  2334. CloseCrc(&ctx.pBitStreamCrc);
  2335. }
  2336. if(ctx.output_memory_type == V4L2_MEMORY_DMABUF && ctx.enc)
  2337. {
  2338. for (uint32_t i = 0; i < ctx.enc->output_plane.getNumBuffers(); i++)
  2339. {
  2340. /* Unmap output plane buffer for memory type DMABUF. */
  2341. ret = ctx.enc->output_plane.unmapOutputBuffers(i, ctx.output_plane_fd[i]);
  2342. if (ret < 0)
  2343. {
  2344. cerr << "Error while unmapping buffer at output plane" << endl;
  2345. goto cleanup;
  2346. }
  2347. ret = NvBufSurf::NvDestroy(ctx.output_plane_fd[i]);
  2348. ctx.output_plane_fd[i] = -1;
  2349. if(ret < 0)
  2350. {
  2351. cerr << "Failed to Destroy NvBuffer\n" << endl;
  2352. return ret;
  2353. }
  2354. }
  2355. }
  2356. if(ctx.capture_memory_type == V4L2_MEMORY_DMABUF && ctx.enc)
  2357. {
  2358. for (uint32_t i = 0; i < ctx.enc->capture_plane.getNumBuffers(); i++)
  2359. {
  2360. /* Unmap capture plane buffer for memory type DMABUF. */
  2361. ret = ctx.enc->capture_plane.unmapOutputBuffers(i, ctx.capture_plane_fd[i]);
  2362. if (ret < 0)
  2363. {
  2364. cerr << "Error while unmapping buffer at capture plane" << endl;
  2365. return ret;
  2366. }
  2367. NvBufSurface *nvbuf_surf = 0;
  2368. ret = NvBufSurfaceFromFd(ctx.capture_plane_fd[i], (void **)(&nvbuf_surf));
  2369. if (ret < 0)
  2370. {
  2371. cerr << "Error while NvBufSurfaceFromFd" << endl;
  2372. return ret;
  2373. }
  2374. ret = NvBufSurfaceDestroy(nvbuf_surf);
  2375. if(ret < 0)
  2376. {
  2377. cerr << "Failed to Destroy NvBuffer\n" << endl;
  2378. return ret;
  2379. }
  2380. }
  2381. }
  2382. /* Release encoder configuration specific resources. */
  2383. delete ctx.enc;
  2384. delete ctx.in_file;
  2385. delete ctx.out_file;
  2386. delete ctx.roi_Param_file;
  2387. delete ctx.recon_Ref_file;
  2388. delete ctx.rps_Param_file;
  2389. delete ctx.hints_Param_file;
  2390. delete ctx.gdr_Param_file;
  2391. delete ctx.gdr_out_file;
  2392. free(ctx.in_file_path);
  2393. free(ctx.out_file_path);
  2394. free(ctx.ROI_Param_file_path);
  2395. free(ctx.Recon_Ref_file_path);
  2396. free(ctx.RPS_Param_file_path);
  2397. free(ctx.hints_Param_file_path);
  2398. free(ctx.GDR_Param_file_path);
  2399. free(ctx.GDR_out_file_path);
  2400. delete ctx.runtime_params_str;
  2401. if (ctx.blocking_mode)
  2402. {
  2403. if (ctx.RPS_threeLayerSvc)
  2404. {
  2405. sem_destroy(&ctx.rps_par.sema);
  2406. }
  2407. }
  2408. else
  2409. {
  2410. sem_destroy(&ctx.pollthread_sema);
  2411. sem_destroy(&ctx.encoderthread_sema);
  2412. }
  2413. return -error;
  2414. }
  2415. /**
  2416. * Start of video Encode application.
  2417. *
  2418. * @param argc : Argument Count
  2419. * @param argv : Argument Vector
  2420. */
  2421. int main()
  2422. {
  2423. /* create encoder context. */
  2424. context_t ctx;
  2425. int ret = 0;
  2426. /* save encode iterator number */
  2427. int iterator_num = 0;
  2428. do
  2429. {
  2430. /* Invoke video encode function. */
  2431. ret = encode_proc(ctx);
  2432. iterator_num++;
  2433. } while((ctx.stress_test != iterator_num) && ret == 0);
  2434. /* Report application run status on exit. */
  2435. if (ret)
  2436. {
  2437. cout << "App run failed" << endl;
  2438. }
  2439. else
  2440. {
  2441. cout << "App run was successful" << endl;
  2442. }
  2443. return ret;
  2444. }