video_encode_main.cpp 77 KB

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